« Back to Data processing: Frequently Asked Questions

How to insert a line at the beginning of a file

To insert a line at the beginning of a file, we can use sed to achieve this task. By using in-place editing -i, we can instruct sed to make a change to an existing file. The next step is to tell sed what to change or insert and at what place.

sed -i '1i # New first line' mytextfile.txt


  • -i = inline file edit
  • 1i = insert at first line
  • # New first line = Text to add

Other questions related to Data processing


Is the described answer not working or incorrect, got another tip or question? Share your thoughts!