vinaym
vinaym

Reputation: 467

Removing a line from multiple files using grep & sed

I have several source code files in a multi-level directory which contain forward declaration to a function

extern "C" void myPrintf(char *fmt, ...);

I want to remove this declaration from all the files. I could do a grep which prints the line containing that declaration for all the files but not sure how to pipe it with sed to get the desired result.

Upvotes: 0

Views: 1161

Answers (2)

jaypal singh
jaypal singh

Reputation: 77185

Using awk:

awk '/void myPrintf/{next}1' oldfile > newfile

For batch updates, you can do something like this -

find /path/to/files -type f -iname "*.c" | while read file; do
awk '/void myPrintf/{next}1' "$file" > "$file".new
mv "$file".new "$file"
done

Upvotes: 0

SiegeX
SiegeX

Reputation: 140557

With GNU sed

sed -i '/extern "C" void myPrintf(char \*fmt, \.\.\.);/d' *.c

If you want to perform this recursively, you have two options:

With Bash 4.X

shopt -s globstar; sed -i '/extern "C" void myPrintf(char \*fmt, \.\.\.);/d' **.c

With find

find /some/path -type f -name "*.c" -exec sed -i '/extern "C" void myPrintf(char \*fmt, \.\.\.);/d' {} \;

Upvotes: 1

Related Questions