LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   expressing a specific range with regex and perl extractors. (https://www.linuxquestions.org/questions/linux-newbie-8/expressing-a-specific-range-with-regex-and-perl-extractors-791999/)

casperdaghost 02-27-2010 01:24 PM

expressing a specific range with regex and perl extractors.
 
I am tryng to get values BBA 204- BBA 324 from a log file.

this is my script:
zmore log.gz | perl -nle 'print for m/BBA\s[2|3][0-9][0-4]/g' | sort -u | perl -ne 'print if /^BBA\s204$/../^BBA\s324$/'

the first perl one liner gives me everything from BBA 200 to BBA 394, and the second perl one liner cuts it down to BBA 204 to BBA 324

Is there a elegant regex that will allow me to skip the second perl one liner and just take out the BBA 204 - BBA 324

BeacoN 02-27-2010 01:38 PM

Hey Casper,

Zmore, huh. Learned a new command-kewl.

Can't you get this done with BaSH alone? Not really sure what you're trying to do with the number range but egrep handles regexes.

zmore myfile.gz | egrep -o "BBA [2|3][0-9][0-4]"

the -o flag says to show only matching strings

you can even tack a | sort on the end to put them in order ( I think this is what you were trying to do :-)

The regex [2|3][0-9][0-4] will only match the first 5 numbers of every ten, since the last [0-4] will only match 0,1,2,3,4. To get the range from 200-394 would be a bit more intricate.

chrism01 02-28-2010 07:51 PM

You can use grouping parentheses to try alternate non-overlapping matches
Code:

perl -nle 'print /(20[4-9])|(2[1-9][0-9])|(3[0-2][0-4])/'


All times are GMT -5. The time now is 07:09 PM.