在egrep中提取模式并将其放置在输出文件的行的开头?

在egrep中提取模式并将其放置在输出文件的行的开头?,grep,Grep,在grep/egrep中有没有一种方法可以从这个文本中提取复杂的模式,将它们插入到一行的开头,并保留行的剩余部分,这样看起来就像下面这样 从许多具有“非特定”一词的文件中提取的原始文本。现在,我需要组织这些名称,以便名称从行的开头开始,以便更容易阅读。在它们之间插入一条空行也会有帮助,但在egrep中可能无法做到这一点 输入: SofasCouchesChairs/Type1234567.xml:Nonspecific Couch-W ISSUESTablesDesks/Type123765.x

在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 
预期输出:

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 

见评论;输入实际上是:

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
):


我需要组织这些名称,以便名称从行的开头开始
。你能解释一下名字的构成吗?@djf是的,对不起。在上面的框中,有组织的文本没有按我所希望的那样显示。他们混在一起了。每个名称看起来都像“臭虫/Type2893993.xml:”并且应该从行首开始。@nlper:我们应该如何处理,例如,
异常臭虫/Type2893993.xml:
IssueStablesDesk/Type123765.xml:
?@nlper:当您从许多有“非特异性”一词的文件中提取原始文本时,你能设置某种分隔符吗?在您的例子中,空分隔符使下游操作更加困难。@nlper:我对您的输入看起来有点困惑。可以用4个空格缩进文本以开始代码块。我已尝试更新您的输入,但可能理解不正确。请作相应调整。然而,我确实认为你的问题可能源于抄写到记事本上。你应该避免这样。我假设您正在搜索多个模式。如果您多次调用grep,只需将其附加到文件中,即:
grep“pattern”input>>output
。或者你可以像这样搜索多个模式:
grep-P“yourexpression”anotherexpression“input>output.txt