From: MJ Ray Sent: 03 February 2005 18:10 Brett wrote:
It should be relatively simple... parse the file for the function keyword at the beginning of a line, then select everything up to the first occurrence of */, should be relatively simple.
Let's use sed, the stream editor!
sed -n -e '/^function /,/^ **//{;/^function /d;p;}' sourcefile.js
Very neat (and original) solution, Mark and it almost works except it doesn't select the function statement as I needed but that simplifies things a bit :o)
But before I say how I fixed that, let me try and feed my understanding back to you to make sure I understand this properly;
apart from the actual sed command and the options, we have:-
'/^function /,/^ **//{;/^function /d;p;}'
the single quotes (or apostrophes or whatever you want to call them) i.e. the [ ' ] characters simply delimit the argument string for the -e option, which leaves,
/^function /,/^ **//{;/^function /d;p;}
and breaking this down as you describes gives,
/^function /
then a comma separator, then
/^ **//
followed by
{;/^function /d;p;}
ok, so for /^function /
the oblique strokes are just delimiters and the caret says match at the beginning of the line, which leaves the string to be matched, which is 'function ' (is the trailing space deliberate? to delimit a complete word?)
now for /^ **//
again the oblique strokes are just delimiters and the caret says match at the beginning of the line, which leaves,
**/
which is used to look for */,
complicated because the backslash escape characters are needed to specify that the asterisk and oblique stroke are actually characters to be matched rather than special characters. I presume the first asterisk is so that the */ string can be preceded by any extraneous spaces or tabs at the start of the line?
Now for the interesting bit :o)
{;/^function /d;p;}
I think the semi-colons delimit the sed commands, is that correct? if so are the leading and trailing ones needed?
so we have;
/^function /d
and
p
according to the manual d deletes the pattern space and starts the next cycle, but here you're matching a line starting with the word - function - so only that line is deleted (which is probably why the function definition isn't being output),
and the p command just tells sed to print out the pattern space?
So to get sed to do what I want I just need;
sed -n -e '/^function /,/^ **//p' sourcefile.js
Which works a treat!!! :D
Once again, many thanks for the elegant solution Mark - rispek init :o).
Sorry to everyone else for a long winded reply but I'm not that well up on either sed or regular expressions so I thought picking it apart would ensure that I understood it correctly and that anyone else with the same level of knowledge as me could see what was going on.
Regards,
Keith ____________ Life is painful, suffering is optional. - Sylvia Boorstein
Keith wrote:
But before I say how I fixed that, let me try and feed my understanding back to you to make sure I understand this properly;
Yep, I think you've broken it down excellently. Glad it works for you.