Walt Mankowski on Wed, 9 Jun 1999 14:42:12 -0400 (EDT)


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [Plug] grep, awk, sed ?


On Wed, Jun 09, 1999 at 01:54:46PM -0400, root wrote:
> 
> Walt,
> 
> Thanks a lot!  This works great, so now I'll try to figure out HOW it works!
> 
> Ben
> 
> 
> >perl -e 'undef $/;$_ = <STDIN>;print "$&\n" if /\[$1[^\]]*\]/ms;' spectrophotometer 
> <diary.txt
> 

I'll decode it for you...

perl -e

The -e option lets you enter a perl program on the command line.

undef $/

$/ is a special perl variable representing the Input Record
Separator.  The default is a new line, so normally "<STDIN>"
would read STDIN until it hits a newline.  By making $/
undefined, it will read until EOF and return the entire file as
one long, multiline string.

$_ = <STDIN>

This reads standard input and stuffs it into the special perl
variable $_.  That simplifies the regular expression that follow
a little bit.

print $&

$& is yet another special perl variable that contains the
substring that matched a regular expression.

/\[$1[^\]]*\]/ms

This is a regular expression that matches a multiline string
inside square brackets.  It's a little hard to read because
square bracket are special chars in regexes to I had to escape
some of them.

What I'm looking for is...

- a left square bracket
- the input argument ($1)
- 0 or more characters that aren't right square brackets
- a right square bracket.

The /ms at the end allows the search to span newlines.  It turns
out the m is redundant.  Only the s is needed.

Hope this helps...

Walt

_______________________________________________
Plug maillist  -  Plug@lists.nothinbut.net
http://lists.nothinbut.net/mail/listinfo/plug