3

I need to add an additional blank line after the line 45 using sed

for example:

44 some text one
45 some text two
46 some text three
47 some text four

result:

44 some text one
45 some text two
46 
47 some text three
48 some text four

I've tried to use

sed '45G' myfile.txt

but not seems to be working, it does prints content of the file on the screen but do not adds any space after the line 45

Using CentOS 7 minimal

Zaza
  • 458
  • 2
  • 7
  • 15
  • This is not a code writing service. What did you try so far? Post your code! What happened when you ran it? What did you expect to happen instead? What specifically are you having problems with? https://stackoverflow.com/help/mcve – Robert Nov 11 '16 at 18:23
  • I've actually done: sed '45G' myfile.txt but then it runs a content of the full file on the screen, which I feel not really appropriate and tough may be some nicer way to do it... – Zaza Nov 11 '16 at 18:31

2 Answers2

2

You can do:

sed $'45 a \n' file.txt
  • $'' initiates C-style quoting, might be needed in some sed while using \n

  • 45 a \n appends a newline (\n) after (a) the 45-th line

heemayl
  • 39,294
  • 7
  • 70
  • 76
  • Thanks for your answer, but it is not working for me, it prints content of the file on the screen but do not add's any space after line 45 – Zaza Nov 11 '16 at 18:41
  • @eadmineadmin To edit the file in place, use `-i`: `sed -i $'45 a \n' file.txt` – heemayl Nov 11 '16 at 18:42
0

sed is for simple substitutions on individual lines, that is all. For anything else just use awk:

awk '{print} NR==45{print ""}' file

That will work with any awk on any UNIX box.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185