pMan
pMan

Reputation: 9198

using sed to delete matching line from multiple text files

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

var $useDbConfig = 'default_dev';

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

Upvotes: 0

Views: 1716

Answers (3)

bmk
bmk

Reputation: 14147

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.

Upvotes: 0

drysdam
drysdam

Reputation: 8637

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" *

Upvotes: 1

ghostdog74
ghostdog74

Reputation: 343201

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}' *

Upvotes: 1

Related Questions