【发布时间】:2012-08-12 22:34:05
【问题描述】:
在 grep/egrep 中是否有一种方法可以从该文本中提取棘手的模式,将它们插入到行首以及剩余行,使其如下所示?
从许多包含“非特定”一词的文件中提取的原始文本。现在我需要组织这些,以便名称从行首开始,以便于阅读。在它们之间插入一个空行也会有所帮助,但这在 egrep 中可能是不可能的?
输入:
SofasCouchesChairs/Type1234567.xml:Nonspecific Couch-W ISSUESTablesDesks/Type123765.xml:Nonspecific Tables issues BedsDivans/Type4567345.xml:Nonspecific bed abnormalitiesBedBugs/Type2893993.xml:Nonspecific bugs in the spring boxes related to the mattressBed_Sofas/Type1317994.xml:Nonspecific WR abnormalities these are from Radios_TV/Type1274978.xml:radiation perhaps with nonspecific cell phones and cell towers Cabinets_TelephoneWires/Type1299691.xml:DATA:all kinds of nonspecific cell phone wave changes, with a
预期输出:
SofasCouchesChairs/Type1234567.xml:Nonspecific Couch-W ISSUES
TablesDesks/Type123765.xml:Nonspecific Tables issues
BedsDivans/Type4567345.xml:Nonspecific bed abnormalities
BedBugs/Type2893993.xml:Nonspecific bugs in the spring boxes related to the mattress
Bed_Sofas/Type1317994.xml:Nonspecific WR abnormalities these are from
Radios_TV/Type1274978.xml:radiation perhaps with nonspecific cell phones and cell towers
Cabinets_TelephoneWires/Type1299691.xml:DATA:all kinds of nonspecific cell phone wave changes, with a
【问题讨论】:
-
I need to organize these so that the names start at the start of the line。你能解释一下什么是名字吗? -
@djf 是的,很抱歉。有组织的文本没有像我希望的那样出现在上面的框中。他们混在一起了。每个名称看起来都像“BedBugs/Type2893993.xml:”并且应该从行首开始。
-
@nlper:我们应该如何处理,例如
abnormalitiesBedBugs/Type2893993.xml:或ISSUESTablesDesks/Type123765.xml:? -
@nlper:当您从许多包含“非特定”一词的文件中提取原始文本时,您是否能够设置某种分隔符?在您的情况下,空分隔符使下游操作变得更加困难。
-
@nlper:我对您输入的内容有点困惑。您可以缩进 4 个空格的文本来开始一个代码块。我已尝试更新您的输入,但我可能没有正确理解。请相应调整。但是,我确实认为您的问题可能源于复制到记事本。你应该避免这种情况。我假设您正在搜索多种模式。如果您多次调用 grep,只需附加到您的文件,即:
grep "pattern" input >> output。或者您可以像这样搜索多个模式:grep -P "yourexpression|anotherexpression" input > output.txt
标签: grep