best way to insert a line at the top of a file?

say I want to insert "this is a test" as the first line into file A, besides

echo "this is a test" > /tmp/tmpfile

cat /tmp/tmpfile fileA >> /tmp/result,

is there any simple way I can do it? thanks

you mean on a file that's already there???

if the file in question isn't so huge it crashes vi, try this:


vi file << EOF
OWhatever you want as first line
^[:wq!
EOF

Where the ^[ is actually the ESCAPE character entered ( through vi ) with
CONTROL-V and ESC.

but honestly.... no, there isn't a better way.

Also, see this older thread:

thanks, just wondering if there is a better way can be used to do this.

"Better" is "relative" (and does not have much context)

If you like PERL, then a perl solution is "better".....

What do you mean by "better" ?

you can use the sed "insert" comand:

the_prompt$ cat thefile

returns:

line1
line2
line3
the_prompt$ sed -e '1 i \the line you wanna put first' thefile

returns:

the line you wanna put first
line1
line2
line3

No, that is not correct...

Yes I agree.. use the sed insert command, and you can even do it inline so that you do not have to pipe to another file then replace the old with the new.

[user@host ~]$ cat lines.txt
LineA
LineB
LineC

[user@host ~]$ sed -i '1 i \Line1' lines.txt
[user@host ~]$ cat lines.txt
Line1
LineA
LineB
LineC