tags:

views:

53

answers:

3

Hi, i am 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

A: 

try using -Ev

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

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

Spooks
grep doesn't delete anything. It either prints lines that matche or lines that doesn't match. You'll need to redirect output in order to save what is printed.
Bryan Oakley
+2  A: 

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.

Paul Tomblin
Better still, use the `mktemp` utility.
Zack
+1  A: 

you can use sed, with in place -i

sed -i '/pattern/d' file
ghostdog74