163

I've been looking around sed command to add text into a file in a specific line. This works adding text after line 1:

sed '1 a\

But I want to add it before line 1. It would be:

sed '0 a\

but I get this error: invalid usage of line address 0.

Any suggestion?

αғsнιη
  • 41,407
Manolo
  • 1,767
  • 2
  • 11
  • 11

16 Answers16

218

Use sed's insert (i) option which will insert the text in the preceding line.

sed '1 i\

Question author's update:

To make it edit the file in place - with GNU sed - I had to add the -i option:

sed -i '1 i\anything' file

Also syntax

sed  -i '1i text' filename

For non-GNU sed

You need to hit the return key immediately after the backslash 1i\ and after first_line_text:

sed -i '1i\
first_line_text
'

Also note that some non-GNU sed implementations (for example the one on macOS) require an argument for the -i flag (use -i '' to get the same effect as with GNU sed).

For sed implementations that does not support -i at all, run without this option but redirect the output to a new file. Then replace the old file with the newly created file.

Kusalananda
  • 333,661
suspectus
  • 6,010
  • 1
    It doesn't work without -i option. I'll update your answer before accepting. – Manolo Nov 08 '13 at 12:07
  • 1
    thanks. What OS are you using? Original solution worked on OpenSuse 9. – suspectus Nov 08 '13 at 12:22
  • Linux Mint 13 Maya – Manolo Nov 08 '13 at 12:25
  • 1
    Interesting! I'm on Lubuntu 13.10 and sed '1 i\this text is entered above the existing first line' file works for me. –  Nov 08 '13 at 13:47
  • 2
    Here, using sed (GNU sed) 4.2.2 just sed '1 i text to insert' -i file worked like a charm. Thanks – ton Feb 15 '16 at 14:06
  • can confirm, gnu sed does not require the -i flag – Yoshua Wuyts Mar 02 '16 at 00:11
  • 6
    Does not work on empty files. – rudimeier Sep 28 '16 at 20:32
  • 1
    The -i argument to sed tells it to edit the file in-place — I have a feeling all of the commenters here were seeing the same behavior, but simply had different expectations. Without -i, sed will execute the commands specified and output the results to the terminal, unless you redirect it somewhere else. WITH -i, it will actually modify the file on disk, and write the changed version in place of the original. Two different approaches to sed-ing a file's contents, depending whether you need to preserve the unaltered version of the input file. – FeRD Mar 03 '18 at 23:27
  • 1
    @rudimeier Huh! You're right, it doesn't work on completely empty files. I guess because sed is super-literal, and you can't insert text at line 1 when there isn't a "line 1" in the file. So touch empty.txt && sed '1i Text' -i empty.txt will do nothing, whereas echo > blank.txt && sed '1i Text' -i blank.txt will result in blank.txt containing a line that reads "Text", with a blank line after it. – FeRD Mar 03 '18 at 23:41
  • 1
    @FeRD See my bad ranked awk answer below. This should work. – rudimeier Mar 04 '18 at 00:02
  • Well, inserting text into a completely empty file is kind of a pathological case, anyway. I can see how some script might need to be able to handle that case, but in any interactive use the way to "insert" text at the start of an empty file is just echo "text" > file. :-) – FeRD Mar 04 '18 at 00:05
  • I get "invalid command code f" with osx – Johny19 Apr 12 '18 at 09:45
  • Just added an update for OSX – suspectus Apr 15 '18 at 20:06
  • How do you insert a string which needs to be processed first? – Timothy Swan Apr 18 '18 at 21:40
  • 1
    For macOS if you declare double quotes for some reason then you should declare \ just before the new-line or text. for example: sed -i '' "1i\\ " – emrcftci Jan 04 '22 at 15:21
  • not sure what this command do, i just got sed: 1: "~/testing.txt": extra characters at the end of q command, definitely not a solution for the post title – CuriousNewbie Apr 07 '22 at 05:36
  • For MacOS sed: -e $'1i\\\ntext to add' – Rob Aug 11 '22 at 20:50
  • how toadd variable value in sed command – rose Nov 15 '22 at 05:08
44

Echo is used to get the text. Cat filename - prints the file in the console and > uses it to send to another file filename1 and then we move filename1 to filename to get the text inserted to the first line of desired file.

  (echo "some text" && cat filename) > filename1 && mv filename1 filename
Ankit Shah
  • 604
  • 5
  • 7
  • 11
    Please let me know why the following is downvoted – Ankit Shah Jan 09 '17 at 14:58
  • 1
    Perhaps because the OP mentions sed. That does not deserve a down vote in my opinion. That solution was even the best fitting my needs, then upvoted. – рüффп Apr 12 '18 at 10:46
  • 1
    I upvoted. works on ksh for me. – arcee123 Aug 22 '19 at 13:47
  • 3
    this is great solutions! it work in Makefile & MacOS well. – zx1986 Jun 18 '20 at 23:33
  • 1
  • Sed is installed everywhere
  • This is not an elegant solution : You call 1 builtin + cat + mv instead of just calling one command. Imagine the waste of time if this solution is used as part of a script that will run it thousands of time.
  • – binarym Jun 10 '22 at 09:09
  • 1
    @binarym as what I know, sed is not globally working in sync everywhere, for example : some of sed command are not do-able in OSX, even tho the program name is called sed, some say installing gnu-sed will fix the problem, but that mean not using the tool that comes with the OS anymore, instead, using outsourcing tool, but again, the post creator asking for sed, that's probably the main reason this answer in not accepted as solution. – CuriousNewbie Nov 11 '22 at 01:05