Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

10 Answers 10

up vote 18 down vote accepted
awk 'NR==1{print; print "new line"} NR!=1'
share|improve this answer
12  
A little shorter: awk 'NR==2 {print "new line"} 1' –  glenn jackman Jan 23 '12 at 17:31
    
@glennjackman: your version doesn't work on my system, but the answer does. Specifically, with yours, line 2 is duplicated and the new line is inserted between both (so as line 3). –  Nikana Reklawyks Feb 21 at 0:36

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.

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

Explanation.

share|improve this answer

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

share|improve this answer

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

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

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.

share|improve this answer
1  
You could spare some echo's, especially under bash: ed - filename <<< $'1a\nLine to insert\n.\nw'. –  manatwork Jan 28 '12 at 16:56
    
Doesn't work in all shells, for example in dash, which on many systems is /bin/sh. –  Arcege Jan 28 '12 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 '12 at 17:37

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
share|improve this answer
    
I am sorry, what does it do? –  Nikhil Mulley Jan 28 '12 at 8:31
    
It inserts a newline after line 1 –  potong Jan 30 '12 at 14:20
    
Or rather appends a newline \n to line one! –  potong Jan 30 '12 at 14:36

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()`
share|improve this answer

I just like using an explicit counter:

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

Python Solution

python -c "import sys; lines = sys.stdin.readlines(); lines.insert(1,'New Line\n'); print ''.join(lines).strip()" < input.txt
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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