开发者

using sed to delete matching line from multiple text files

All files in a directory of my project has one line:

var $useDbConfi开发者_Python百科g = 'default_dev';

How can I delete this line from all files and then save the same file, with a single line command using sed?


you may try

sed -i.bak '/var \$useDbConfig = .*default_dev.*;/d' *

or you can use awk

awk '/var/&&/\$useDbConfig/&&/default_dev/{next}{print $0>FilENAME}' *


The -i argument to sed edits in place. With an argument, it saves a backup. So you want something like this:

STR="var $useDbConfig = 'default_dev';"
sed -i.bak "/$STR/d" *


If your sed version supports the -i option you could use this command to interactively delete the line from all files in current directory:

sed -i "/var \$useDbConfig = 'default_dev';/d" ./*

Furthermore: The way to quote the string works in bash. In other shells (like csh) you should adjust the pattern.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜