简体   繁体   中英

Sed : replace words only with first occurrence of string in the line not till last match

I am stuck at one point in my script. Can you please help with this.

Problem : there is a file with lines:

"<abc>-group ab:cd:ef +define_1 +DEFINE_2 </abc>"

Now i want to remove -group ab:cd:ef from all the lines in that file. I tried with

%s/-group .* //g

but it will remove all the test till the last space. and the new text will become <abc></abc> where as i want something like this :

<abc>+define_1 +DEFINE_2 </abc>

Thanks.

Using sed

sed 's/-group [[:graph:]]* //' file

in vi/vim:

%s/-group [[:graph:]]* //g

Using awk

awk '{sub(/-group [^ ]* /,"")}1' file
"<abc>+define_1 +DEFINE_2 </abc>"

Or this sed

sed -r 's/-group [^ ]* //' file
"<abc>+define_1 +DEFINE_2 </abc>"

you should change the search & replace expression to something like this:

`%s/-group ab:cd:ef //g`

It would avoid the whole line matching.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM