Operating System - HP-UX
1833056 Members
2590 Online
110049 Solutions
New Discussion

append multiple lines of text at begining of a existing file

 
steve-n
Occasional Contributor

append multiple lines of text at begining of a existing file

how can I append multiple lines of text at begining of a existing file.

I can use awk 'BEGIN { print "line1} {print}' file.org >> file.new.

Outcome:
line1
line2
existing text
3 REPLIES 3
Pete Randall
Outstanding Contributor

Re: append multiple lines of text at begining of a existing file

You can do something as simple as this:

echo line1 > file.new
echo line2 >> file.new
cat file.orig >> file.new
mv file.new file.orig


Pete

Pete
Hein van den Heuvel
Honored Contributor

Re: append multiple lines of text at begining of a existing file

>> how can I append multiple lines of text at begining of a existing file.

You can not, not directly.

You'll need to stash the lines from the existign files, or use an intermediate file.

To give but a simple example:

$ cat x y > y
cat: Cannot use y as both input and output.

'solution'

$ cat x y > z
$ mv z y

Here is a silly PERL solution which SLURPs the input files, then BURPs the output.

perl -e '$a = do { local( @ARGV, $/)=shift;<>}; $out=shift; $b = do { local( @ARGV, $/)=$out;<>}; open $fh,">$out"; print $fh $a.$b' x y'

Remember, beauty is in the eye of the beholder

The 'local $/' undefines the line terminator making the whole file a piece of string.

http://www.perl.com/pub/a/2003/11/21/slurp.html

hth,
Hein.

steve-n
Occasional Contributor

Re: append multiple lines of text at begining of a existing file

na