Operating System - Linux
1755834 Members
4075 Online
108838 Solutions
New Discussion юеВ

Inserting a line on top file.

 
SOLVED
Go to solution
Leo The Cat
Regular Advisor

Inserting a line on top file.

Hi Guys

How to insert a new line with text at the beginning of a text file ?

Bests Regards
Den
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor
Solution

Re: Inserting a line on top file.

Hi Den:

This will perform an "in-place" update:

# perl -pli -e '$.==1 and print "INSERTED"' file

Regards!

...JRF...
Shoghi Martinez G.
Honored Contributor

Re: Inserting a line on top file.

Assuming that you are talking about scripting,
Maybe something lke this:

file.txt : the file without the the new header file
head.txt : a file with the line to be inserted in top.

mv file.txt file.txt.old
cat head.txt file.txt.old > file.txt
Leo The Cat
Regular Advisor

Re: Inserting a line on top file.

Thanks James,
That's do the trick !

Bests Regards
Den
Fredrik.eriksson
Valued Contributor

Re: Inserting a line on top file.

Another, non-perlish, solution would be to just recreate the file.

echo "what-you-want-the-top-line(s)-to-contain" > tmp.file
cat orig.file >> tmp.file
mv tmp.file orig.file

Best regards
Fredrik Eriksson
Dennis Handly
Acclaimed Contributor

Re: Inserting a line on top file.

You can also use sed:
sed '
1i\
line
' file > file.new