mrblippy
mrblippy

Reputation: 311

Removing text using grep

Trying to remove a line that contains a particular pattern in a text file. I have the following code which does not work

grep -v "$varName" config.txt

Can anyone tell me how I can make it work properly, I want to make it work using grep and not sed.

Upvotes: 2

Views: 33518

Answers (3)

ghostdog74
ghostdog74

Reputation: 342363

you can use sed, with in place -i

sed -i '/pattern/d' file

Upvotes: 6

Paul Tomblin
Paul Tomblin

Reputation: 182782

grep doesn't modify files. The best you can do if you insist on using grep and not sed is

grep -v "$varName" config.txt > $$ && mv $$ config.txt

Note that I'm using $$ as the temporary file name because it's the pid of your bash script, and therefore probably not a file name going to be used by some other bash script. I'd encourage using $$ in temp file names in bash, especially ones that might be run multiple times simultaneously.

Upvotes: 3

Spooks
Spooks

Reputation: 7177

try using -Ev

grep -Ev 'item0|item1|item2|item3'

That will delete lines containing item[0-3]. let me know if this helps

Upvotes: 0

Related Questions