A sed syntax

Mike McCarty Mike.McCarty at sbcglobal.net
Wed Jun 23 03:22:35 PDT 2010


littlebat wrote:
> Hi,
> I am learning LFS BOOK:
> http://www.linuxfromscratch.org/lfs/view/6.6/chapter05/adjusting.html
> 
> Below is a sed syntax I can't understand and haven't found a place to
> learn it. 
> <code>
> sed -e "/^\*cpp:$/{n;s,$, -isystem /tools/include,}"
> </code>

You already got a good answer, perhaps a little more detail helps...
I'm no sed expert, but this is the way I read that command.

sed	the command

-e	means "execute this little program which follows"

  "	the quotes are necessary to keep the shell from
	trying to do stuff with what's here, and to make
	what follows "all one argument" to the program

/	sed looks at the first character, and takes that
	to be the "delimeter". So, everything from here to
	the next "/" is the "address" sed will use to select
	lines from the file; the program gets executed on lines
	which match this pattern, all other lines pass through
	unchanged

^	this indicates that the pattern must start at
	the beginning of the line

\*	we have to "escape" the "*", or the shell will try to
	put file names in there, hence the "\" to make this
	a literal "*"

cpp:	more string to look for

$	this says that when we've matched what went before,
	we must next find end of line, so, the entire line
	must be "*cpp:", so the command gets executed only
	on lines which contain "*cpp:" and nothing else

/	here's the other delimeter "/" which ends the "address"

{	this tells sed that what is contained is the script to
	execute, when we find a matching line; we do so up to
	the closing "}"

n	Read/append the next line of input into the pattern space
	IOW, print what has been matched so far ("*cpp:") and
	then work on the next line

;	end of "n" command, so all we print is just "*cpp:"
	we use ";" to put multiple commands together, so this
	separates the "n" command from the "s" command

s	now we start a "substitute" command

,	this is taken by sed to be the delimter of the string
	to substitute for; this could be any character, like
	the "/" above; the "s" command wants

	s<delim><string to find><delim><string to sub><delim>

	where <delim> may be any character you like, but all three
	must be the same. In this case, ","

$	the pattern we are going to substitute for is end of line...

,	... and nothing else, the second "," matches the one above
	and ends the search string

  -isystem /tools/include
	this is the string to substitute at end of line

,	here's the third delimeter

}	this marks end-of-command

"	this is the matching quote for the shell to see

HTH

Mike
-- 
p="p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
Oppose globalization and One World Governments like the UN.
This message made from 100% recycled bits.
You have found the bank of Larn.
I speak only for myself, and I am unanimous in that!



More information about the lfs-support mailing list