Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove all lines matching a pattern from a set of files?

I've got an irritating closed-source tool which writes specific information into its configuration file. If you then try to use the configuration on a different file, then it loads the old file. Grrr...

Luckily, the configuration files are text, so I can version control them, and it turns out that if one just removes the offending line from the file, no harm is done.

But the tool keeps putting the lines back in. So every time I want to check in new versions of the config files, I have to remove all lines containing the symbol openDirFile.

I'm about to construct some sort of bash command to run grep -v on each file, store the result in a temporary file, and then delete the original and rename the temporary, but I wondered if anyone knew of a nice clean solution, or had already concocted and debugged a similar invocation.

For extra credit, how can this be done without destroying a symbolic link in the same directory (favourite.rc->signals.rc)?

like image 393
John Lawrence Aspden Avatar asked Apr 18 '12 09:04

John Lawrence Aspden


People also ask

How do I remove a specific pattern from a Unix file?

N command reads the next line in the pattern space. d deletes the entire pattern space which contains the current and the next line. Using the substitution command s, we delete from the newline character till the end, which effective deletes the next line after the line containing the pattern Unix.


1 Answers

sed -i '/openDirFile/d' *.conf

this do the removing on all conf files

you can also combine the line with "find" command if your conf files are located in different paths.

Note that -i will do the removing "in place".

like image 115
Kent Avatar answered Oct 01 '22 00:10

Kent