http://www.perlmonks.org?node_id=7244


in reply to Writing to files

All the above good advice applies, but one liners are cool:
perl -i -p -e "BEGIN{print 'A new line!\n';}" your files here

Replies are listed 'Best First'.
RE: Re: Writing to files
by chromatic (Archbishop) on Apr 10, 2000 at 21:01 UTC
    I had better luck with this: perl -i -p -e 'print "first line\n" if $. == 1;' data It depends on the particular magic of $., of course.
RE: Re: Writing to files
by btrott (Parson) on Apr 10, 2000 at 20:42 UTC
    Unfortunately, this doesn't work, because the BEGIN occurs before STDOUT is redirected to the output file (the file you're writing to). Plus, in your case, you used single quotes to surround your "\n", so it didn't get interpolated.

    Here's what I get:

    % perl -i -p -e "BEGIN{print 'A new line!\n';}" foo A new line!\n%
    I tried this myself, the other day, but ran into the same problem.