LinuxQuestions.org
Help answer threads with 0 replies.
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 05-03-2012, 08:15 AM   #1
MikeHasLinuxQuestions
LQ Newbie
 
Registered: May 2012
Posts: 6

Rep: Reputation: Disabled
Smile Regular Expressions with egrep!


Hi Everyone!

Newbie big time here...

So I figured I would give regular expressions a shot here... and WOW is it confusing!

Ok so I'm looking for files in the /etc directory (not subdirectories) that have phone numbers in them. I'm looking for USA phone numbers so I want to use this pattern: 1-NNN-NNN-NNNN, where each N is replaced with a number. I want to extract the filenames of every file in the /etc directory which contains the pattern of numbers, one file name per line, sort it alphabetically, and using absolute references.

So this is what I have so far:

egrep -f 1-[[:digit:]]{3}-[[:digit:]]{3}-[[:digit:]]{4}' /etc | sort

I hit enter on this and it just sits there doing nothing, at least it seems that way. I have to hit CTRL-C to stop it, and continue scratching my head.

So what do the Linux gurus think?

Thank you!

Mike
 
Old 05-03-2012, 08:34 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,842

Rep: Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823
Until the gurus get here, I think you should read the manpage more carefully. Particularly the "-f" switch.
 
Old 05-03-2012, 08:38 AM   #3
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
also, make sure that your pattern is enclosed in single quotes ( ' ) and not between a single quote and something that pretends to be a single quote.

edit: yes I know this topic of typography is somewhat more complex (single quotes / acutes / feet / minutes and such) but you know what I mean by single quote, right?

Last edited by millgates; 05-03-2012 at 08:42 AM.
 
Old 05-03-2012, 08:59 AM   #4
MikeHasLinuxQuestions
LQ Newbie
 
Registered: May 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Sorry guys, that went right over my head. So I take it that I should not use "-f" switch and put more single quotes in my expression?
 
Old 05-03-2012, 09:03 AM   #5
MikeHasLinuxQuestions
LQ Newbie
 
Registered: May 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Oh and by the way, thanks guys for your time! I really do appreciate it. I'm just trying to self teach myself Linux. So far it's been pretty good. I am willing to do the work, I just need a bit of a clear-cut nudge.

Thanks again!
 
Old 05-03-2012, 09:07 AM   #6
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
1) the -f switch is there for reading patterns from file. This is clearly not what you want. Maybe you wanted -r (recursive) instead?
2) if you look at the example in your first post more carefully, you'll notice that the character before the pattern is not a single quote ( ' ). It may have been replaced somewhere during pasting the code, but it's worth checking.
 
Old 05-03-2012, 09:30 AM   #7
Rohit_4739
Member
 
Registered: Oct 2010
Distribution: Red Hat
Posts: 228

Rep: Reputation: 9
Quote:
Originally Posted by MikeHasLinuxQuestions View Post
Hi Everyone!

Newbie big time here...

So I figured I would give regular expressions a shot here... and WOW is it confusing!

Ok so I'm looking for files in the /etc directory (not subdirectories) that have phone numbers in them. I'm looking for USA phone numbers so I want to use this pattern: 1-NNN-NNN-NNNN, where each N is replaced with a number. I want to extract the filenames of every file in the /etc directory which contains the pattern of numbers, one file name per line, sort it alphabetically, and using absolute references.

So this is what I have so far:

egrep -f 1-[[:digit:]]{3}-[[:digit:]]{3}-[[:digit:]]{4}' /etc | sort

I hit enter on this and it just sits there doing nothing, at least it seems that way. I have to hit CTRL-C to stop it, and continue scratching my head.

So what do the Linux gurus think?

Thank you!

Mike
Hi Mike,

Here is what you need to use to get the desired result

Code:
egrep -R "^1-[0-9]{3}-[0-9]{3}-[0-9]{4}" /etc | sort
And this would give you filenames that have phone numbers in the pattern you specified and then would sort the result.
 
Old 05-03-2012, 09:39 AM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,842

Rep: Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823
You reckon ???.
Quote:
Originally Posted by MikeHasLinuxQuestions View Post
Ok so I'm looking for files in the /etc directory (not subdirectories)
(my emphasis)
 
Old 05-03-2012, 10:13 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
I believe syg00 has had the best suggestion so far, ie read the man page.

Currently you have been informed that -f is to read expressions from a file, which of course we do not need here.
Again syg00 points out the last suggestion -R is of no use to you as you stated you only wish to search data in a single directory and no further.

I have a few suggestions:

1. egrep is considered deprecated although it still works and the suggestion is to use the -E option with grep

2. You have expressed you would like to return the file names, so may I suggest you look at -l (that is a lower case L)

3. To perhaps speed things up a bit, you ultimately only need to find a single match before returning the file so progressing past one is of no value, so again may I suggest looking at the -m option

Let us know how you go
 
Old 05-03-2012, 01:09 PM   #10
MikeHasLinuxQuestions
LQ Newbie
 
Registered: May 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Hey Guys!

Thanks very much for all of your input, this was a great learning experience! Ok, so I went with this:

egrep -l '1-[[:digit:]]{3}-[[:digit:]]{3}-[[:digit:]]{4}' /etc | sort

And it works great!!! You guys are awesome!!!

Thanks again!

Mike
 
Old 05-03-2012, 07:27 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
Congrats
For future ref I highly recommend this book http://regex.info/.
One important thing it points out is that different tools and prog langs have slightly different regex engines ...
 
  


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
Regular Expressions nova49 Linux - Newbie 4 07-13-2011 08:05 AM
help with regular expressions mariogarcia Linux - Software 3 01-28-2009 04:23 AM
Regular Expressions ziggy25 Linux - Newbie 7 11-05-2007 07:57 AM
Regular Expressions markjuggles Programming 2 05-05-2005 12:39 PM
help with REGULAR EXPRESSIONS ner Linux - General 23 11-01-2003 12:09 AM


All times are GMT -5. The time now is 10:29 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration