[PLUG] Inserting text into multiple files at once...

Jeff DeFouw defouwj@purdue.edu
Mon, 21 Oct 2002 21:59:48 -0500


On Mon, Oct 21, 2002 at 09:11:54PM -0500, Tommy Montgomery wrote:
> Does anyone know what program I could use to insert a line of text into 
> multiple text files at once?  I have approximately 100 html files that I 
> need to add a CSS file tag into and I don't want to have to do this to 
> every file individually.  Any ideas?

Many options, particularly if it's the same line number in each file. 
The first that come to mind are sed and perl.  For sed you need extra
help from the shell.

To insert a line of "blahblahblah" as line 2 in each file matching
*.html:

Using sed:

In a file sedscript:
2i\
blahblahblah

(The \<newline> is part of the syntax.  It's easier to use in a file
instead of the command line.)

for file in *.html; do
  sed -f sedscript $file > $file.sed && mv $file.sed $file
done

Using perl:

perl -pi -e 'print "blahblahblah\n" if ($. == 2); close ARGV if eof' *.html

-- 
Jeff DeFouw <defouwj@purdue.edu>