user954299
user954299

Reputation: 195

Unix commands to remove a particular line form all files

is there any command or a way in unix to remove particular line from all the files.

example : I have some 300 .c files where the file header has author name. and I need to remove this line from all the files. I am looking to do this in oneshot.

if there is any command or a way please help me on this.

Thanks..

Upvotes: 0

Views: 1121

Answers (4)

ilomambo
ilomambo

Reputation: 8350

perl -i.bak -pne "s/^whatever it is your line to delete$//" files

Will create files without your line with extension .bak. Try it on one file and if you are confident enough to overwrite all your files you can do it by:

perl -i -pne "s/^whatever it is your line to delete$//" files

This will overwrite the original file. Other similar options are:

perl -i -ne "print if !/^whatever it is your line to delete$/" files

perl -i -pne "next if /^whatever it is your line to delete$/" files

Make sure you define the line from '^' (begining of line) to '$' (end of line) otherwise you might delete similar lines that have a partial match.

Upvotes: 0

Darknight
Darknight

Reputation: 1152

Try this,

grep -lr "<Authorname keyword>" *.c|xargs sed -i '0,/<Authorname>/d' 

grep - list all the c files in the location with matching keyword '', then sed - delete the 1st line it matches the keyword

Go through the site sed examples

for your future references

Upvotes: 0

Q_SaD
Q_SaD

Reputation: 355

Use this command

find -iname "*.c" -exec sed -i '/author/'d {} \;

First find command will find all c files and then sed will delete the author line in the file.

Upvotes: 0

fedorqui
fedorqui

Reputation: 289725

Supposing you have a pattern to find these .c files, you can use some solutions from Sed - Delete a line containing a specific string:

find . -type f -name "your_pattern.c" -exec sed -i '/pattern/d' {} \;

Upvotes: 2

Related Questions