开发者

Removing text using grep

开发者 https://www.devze.com 2023-01-12 16:54 出处:网络
Trying to remove a line that contains a particular pattern in a text file.I have the following开发者_如何学C code which does not work

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

精彩评论

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