Prepare some files in a bash / osx terminal

I would like to add some text to several files in bash, I found this message that deals with prepend: add one liner shell to the file?

And I can find all the files that I need to process using find:

find ./ -name "somename.txt" 

But how do I combine the two using the channel?

+4
source share
4 answers

You have several options. The easiest option is sed:

 find ./ -name somename.txt -exec sed -e '1i My new text here` {} \; 

It will be faster if you add '2q' to say that it is done after the preliminary text, and if this happens in the file with the -i flag

 find ./ -name somename.txt -exec sed -i .bak -e '2q;1i My new text here` {} \; 

This leaves the source files with the extension .bak .

+9
source
 find . -name "somefiles-*-.txt" -type f | while read line; do sed -i 'iThis text gets prepended' -- "$line"; done 

or

 find . -name "somefiles-*-.txt" -type f | xargs sed -i 'iGets prepended' -- 

Best (I think):

 find . -name "somefiles-*-.txt" -type f -exec sed -i 'iText that gets prepended (dont remove the i)' -- '{}' \; 

Thanks for the missing "-hint. I have also added important --s.

+2
source
 find ./ -name "somename.txt" -print0 | xargs -0 -n 1 prepend_stuff_to 
0
source
 find . -name "somename.txt" | while read a; do prepend_stuff_to "$a" ; done 

or

 find . -name "somename.txt -exec prepend_stuff_to "{}" \; 
-1
source

All Articles