I have an external program that produces an output file (largish, 20K lines possible).

I need to insert a new line between the existing line 1 and line 2. I've been looking at awk and sed - I use one liners in each fairly regularly - but I haven't been able to come up with the right switches to do this.

link|improve this question

feedback

9 Answers

up vote 16 down vote accepted
awk 'NR==1{print; print "new line"} NR!=1'
link|improve this answer
10  
A little shorter: awk 'NR==2 {print "new line"} 1' – glenn jackman Jan 23 at 17:31
feedback

For your specific case, this should be simpler:

sed '1 { P ; x }' your-file

Explanation: at line 1, do the following

  1. Print the line
  2. Exchange the pattern space with the holding space (basically empties the buffer)

Then, the line (empty now) is printed again as part of the cycle.


If you want to add a new line instead of a new line character (what I understood initially) then just use sed's command a\ (append):

sed '1 a\
appended line' your-file

or even

sed '1 aappended line' your-file

It appends "appended line" after line 1.

link|improve this answer
sed -i '1 a\\' file for just a newline. – forcefsck Jan 23 at 18:13
@forcefsck Yes, only disadvantage is that it's a GNUism, as my second '1 aapended line' example. – angus Jan 24 at 10:36
feedback
sed ':a;N;$!ba;s/\n/\n\n\n/' yourBigFile

Explanation.

link|improve this answer
feedback

I guess the sed approach would be:

sed '2 i whatever_line_of_text_you_wanted_to_INSERT' filename.txt

This will get the text into the second line of the file and then the the actual second line with in the file will become the third.

Note that, using append mode, if it had to be, it had to use the first line, since the append will happen after the line number noted.

sed '1 a whatever_line_of_text_you_wanted_to_INSERT' filename.txt

link|improve this answer
feedback

Using sed and (Bash-specific) ANSI escape codes:

# note: \ --> \\
printf '%s\n' 1 2 3 4 5 | sed -e $'1 { s/\\(.*\\)/\\1\\\ninserted line/; }'
link|improve this answer
feedback

I usually use ed for this:

(echo 1a; echo 'Line to insert'; echo .; echo w) | ed - filename

Similar to a sed solution, but without the clutter of escaped newlines.

link|improve this answer
You could spare some echo's, especially under bash: ed - filename <<< $'1a\nLine to insert\n.\nw'. – manatwork Jan 28 at 16:56
Doesn't work in all shells, for example in dash, which on many systems is /bin/sh. – Arcege Jan 28 at 17:26
That is why I wrote “You could spare some echo's”. Sparing all echo's is indeed bash only feature, but reducing the echo count from 4 to 1 works in dash and ksh too: echo '1a\nLine to insert\n.\nw' | ed - filename. – manatwork Jan 28 at 17:37
feedback

This might work for you:

sed 1G file

If you want to insert something after the newline:

sed '1{G;s/$/something/}' file

Or if you sed handles \n:

sed '1s/$/\nsomething/' file

Of course a is even easier:

sed '1a something' file
link|improve this answer
I am sorry, what does it do? – Nikhil Mulley Jan 28 at 8:31
It inserts a newline after line 1 – potong Jan 30 at 14:20
Or rather appends a newline \n to line one! – potong Jan 30 at 14:36
feedback

i would use python for this

import fileinput

for linenum,line in enumerate(fileinput,FileInput("file",inplace=1)):
    if linenum ==1:
          print ""
          print line.rstrip() 
      else: 
          print line.rstrip()`
link|improve this answer
feedback

I just like using an explicit counter:

awk '(c==1) {print "new line"} (c!=1) {print $0} {c++}' file
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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