LA.pm: Prepend

Jeff Yoak jeff at yoak.com
Fri Oct 5 14:46:41 CDT 2001


At 01:28 PM 10/5/2001 -0500, Arkadiy Sudarikov wrote:
>Hey there. Got a question fer ya. Can anyone please help me prepend a line
>to a file?

There is no really cool way to do that... well... depending on what your 
idea of cool is.  :-)  I mean there is no way to "open to prepend" or 
anything.  You either need to write a new file and move it, or read it in 
the memory and then open to write and print it out.  Or if you need it 
flock safe because it might happen simultaneously, try open +< read it in, 
seek to the beginning, write your new line and then print the rest.  Or if 
your priority is terseness of expression, you could:

perl -pi -e 's/$_/newline\n$_/ if $.==1' foo.txt

This puts "newline\n" as the first line.









More information about the Losangeles-pm mailing list