Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

multiline sed commands

Am trying to understand this. A sed book (Oreilly UNIX sed & awk 2Ed) says commands can be grouped at the same address using the format

address{
command1
command2
command3
}


I am trying a substitution /XA/xx/ in lines 7 to 100 only if they contain the character "-"

sed 7,100{
s/-/XA/xx/
}


Am I supposed to run this as a sed script in an external file only cos when I type
sed 7,100{
and type return
I get this error :
sed: 1: "7,100{": unexpected EOF (pending }'s)


What is wrong here?

PowerMac G4, iBook G4, MacBook Pro, iMac, Mac Pro, Mac OS X (10.5.7)

Posted on Jul 29, 2009 6:36 PM

Reply
6 replies

Jul 29, 2009 8:04 PM in response to dj9027


sed 7,100{
s/-/XA/xx/
}

I see several problems

1st, I think your substitution syntax is wrong. I think it should look like

/-/s/XA/xx/

Notice that first you have /-/ which selects the lines with a '-' and then the s/XA/xx/ that does substitution on the line.

2nd, if you want a shell command line to span multiple lines, then you need to tell the shell that command is not finished when you hit RETURN.

There are several ways to do this, but the easiest is to just enclose the sed arguments in quotes.

sed '7,100{
/-/s/XA/xx/
}' input.file

Double quotes "..." could be used, but you must be careful as the shell will perform $var substitutions and may do unplesant things to so single quotes '...' are preferred, unless you need to include a single quote in your sed commands.

You could also just put the commands in 1 line

sed '7,100{/-/s/XA/xx/}' input.file

multiline sed commands

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.