Reputation: 4906
I have the following file :
#!example
#!toto
example
#example
;example
toto
example
I want to remove the lines containing the string "example"
except the lines start with "#!"
.
So the result file should be:
#!example
#!toto
toto
How to do it with only sed
command?
Upvotes: 2
Views: 4114
Reputation: 58420
This might work for you (GNU sed):
sed '/#!/b;/example/d' file
This prints all lines containing #!
and all other lines except those containing example
.
Upvotes: 2
Reputation: 195059
how about this line:
sed '/^#!/n;/example/d' file
test with your example text:
kent$ cat file
#!example
#!toto
example
#example
;example
toto
example
kent$ sed '/^#!/n;/example/d' file
#!example
#!toto
toto
if you like, awk could do it too:
kent$ awk '/^#!/ || !/example/' file
#!example
#!toto
toto
EDIT
sed:
starting with #!
, stop processing, go to next line (n
)#!
), check if containing example
, if yes, d
delete (do not print in output)awk
#!
or (||
) not containing example.performance:
I cannot really tell. because the requirement is simple enough. You could build a huge file, and use time
to compare by yourself.
Upvotes: 4
Reputation: 1
If you want to keep only lines starting with #!
$ sed '/#!/!d' foo.sh
#!example
#!toto
Upvotes: 2