[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Feature Request - prepend to file
From: |
Bob Proulx |
Subject: |
Re: Feature Request - prepend to file |
Date: |
Sat, 5 Apr 2008 21:01:11 -0600 |
User-agent: |
Mutt/1.5.13 (2006-08-11) |
Eric Blake wrote:
> Brock Noland wrote:
> | I find myself for whatever reason, wanting to prepend a file (or
> | stdin) to a file. Most often when dealing XML that does not have a top
> | level tag.
> |
> | This can be done safely in the shell but requires a lot of work.
> | (Maybe there is already a better way?)
>
> What shell commands have you been trying? I find the following to be
> relatively simple to do:
>
> { echo header; cat file; } > file1 && mv file1 file
In addition to the above if I restrict myself to programs that already
know how to edit files in place I can think of a few additional easy
ways. Sed of course comes to mind. Here is one way to use sed to
edit a file in place and insert "foo" at the first line of the file.
sed --in-place '1i\
foo' bar
Using perl:
perl -i -lpe 'print "foo" if $. == 1;' bar
Using ruby:
ruby -i -lpe 'print "foo" if $. == 1' bar
Being an 'ed' fan I would would probably use ed.
ed -s bar <<EOF
i
foo
.
w
q
EOF
Or to read a file "foofile" at the first line of the file.
ed -s bar <<EOF
0r foofile
w
q
EOF
Bob