开发者

Removing text using grep

Trying to remove a line that contains a particular pattern in a text file. I have the following开发者_如何学C 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.


you can use sed, with in place -i

sed -i '/pattern/d' file


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.


try using -Ev

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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜