System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

Inserting a line on top file.

 
SOLVED
Go to solution
Highlighted
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
Highlighted
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...
Highlighted
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
Highlighted
Regular Advisor

Re: Inserting a line on top file.

Thanks James,
That's do the trick !

Bests Regards
Den
Highlighted
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
Highlighted
Acclaimed Contributor

Re: Inserting a line on top file.

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