user1535967
user1535967

Reputation: 177

Regarding grep command

I am trying to change the copyright headers in my assignment. I was able to list all the files with the copyright headers by using following commmand:

    grep -rni copyright *

By the above command, I got around 1000 files.

Can anyone please help me how to change all the files in one go?

Upvotes: 4

Views: 348

Answers (3)

Explosion Pills
Explosion Pills

Reputation: 191799

grep -ril copyright * | xargs sed -i 's/old text/new text/'

Upvotes: 1

timday
timday

Reputation: 24892

There's a simple tool called headache I've found quite useful for dealing with this sort of problem. Available on Debian and Ubuntu at least.

Upvotes: 0

ghoti
ghoti

Reputation: 46886

This will apply a text change to files with the word "copyright" in them (case insensitive):

for filename in *; do
  if grep -qi "copyright" "$filename"; then
    sed -i'' -e 's/old text/new text/' "$filename"
  fi
done

Note that this only works on the current directory. To handle files in subdirectories, you'll probably want to use the find command.

If you can describe the text change you want to make, we may be able to suggest more precise methods to achieve your goal.

Upvotes: 3

Related Questions