0

Having some trouble with sed outputting what I want:

I have my file contents (idmap):

idmap config ....

idmap config ....

File being written to (testing)

[global]

[global]

I used the following sed command:-

sed '/\[global\]/ r idmap' /tmp/testing

The command works as expected and matches all [global] tags and inserts the text after all occurrences of [global]:

[global]

idmap config ....

idmap config ....

[global]

idmap config ....

idmap config ....

I try the following to match only the first occurrence of [global]

sed '0,\[global\]/ r idmap' /tmp/testing 

Which then produces:

idmap config ....

idmap config ....

idmap config ....

idmap config ....

[global]

idmap config ....

idmap config ....

[global]

Any explanation of what is going on here would be most appreciated, awk could also work for me, and how to get the desired output which would be:

[global]

idmap config ....

[global]

idmap config ....

Running on Centos 7.1 bash.

Inian
  • 80,270
  • 14
  • 142
  • 161
user985644
  • 11
  • 3

1 Answers1

0
awk 'FNR==NR{a=a"\n"$0;next} /^\[global\]/&& !f{$1=$1""a;f=1};1' idmapfile testingfile
jijinp
  • 2,592
  • 1
  • 13
  • 15