sed - append line after block


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed - append line after block
# 1  
Old 04-19-2013
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)

Code:
[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 :

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

I get the following error :

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


Last edited by zaxxon; 04-19-2013 at 07:24 AM.. Reason: 1 more code tag for the output
# 2  
Old 04-19-2013
The code is working fine with GNU sed. What OS are you on and what type of sed are you using?
# 3  
Old 04-19-2013
@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.

Quote:
Originally Posted by andyatit
Hi, I posted in another section, but no reply yet.
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:

Code:
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:

Code:
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
Code:
/<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
# 4  
Old 04-19-2013
Quote:
@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.
@bakunin:
I know - that's why I asked the latter Smilie

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.
# 5  
Old 04-22-2013
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 :
Code:
section=$1
line=$2
 
sed -e '/\['$section'\]/ {
             :skipline
             n
             /^$/! b skipline
             i\'"$line"'
        }' wav.test


Code:
Label too long:              :skipline


most frustrating as I know you said it works, so I'm not sure what I'm doing wrong.
# 6  
Old 04-22-2013
Quote:
Originally Posted by andyatit
I'm using SunOS 5.10
not sure how to get more version info about sed than that
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?


Quote:
Originally Posted by andyatit
I put in as Bakunin said and still got a label too long error :
Code:
Label too long:              :skipline

most frustrating as I know you said it works, so I'm not sure what I'm doing wrong.
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

Code:
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
This User Gave Thanks to bakunin For This Post:
# 7  
Old 04-22-2013
Try this
Code:
sed -e '/\['$section'\]/ {
             :skip
             n
             /^$/!b skip
             i\
'"$line"'
        }' wav.test

This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command to append word at end of line

hello Team, I am looking for sed command or script which will append word at end of line. for example. I want to validate particular filesystem with mount |<filesystem name> command. if nodev parameter is not there then it should add in the fstab file with receptive to the filesystem. # mount... (8 Replies)
Discussion started by: ghpradeep
8 Replies

2. Shell Programming and Scripting

Using sed to find and append or insert on SAME line

Hi, $ cat f1 My name is Bruce and my surname is I want to use SED to find “Bruce” and then append “ Lee” to the end of the line in which “Bruce” is found Then a more tricky one…. I want to INSERT ….a string… in to a line in which I find sometihng. So example $ cat f2 My name is... (9 Replies)
Discussion started by: Imre
9 Replies

3. Shell Programming and Scripting

sed - Find a String and append a text end of the Line

Hi, I have a File, which have multiple rows. Like below 123456 Test1 FNAME JRW#$% PB MO Approver XXXXXX. YYYY 123457 Test2 FNAME JRW#$% PB MO Super XXXXXX. YYYY 123458 Test3 FNAME JRW#$% PB MO Approver XXXXXX. YYYY I want to search a line which contains PB MO Approver and append... (2 Replies)
Discussion started by: java2006
2 Replies

4. Shell Programming and Scripting

sed to append on specific line in password file

I have the a group file and my ftp group line looks like this ... (3 Replies)
Discussion started by: slufoot80
3 Replies

5. Shell Programming and Scripting

sed append without using new line

im trying to append to the end of the line using sed but I want to do it without creating a new line the text to which I want to append is all in capital letters. I want to do something like this: LINE]Foo but when I do this: //a\ ] Foo it prints foo on a new line: LINE ]Foo ... (11 Replies)
Discussion started by: mrjavoman
11 Replies

6. Shell Programming and Scripting

How to append line with sed?

Input: gstreamer-plugins-good gstreamer-plugins-bad gstreamer-plugins-ugly Output should be: gstreamer-plugins-good gstreamer-plugins-bad gstreamer-plugins-ugly How can it be done with sed? (5 Replies)
Discussion started by: cola
5 Replies

7. UNIX for Dummies Questions & Answers

sed - append text to every line

Hi all I tried this on an old version of sed on NCR Unix MP-RAS: sed -e "s/$/nnn/" file1 >file2 This file (file1): the cat sat on the mat. the cat sat on the mat. the cat sat on the mat. becomes this (file2): the cat sat on the mat.nnn the cat sat on the mat.nnn nnn the... (3 Replies)
Discussion started by: jgrogan
3 Replies

8. Shell Programming and Scripting

Sed : identify a pattern and append a word at the end of a line

Hello to all, On aix, I want to identify a term on a line in a file and then add a word at the end of the line identified. I do not want the word to be added when the line contains the symbol "#". I use the following command, but it deletes the term identified then adds the word. #sed... (4 Replies)
Discussion started by: dantares
4 Replies

9. UNIX for Dummies Questions & Answers

using sed to append text to the end of each line

Anyone know how to use SED to append a comma to the end of each line example: field1,field2,field3,field4 If i Cat /textfile ---- How can i append the end of /textfile with a comman? (8 Replies)
Discussion started by: Redg
8 Replies

10. Shell Programming and Scripting

Using SED to append character to each line

Hey - my first post here, and I'm a total SED newb. I've looked around for previous help on this, but have so far been unsuccessful. I have a program (AMStracker for OS X) that outputs data in the terminal. Output is in this form: . . . 3 0 -75 3 0 -76 3 0 -77 ... (4 Replies)
Discussion started by: c0nn0r
4 Replies
Login or Register to Ask a Question