Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing Question

Status
Not open for further replies.

JRyanCon

Technical User
Aug 19, 2004
47
US
I want to parse information out of a linux command but am having a bit of trouble getting it to do what I want. The values I want to gather can be any digits caps or lowercase and can end in (rev [a-z0-9]) or not. If they end in this value I want to gather everything up to but not including the (rev 01). But if they dont end in this value I want to gather the full line. for instance if I had:

===========

blah blah 1101 blah (rev 03)
another value here blah

===========

I would want to capture:

blah blah 1101 blah
another value here blah


Any tips or ideas on how to do this?
 
Something like this (untested)?:

Code:
my @values = ( "blah blah 1101 blah (rev 03)", "another value here blah" );

my @captures = map /^([\w\s]+)(?:\(rev [a-z0-9]+\))?$/i, @values;
Your description isn't totally clear on what characters are allowed before the (rev 03) so I've assumed that word or space characters are acceptable and no others.
 
Wouldn't this work? (Please correct my code if necessary)
Code:
my $line = "blah blah 1101 blah (rev 03)";
my $output = $line ~= m/(rev [a-z0-9]+))$//;

--Chessbot

"Violence is the last refuge of the incompetent." -- Asimov, Foundation
 
The following code will print all output lines removing the end of those lines where there is a rev code as you specified:

Code:
open(SC, "linx_command |");
while (<SC>) {
 chomp;
 s/\s+\(\s*[rR][eE][vV]\s*[a-zA-Z0-9]+\s*\)\s*$/;
 print "$_\n";
}
close(SC);


Michael Libeson
 
Hmmm.. here's kind of an ugly looking match regex that should work if you don't want to modify the original output from the command.

Code:
m/(.+?)(\(rev \w+\))?$/i;
print "$1\n";

If you're not worried about modifying the output from the command, a substitution will probably work. Give this a shot (and it's a lot cleaner.)

Code:
s/\(rev \w+\)//i;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top