In a directory, I have too many site.ini.append.php
(about 100 files).
They all look like:
[block1]
blabla[]
blabla[]=blabla
...
blabla[]=blabla
[block2]
blabla[]
blabla[]=blabla
...
blabla[]=blabla
[block3]
blabla[]
blabla[]=blabla
...
blabla[]=blabla
However, [block1]
has different number of entries depending on the file. It's the same thing for the other blocks.
I need to add a new line at the end of [block2]
:
[block2]
blabla[]
blabla[]=blabla
...
blabla[]=blabla
blabla[]=new_entry
And I cannot just use line number because, as I said, blocks have different entries, and [block2]
may have 20 lines in a file, and 10 in another etc...
I was thinking about somthing like this
sed -n '/^\[block2\]$/,/^$/ a blabla[]=new_entry' path/to/one_of_the_site.ini.append.php
But it doesn't work, obviously, because /^\[block2\]$/,/^$/
is not an address.
How do I do it (preferably using sed)?
UPDATE
As explained here, /^[block2]$/,/^$/ is a valid address.
However, neither a, nor i fixes the issue for me
sed -n '/^\[block2\]$/,/^$/ a blabla[]=new_entry' path/to/one_of_the_site.ini.append.php
sed -n '/^\[block2\]$/,/^$/ i blabla[]=new_entry' path/to/one_of_the_site.ini.append.php
Environment
$ sed --version
GNU sed version 4.1.5
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
to the extent permitted by law.
/^\[block2\]$/,/^$/
is a valid address. It doesn't do what you want because ita
ppends to all lines in that range. What you need here is toi
nsert before the empty line in that range. Very simple.