LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-27-2010, 02:24 PM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
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
 
Old 02-27-2010, 02:38 PM   #2
BeacoN
Member
 
Registered: Nov 2008
Distribution: Linux Mint
Posts: 56

Rep: Reputation: 17
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.
 
Old 02-28-2010, 08:51 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
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])/'
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Specific range of records dina3e Linux - Newbie 6 03-01-2010 11:43 AM
need help with regex in perl umbrella2 Programming 5 01-15-2009 10:13 AM
Regex to exclude a specific phrase grob115 General 5 11-19-2008 01:10 PM
Perl regex $ ShaqDiesel Programming 6 08-18-2006 03:40 PM
regex Perl help igotlongestname Programming 2 09-14-2005 08:51 PM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration