Insert line(s) at top of file using sed

sed -i '1iI am a new line' file.txt
You can use \n in your inserted data to insert multiple lines. The leading number is the position in the file where you want the insert, so in this case a '1' indicates the top of the file.

0
By: bbates
2014-02-22 14:36:57
sed

These Might Interest You

  • Use this command to insert line numbers in source files, .' ' control how spaces you insert after number. Show Sample Output


    0
    :%s/^/\=line('.').' '
    voyeg3r · 2009-08-16 17:48:03 3
  • After every line in targetfile (empty lines included) insert in a line from addfile. "Save" results to savefile. Addfile should be longer than targetfile since this doesn't loop back to the top of addfile. /^/R addfile -- says for every line that matches "has a start of line" output a line from the file addfile. > savefile (optional) -- redirect output to savefile file. Show Sample Output


    1
    sed '/^/R addfile' targetfile > savefile
    tobiasboon · 2009-04-16 05:11:07 0
  • specially usefull for sql scripts with insert / update statements, to add a commit command after n statements executed. Show Sample Output


    2
    ls -l | sed "$(while (( ++i < 5 )); do echo "N;"; done) a -- COMMIT --"
    glaudiston · 2010-03-17 20:12:05 1
  • If you've ever wanted to change a text files contents without having to create an intermediate file. This is it. Ex is a part of vim. The command as given will delete ALL lines containing "delete this line" from the file. More Examples: print '%s/'this is a line'/'that is not the same line'/g\nwq' | ex file.txt will substitute the first string with the second string. print '3a\n'Inserted Line'\n.\n\nwq' | ex file.txt will insert the given line after line 3. CAVEAT, Some distro's like the print command, others like echo with this command. Also note there are NO error messages on failure, at least that I've ever seen. Ex can also be quite fussy as to how it takes strings, parameters, etc... I use at&t's ksh syntax may very with other shells.


    0
    print 'g/'delete this line'/delete\nwq' | ex file.txt
    mph · 2009-07-20 19:15:29 3

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: