在egrep中提取模式并将它们放在输出文件中的行首?

时间:2012-08-12 22:34:06

标签: grep

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 

1 个答案:

答案 0 :(得分:1)

见评论;输入实际上是:

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

您可以将输出空间加倍以匹配预期输出:

sed G input.txt > output.txt

另外,如果你想让事情更容易阅读,你可以试验G的数量。例如,这会使文件空间增加三倍:

sed G;G input.txt > output.txt

此外,要直接对您的文件进行更改,您可以使用-i标记(这可以避免我们不必要地创建output.txt):

sed -i G input.txt