Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 647
bsmoo Avatar asked Feb 21 '15 14:02

bsmoo


People also ask

How do I grep a specific line from a file?

The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in.

How do you delete a specific line in a file Linux?

To delete a line, we'll use the sed “d” command. Note that you have to declare which line to delete. Otherwise, sed will delete all the lines.

How do you grep the line after a line?

To also show you the lines before your matches, you can add -B to your grep. The -B 4 tells grep to also show the 4 lines before the match. Alternatively, to show the log lines that match after the keyword, use the -A parameter. In this example, it will tell grep to also show the 2 lines after the match.


2 Answers

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.

like image 114
Ed Morton Avatar answered Oct 14 '22 13:10

Ed Morton


You could use sed,

sed -i '/test/d' example.txt

-i saves the changes made to that file. so you don't need to use a redirection operator.

-i[SUFFIX], --in-place[=SUFFIX]
             edit files in place (makes backup if SUFFIX supplied)
like image 29
Avinash Raj Avatar answered Oct 14 '22 12:10

Avinash Raj