Friday, May 3, 2024
 Popular · Latest · Hot · Upcoming
45
rated 0 times [  45] [ 0]  / answers: 1 / hits: 83428  / 2 Years ago, mon, april 25, 2022, 8:59:31

I want to insert text at the top of an already existing file. How can I achieve this. I tried echo and tee but was not successful.



I was trying to insert repo line at the top of sources.list file from terminal.



Note



I need an one line quick solution, because another answer's method was known to me already


More From » apt

 Answers
4

It's actually quite easy with sed:




  • sed -i -e '1iHere is my new top line' filename


  • 1i tells sed to insert the text that follows at line 1 of the file; don't forget the newline at the end so that the existing line 1 is moved to line 2.



[#37546] Tuesday, April 26, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
uccase

Total Points: 473
Total Questions: 100
Total Answers: 110

Location: Anguilla
Member since Sun, Jan 29, 2023
1 Year ago
;