in reply to To add a string to the content of a file

or just do a one-liner:
perl -i.orig -p -e 's/^(#!.+)$/$1Add this line/' file

Replies are listed 'Best First'.
Re^2: To add a string to the content of a file
by sgifford (Prior) on Nov 28, 2005 at 04:36 UTC
    To expand a little bit, the -p flag tells perl to wrap the whole script inside a:
    whie (<>) { # ... your code print $_; }

    The -i.orig tells it to first rename the original input file to its old name followed by .orig, then run the script and put its output into a new file with the same name as the input file.

    I suspect the code snippet you started off with assumed you were running under perl -p.