Regarding the grep command

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

grep -rni copyright * 

In the above command, I got about 1000 files.

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

+4
source share
3 answers

This will apply the 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 

Please note that this only works in the current directory. To process files in subdirectories, you probably want to use the find .

If you can describe the text change you want to make, we can offer more precise methods to achieve your goal.

+3
source
 grep -ril copyright * | xargs sed -i 's/old text/new text/' 
+1
source

There is a simple tool called headache . I found it quite useful to solve this problem. Available on Debian and Ubuntu at least.

0
source

Source: https://habr.com/ru/post/1435397/


All Articles