derwiki

May 22 2008

BASH Scripting: Inserting text into a fixed location of a file

Ever need to insert some test into the middle of a file? Turns out there’s a pretty simple way to do it! Assume you want to put the contents of “addition.txt” into “myfile.txt” at line 10:

head -n 10 myfile.txt > output.txt
cat addition.txt » output.txt
tail -n +10 myfile.txt » output.txt
mv output.txt myfile.txt

Of course, 10 can be changed to a variable, and file names can be changed to variables.

Comments (View)
blog comments powered by Disqus