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?

up vote 72 down vote accepted

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).

  • 1
    It doesn't work without -i option. I'll update your answer before accepting. – Manolo Nov 8 '13 at 12:07
  • 1
    thanks. What OS are you using? Original solution worked on OpenSuse 9. – suspectus Nov 8 '13 at 12:22
  • 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. – user15760 Nov 8 '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
  • 1
    @FeRD See my bad ranked awk answer below. This should work. – rudimeier Mar 4 at 0:02

You can use Vim in Ex mode:

ex -sc '1i|ALFA BRAVO' -cx file
  1. 1 select first line

  2. i insert text and newline

  3. x save and close

You want to insert text before the first line rather than append it after, so use

sed '1 i\
your_text' your_file

A here document can also help:

cat /dev/stdin your_file <<EOI
Your text
goes here
EOI

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
  • 5
    Please let me know why the following is downvoted – Ankit Shah Jan 9 '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 at 10:46

Using GNU awk >= 4.1:

awk -i inplace 'BEGINFILE{print "first line"}{print}' foo.sh

In opposite to all the sed answers, this works on empty files too.

  • inplace is a library, and unfortunately it appears on all awk distributions ship it. I get "awk: fatal: can't open source file 'inplace' for reading" GNU Awk 4.2.1, API: 2.0 (GNU MPFR 4.0.1, GNU MP 6.1.2) – vossad01 Jul 8 at 14:15
sed  -i '1i new_text' file_name

If you don't specify the -i option, it won't show any error, and displays the output on standard terminal, but doesn't insert the text in the file.

  • The question is not about the -i switch. – don_crissti Mar 14 '16 at 20:30
  • How do you figure that?  It is an answer to the question.  Yes, it's similar to the accepted answer, but not exactly the same. – G-Man Mar 14 '16 at 22:51
  • @G-Man - one might argue the template used by cas is far from ideal but really, this is similar like in nearly identical... the difference being the spacing and the fact this one has no backslash. As far as I am concerned they are technically identical (as they are both gnu sed specific and it just happens that gnu sed will accept the i without a backslash). If others think this warrants a separate answer + an upvote... oh well. – don_crissti Mar 15 '16 at 0:41

I am surprised that in this old question nobody has shown the most common (and quite simple in this case) sed command:

$ sed  -i '1s/^/new_text\
/' file_name

Which works in most shells and is portable to several sed versions.
If GNU sed is available, you may use this:

$ sed -i '1s/^/new_text\n' file_name

The difference is that GNU sed allow the use of a \n for a newline and others need a literal newline preceded by a backslash (which also work in GNU sed anyway).

If a shell which accepts the $'…' syntax is in use, you may insert the newline directly, so sed sees that the newline is already there:

$ sed -i $'1s/^/new_text\\\n/' file_name

Which works for more sed versions.

The 0a command you attempted does work in ex, the predecessor to vi:

printf '%s\n' 0a 'Anything you want to add' . x | ex file.txt

The printf command by itself outputs:

0a
Anything you want to add
.
x

The 0a means append after the 0th line (i.e. before the first line).

The next line or multiple lines is literal text to be added.

The . on a line by itself terminates the "append" command.

The x causes Ex to save the changes to the file and exit.

Unfortunately, all the answers above (mostly with sed) didn't work out for me since they all substituted the first line. I am on an Ubuntu 16.04 LTS machine. Here is my workaround with GNU Awk 4.0.2:

awk '{if(NR==1){$0="NEW_FIRST_LINE"$0; print $0} ;if(NR!=1){print $0}}' file_name
  • Not sure what you're talking about... There's only one sed answer that uses replacement but it doesn't substitute the existing line. – don_crissti Nov 15 at 18:06

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.