LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 01-18-2008, 02:06 PM   #1
logicalfuzz
Member
 
Registered: Aug 2005
Distribution: Arch Linux
Posts: 291

Rep: Reputation: 48
"grep" query using wilcards


Hello,

My regex understanding seems to be in a jiffy.
I have a large file(say IPfile.txt) containing IP addresses (say in the range 10.0.0.0 to 10.255.255.255) among other things. I want to see the lines containing IP addresses 10.0.16.0 to 10.0.25.255. I am shooting the following command -
Code:
grep 10\.0\.[16-25]\.[0-255] IPFile.txt
However this does not seem to show me the desired output. I see IPs outside this range too.

What is it that i am missing?
 
Old 01-18-2008, 02:34 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
The range expression only accepts a range of single characters, eg: [6-9]

"[0-255]" means match 0-2, or 5

"[6-2]" appears to match nothing, whereas "[2-6]" matches 2,3,4,5, or 6
 
Old 01-18-2008, 02:44 PM   #3
logicalfuzz
Member
 
Registered: Aug 2005
Distribution: Arch Linux
Posts: 291

Original Poster
Rep: Reputation: 48
here i have observed that when i give the range 10.0.[16-25].[0-255], the output shows me anything in the range
10.0.1.x
10.0.2.x.
10.0.1x.x
10.0.2x.x.

what could be a practical way to do this other than working around each line manually through a 27000 line file?!! Can grep be used in a manner to accept two digit ranges? if not grep what can be a better tool to use?
 
Old 01-18-2008, 04:36 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
As has been pointed out, those are CHARACTER ranges, not NUMERIC ones.

You should be able to work with a little kludge like this:
Code:
for i in `seq 16 25`; do echo "10\.0\.$i\.[0-9]{1,3}" >> range ; done
egrep -f range file

Cheers,
Tink
 
Old 01-18-2008, 04:41 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
On my machine, [16-25] only matches 1 and 5

By contrast, [21-35] matches 1,2,3,or 5 (i.e. 2, the range 1-3, or 5)

Note that when you grep using a range, it only has to match one number in the range to print the line

To look for multi-digit patterns, you need expressions like these:
[0-9][4-8] (any digit followed by 4,5,6,7, or 8)
[0-9]\{3\} (3 occurences of any digit) (Use with egrep)
[2-4]5[0-9] (2,3,or 4; then 5, then any digit)

Note also:
"." means any character
"\." means the literal "."

Last edited by pixellany; 01-18-2008 at 05:22 PM.
 
Old 01-20-2008, 10:01 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
what could be a practical way to do this other than working around each line manually through a 27000 line file?!! Can grep be used in a manner to accept two digit ranges? if not grep what can be a better tool to use?
And, did the kludge do the trick for you?



Cheers,
Tink
 
Old 01-29-2008, 04:19 AM   #7
logicalfuzz
Member
 
Registered: Aug 2005
Distribution: Arch Linux
Posts: 291

Original Poster
Rep: Reputation: 48
Hey yeah Tinkster!! thanks so much! learnt a new command in the process 'seq'..
 
Old 01-29-2008, 02:12 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by logicalfuzz View Post
Hey yeah Tinkster!! thanks so much! learnt a new command in the process 'seq'..
Cool ... always happy to spread knowledge :}

I don't believe in "Knowledge shared is power lost!" ;}


Cheers,
Tink
 
  


Reply



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
standard grep question. "The parameter list is too long" antis Linux - General 1 07-03-2007 01:43 AM
BASH: How to NOT echo to screen with "if echo $x | grep ".*"; then" eur0dad Programming 9 07-27-2006 02:14 PM
"grep" and "tcpdump" Magsol Linux - Newbie 2 03-25-2005 11:56 PM
KPPP "Query Modem" returns empty fields ananya Linux - Software 1 02-21-2005 10:39 PM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 03:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:49 AM.

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
Open Source Consulting | Domain Registration