开发者

using sed to delete matching line from multiple text files

开发者 https://www.devze.com 2023-03-02 02:25 出处:网络
All files in a directory of my project has one line: var $useDbConfi开发者_Python百科g = \'default_dev\';

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

精彩评论

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

关注公众号