Re: Perl split() question (OT)...

2004-07-26 Thread Mathias Samuelson
Steve Bertrand wrote:
 Perl hackers -- Figured someone would have a reasonably quick, easy answer
 for this:
 
 I am trying to read through a file, line-by-line, and I want to extract
 the text in between the [ and ] characters. I would normally half the line
 by split() - ing the line first by [ as follows:
 
 if ($logLine =~ /$struct$structStart/) {
 @lineArray = split (/[/, $logLine);
 
 and then further, half again later using the ]. However, Perl does not
 like it when I search for [, as it thinks I am trying to use a regex. I
 have tried to escape the pattern, to no avail.
 
 Is there a 'special' escape for this, and more importantly, is there an
 easier way to extract data from a line of a file without having to split
 it up twice?
 
 An example of the line I'm trying to get the contents out of is this:
 
 | LRED[Conversation started on 03 Feb 21:51:11]
 
 and I need the data between [ ... ].
 
 I know it's OT, but hopefully someone can help me out.
 
 Tks!
 
 Steve

Something like this perhaps:

while() {
/.*\[(.*)\].*/;
$text = $1;
}

Rgds
Mathias
___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Perl split() question (OT)...

2004-07-23 Thread Fernando Gleiser
On Fri, 23 Jul 2004, Steve Bertrand wrote:

 Perl hackers -- Figured someone would have a reasonably quick, easy answer
 for this:

 I am trying to read through a file, line-by-line, and I want to extract
 the text in between the [ and ] characters.

This is a job for..capturing parens!!!

Try this:

if ($_=~/\[(.+)\]/) {
$var=$1;
}

$1 would be the string matched by the regex between ( and )



Fer
___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


RE: Perl split() question (OT)...

2004-07-23 Thread Carmichael, Lee
Convert the /[/ to /\[/ and see if that works. Generally brackets mean a
character class in perl regexs. You could also use single quotes. E.g.
split('[', $line).

Good luck,

Lee

 -Original Message-
 From: Steve Bertrand [mailto:[EMAIL PROTECTED]
 Sent: Friday, July 23, 2004 10:32 AM
 To: [EMAIL PROTECTED]
 Subject: Perl split() question (OT)...
 
 Perl hackers -- Figured someone would have a reasonably quick, easy
answer
 for this:
 
 I am trying to read through a file, line-by-line, and I want to
extract
 the text in between the [ and ] characters. I would normally half the
line
 by split() - ing the line first by [ as follows:
 
 if ($logLine =~ /$struct$structStart/) {
 @lineArray = split (/[/, $logLine);
 
 and then further, half again later using the ]. However, Perl does not
 like it when I search for [, as it thinks I am trying to use a regex.
I
 have tried to escape the pattern, to no avail.
 
 Is there a 'special' escape for this, and more importantly, is there
an
 easier way to extract data from a line of a file without having to
split
 it up twice?
 
 An example of the line I'm trying to get the contents out of is this:
 
 | LRED[Conversation started on 03 Feb 21:51:11]
 
 and I need the data between [ ... ].
 
 I know it's OT, but hopefully someone can help me out.
 
 Tks!
 
 Steve
 
 ___
 [EMAIL PROTECTED] mailing list
 http://lists.freebsd.org/mailman/listinfo/freebsd-questions
 To unsubscribe, send any mail to freebsd-questions-
 [EMAIL PROTECTED]
___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: Perl split() question (OT)...

2004-07-23 Thread Steve Bertrand
 On Fri, 23 Jul 2004, Steve Bertrand wrote:

 Perl hackers -- Figured someone would have a reasonably quick, easy
 answer
 for this:

 I am trying to read through a file, line-by-line, and I want to extract
 the text in between the [ and ] characters.

 This is a job for..capturing parens!!!

 Try this:

 if ($_=~/\[(.+)\]/) {
   $var=$1;
 }

 $1 would be the string matched by the regex between ( and )


Absolutely perfect!! This worked excellent:

while ($_ = LOGFILE) {
if ($_ =~ /$struct/  $_ =~ /$structStart/) {
if ($_ =~ /\[(.+)\]/) {
$string = $1;
print $string -- $struct$structStart\n;
# ... do other stuff, snipped
}
} else {

Thank-you so much!

Steve



   Fer



___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]