sed - append line after block

Hi, I posted in another section, but no reply yet.

I have an ini file with sections denoted as follows (for example)

[SECTION_1]
blah=blah
blee=blee
bloo=bloo
 
[SECTION_2]
blur=blur
blaa=blaa

I have ksh script that needs to append a line ${line} to the end of section ${section}

I saw this from another forum but I get an error :

section="SECTION_1"
line="blip = blip"
sed -e '/\['$section'\]/{:a;n;/^$/!ba;i\'"$line"'' -e '}' testFile

I get the following error :

Label too long: /\[SECTION\]/{:a;n;/^$/!ba;i\blur = blur

The code is working fine with GNU sed. What OS are you on and what type of sed are you using?

@zaxxon: The problem with GNU-sed is that in some regards it works in outright contradiction to the POSIX-standard which most other sed-implementations adhere to.

If you get no answer you might think about rephrasing your question. zaxxon already pointed out with his question that a vital piece of information was missing. If you think you posted in the wrong section erroneously and another board would be better suited please contact a moderator. We can move the thread for you and we really enjoy doing so, because this activity legitimate us having the right to boss around users. ;-))

Now to your problem: i think the problem is you didn't use a ";" to denote separate commands here:

sed -e '/\['$section'\]/{;:a[...etc...]

but, honestly: this is a horrible way to write sed-scripts, especially long ones. You gain nothing from saving a few bytes of white space but lose the opportunity to read what you have written. In the interest of maintainability you might consider rewriting the script this way:

section="SECTION_1"
line="blip = blip"

sed -e '/\['$section'\]/ {
             :skipline
             n
             /^$/! b skipline
             i\'"$line"'
        }' testFile

By the way: some sed-implementations don't like

/<regexp>/! <command>

and prefer a blank between "/" and "!". I remember writing a script with GNU-sed once which failed under AIX because of exactly this problem.

I hope this helps.

bakunin

@bakunin:
I know - that's why I asked the latter :smiley:

Like bakunin wrote it as non-one-liner, sed on AIX for example needs to have nested parts with curled brackets on their own line.

That might be your problem here too.

Hi, sorry, didn't realise I'd not worded it right, but thanks I'll bear it mind next time.

I'm using SunOS 5.10
not sure how to get more version info about sed than that

I put in as Bakunin said and still got a label too long error :

section=$1
line=$2
 
sed -e '/\['$section'\]/ {
             :skipline
             n
             /^$/! b skipline
             i\'"$line"'
        }' wav.test
Label too long:              :skipline

most frustrating as I know you said it works, so I'm not sure what I'm doing wrong.

That is OK, "SunOS 5.10" already puts it in perspective. Note that with SunOS you need to use the tools in "/usr/xpg4/bin/" rather than in "/usr/bin". What is your "PATH" variable looking like?

Well, it worked on my systems - AIX and Fedora Linux - but all systems are slightly different. As it is i have no Solaris system at hand to test.

Try to shorten the indentation and see if this helps: move the content of the line ":skipline" to the leftmost position, clearing out the leading blanks. Second, try to change "skipline" to something shorter. My sed versions have no problem with labels 8 characters long, but maybe yours does. Change the line

b skipline

too, of course. If this helps you have nailed down the reason, otherwise we will have to look for alternative explanations.

I hope this helps.

bakunin

1 Like

Try this

sed -e '/\['$section'\]/ {
             :skip
             n
             /^$/!b skip
             i\
'"$line"'
        }' wav.test
1 Like

Fantastic, thank you both. Reducing the label and splitting the '"$line"' worked great thanks. A great help, much appreciated.