English 中文(简体)
Oliver SED 稿发现第1行与模式相匹配,删除
原标题:Linux SED script find the 1st line that match pattern and delete it

我想建立一个精干的指挥系统,在档案中找到一条与模式相匹配的线,删除所有这条线,或者用一些其他案文取而代之。 我急切希望与这条线一致,因为这一规则要与其中一部分相匹配。

怎么能够做到。

例如:

myline 1 is pretty
line 2 is ugly
myline 111 is nice

我想删除第1行“1”的内容。

最新资料:我的行文可能具有“/”和“及”等特性;”

Regards Fak

最佳回答

我倾向于使用<代码>awk来处理更为复杂的任务,它比<代码><<>>>>>更强大,拥有适当的住宿和甄选结构(如果你愿意,可将其压缩到一条线):

pax$ echo  
xyz
myline 1 is pretty
line 2 is ugly
myline 111 is nice   | awk  
     /1 is/ {if (f) {print} else {f = 1}}
    !/1 is/ {print} 

xyz
line 2 is ugly
myline 111 is nice

对于配对模式(!/1 是/)的任何线而言,它只是打印。

对于符合模式的“do线”的线路,如果设定了国旗f(最初未定)。 当旗帜没有打上,并碰到一个配对线时,该旗帜就贴上国旗,并用斜线打印。 这基本上删除了所希望的第一个配对线。

如果你想要修改第一条配对线,而不是删除,你就在f = 1旁边插入代码,以便做到这一点,例如:

pax$ echo  
xyz
myline 1 is pretty
line 2 is ugly
myline 111 is nice   | awk  
     /1 is/ {if (f) {print} else {f = 1;print "URK!!!"}}
    !/1 is/ {print} 

xyz
URK!!!
line 2 is ugly
myline 111 is nice

为在<代码>awk上使用单壳变量,可使用<代码>-v的备选办法将其通过成真正的<代码>awk<>/code>。 变量:

pax$ export xyzvar=URK ; echo  
xyz
myline 1 is pretty
line 2 is ugly
myline 111 is nice   | awk -vmyvar=$xyzvar  
     /1 is/ {if (f) {print} else {f = 1;print myvar}}
    !/1 is/ {print} 

xyz
URK
line 2 is ugly
myline 111 is nice
问题回答

http://sed.sourceforge.net/sedfaq4.html#s4.11”rel=“noreferer” FAQ:

sed  0,/RE/{//d;}  file        # delete only the first match
sed  0,/RE/s//to_that/  file   # change only the first match

如果您不使用全球化学品统一分类标签编码,那么就可查到联邦数据表中的替代品。

你们能够做到不 s。 印本:

LINE=$(grep -n "$EXPR" "$FILE" |head -1 |cut -f1 -d:)
head -$(expr $LINE - 1 ) $FILE
tail +$(expr $LINE + 1 ) $FILE

仅作以下声明:$EXPR_$FILE





相关问题
Acronyms with Sphinx search engine

how can i index acronyms like m.i.a. ? when i search for mia , i get results for mia and not m.i.a. . when i search for m.i.a. , i get nothing at all. edit: solution looks roughly like: ...

Querying multiple index in django-sphinx

The django-sphinx documentation shows that django-sphinx layer also supports some basic querying over multiple indexes. http://github.com/dcramer/django-sphinx/blob/master/README.rst from ...

Adding Search to Ruby on Rails - Easy Question

I am trying to figure out how to add search to my rails application. I am brand new so go slow. I have created a blog and done quite a bit of customizing including adding some AJAX, pretty proud of ...

Searching and ranking short phrases (e.g. movie titles)

I m trying to improve our search capabilities for short phrases (in our case movie titles) and am currently looking at SQL Server 2008 Full Text Search, which provides some of the functionality we ...

Will Full text search consider indexes?

Ok I have a full text search index created on my JobsToDo table, but what I m concerned about is if this is rendering my other indexes on the table useless. I have a normal nonclustered index on the ...

Lucene.NET on shared hosting

I m trying to get Lucene.NET to work on a shared hosting environment. Mascix over on codeproject outlines here how he got this to work on godaddy. I m attempting this on isqsolutions. Both ...

Hibernate Search or Compass

I can t seem to find any recent talk on the choice. Back in 06 there was criticism on Hibernate Search as being incomplete and not being ready to compete with Compass, is it now? Has anyone used both ...

热门标签