A sed syntax
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 I read man sed and info sed and googled but can't find this kind of sed usage, can you give me a reference link on the internet or tell me which section in info sed? I knew ^ indicates the begin of a line and $ indicates end of a line, so ^\*cpp:$ indicates a line includes and only includes a string *cpp:. And, I also knew the effect of this command is append string -isystem /tools/include to the end of the next line of line *cpp: by a test. But what's meaning of the every part in {n;s, $, -isystem /tools/include,} and how this command to achieve this effect? Thanks, littlebat -- http://linuxfromscratch.org/mailman/listinfo/lfs-support FAQ: http://www.linuxfromscratch.org/lfs/faq.html Unsubscribe: See the above information page
Re: A sed syntax
On Wednesday 23 June 2010 02:24:51 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 Basically, '/^\*cpp:$/' is the address that matches lines that contain exactly '*cpp:'. The braces indicate a 'compound command'. 'n;s' means execute those two commands (n: print the pattern space; s: substitute the EOL with the 'option'). The effect is, as you know, to append the option to the end of all lines with just '*cpp:' on them. The commas are an unusual selection, but perfectly valid, since the s command allows pretty much any character to delimit the match and replace phrases. This could be rewritten as sed -e 's=^\(*cpp:\)=\1 -s system /tools/include/=' which would be a little more grokable. The only thing you need to find in the manual is the definition and use of the braces. Good enough? -- http://linuxfromscratch.org/mailman/listinfo/lfs-support FAQ: http://www.linuxfromscratch.org/lfs/faq.html Unsubscribe: See the above information page
Re: A sed syntax
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 sdelimstring to finddelimstring to subdelim 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! -- http://linuxfromscratch.org/mailman/listinfo/lfs-support FAQ: http://www.linuxfromscratch.org/lfs/faq.html Unsubscribe: See the above information page
Re: A sed syntax
On Wed, 23 Jun 2010 02:57:44 -0400 Neal Murphy neal.p.mur...@alum.wpi.edu wrote: On Wednesday 23 June 2010 02:24:51 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 Basically, '/^\*cpp:$/' is the address that matches lines that contain exactly '*cpp:'. The braces indicate a 'compound command'. 'n;s' means execute those two commands (n: print the pattern space; s: substitute the EOL with the 'option'). The effect is, as you know, to append the option to the end of all lines with just '*cpp:' on them. The commas are an unusual selection, but perfectly valid, since the s command allows pretty much any character to delimit the match and replace phrases. This could be rewritten as sed -e 's=^\(*cpp:\)=\1 -s system /tools/include/=' which would be a little more grokable. The only thing you need to find in the manual is the definition and use of the braces. Good enough? -- http://linuxfromscratch.org/mailman/listinfo/lfs-support FAQ: http://www.linuxfromscratch.org/lfs/faq.html Unsubscribe: See the above information page Thanks. Good answer. Save me much time, with your help. Always hate read the whole man page (info page) of those big program :-) Got at least three from this: 1, Sed syntax: sed -e /address/command 2, command can be a 'compound command', it is {} 3, just forgot delimiter can be any character sed -e 's=^\(*cpp:\)=\1 -s system /tools/include/=' I modified it as below but can't achieve the effect: sed -e s=^\(*cpp:\)$=\1 -isystem /tools/include= It only append -isystem /tools/include to the end of the line which only include string *cpp:. The book needs append the string to the next line of the line only include string *cpp:. But, at least I have known more on sed. Thanks. -- littlebat -- http://linuxfromscratch.org/mailman/listinfo/lfs-support FAQ: http://www.linuxfromscratch.org/lfs/faq.html Unsubscribe: See the above information page
Re: A sed syntax
On Wed, 23 Jun 2010 05:22:35 -0500 Mike McCarty mike.mcca...@sbcglobal.net wrote: So much detailed answer, I can post it into my blog as a detailed manual. too many thanks. 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 -emeans 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 sdelimstring to finddelimstring to subdelim 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! -- http://linuxfromscratch.org/mailman/listinfo/lfs-support FAQ: http://www.linuxfromscratch.org/lfs/faq.html Unsubscribe: See the above information page -- littlebat -- http://linuxfromscratch.org/mailman/listinfo/lfs-support FAQ: http://www.linuxfromscratch.org/lfs/faq.html Unsubscribe: See the above information page
Re: A sed syntax
On Wed, 23 Jun 2010 09:23:49 -0500 Mike McCarty mike.mcca...@sbcglobal.net wrote: littlebat wrote: On Wed, 23 Jun 2010 05:22:35 -0500 Mike McCarty mike.mcca...@sbcglobal.net wrote: So much detailed answer, I can post it into my blog as a detailed manual. too many thanks. It's considered polite to ask the copyright holder before reproducing his work :-) Mike Of course, I will comment the original author is Mike McCarty Mike.McCarty at sbcglobal.net and original link is http://linuxfromscratch.org/pipermail/lfs-support/2010-June/039030.html :-) -- littlebat -- http://linuxfromscratch.org/mailman/listinfo/lfs-support FAQ: http://www.linuxfromscratch.org/lfs/faq.html Unsubscribe: See the above information page