Search code examples
stringbashawksedgrep

grep for a line in a file then remove the line


$ cat example.txt

Yields:

example
test
example

I want to remove 'test' string from this file.

$ grep -v test example.txt > example.txt 
$ cat example.txt 
$

The below works, but I have a feeling there is a better way!

$ grep -v test example.txt > example.txt.tmp;mv example.txt.tmp example.txt 
$ cat example.txt 
example
example

Worth noting that this is going to be on a file with over 10,000 lines.

Cheers


Solution

  • You're doing it the right way but use an && before the mv to make sure the grep succeeded or you'll zap your original file:

    grep -F -v test example.txt > example.txt.tmp && mv example.txt.tmp example.txt
    

    I also added the -F options since you said you want to remove a string, not a regexp.

    You COULD use sed -i but then you need to worry about figuring out and/or escaping sed delimiters and sed does not support searching for strings so you'd need to try to escape every possible combination of regexp characters in your search string to try to make sed treat them as literal chars (a process you CANNOT automate due to the position-sensitive nature of regexp chars) and all it'd save you is manually naming your tmp file since sed uses one internally anyway.

    Oh, one other option - you could use GNU awk 4.* with "inplace editing". It also uses a tmp file internally like sed does but it does support string operations so you don't need to try to escape RE metacharacters and it doesn't have delimiters as part of the syntax to worry about:

    awk -i inplace -v rmv="test" '!index($0,rmv)' example.txt
    

    Any grep/sed/awk solution will run the in blink of an eye on a 10,000 line file.