-1

Hi i have a question i have searched everywhere but i cant find it.

I want to use the sed command to remove a line containing a certain string. In the text file there are more lines with the same string . The sed command has to delete only the first string and not the rest. How can i do that can somebody tell me? im gonna use it in a shell script wich will be activated multiple times till it runs out of the same string

For example i have the following lines:

aaa 
aaa 
aaa 
bbb 
ccc 

and i only want to remove 1 line with aaa

1
  • 1
    Please edit your question to add information. Don't add it as comments
    – roaima
    Apr 28 at 23:07

1 Answer 1

2

It's more straightforward with awk:

awk '!/aaa/ || n++' < your-file

Which prints the current record if it does not contain aaa or, if it does, that the post-increment of some counter is non-zero (which it will be after the first occurrence).

With GNU sed, you can do:

sed '0,/aaa/{//d}' < your-file

Which only deletes a aaa line if it's in the range of lines from the beginning of the file to the first occurrence of aaa.

See the sed FAQ for other possible approaches for non-GNU sed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.