LinuxQuestions.org
Help answer threads with 0 replies.
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 05-03-2012, 07: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, 07:34 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

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

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
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 07:42 AM.
 
Old 05-03-2012, 07: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, 08: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, 08:07 AM   #6
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
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, 08: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, 08:39 AM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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, 09:13 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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, 12: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, 06:27 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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



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 07:05 AM
help with regular expressions mariogarcia Linux - Software 3 01-28-2009 03:23 AM
Regular Expressions ziggy25 Linux - Newbie 7 11-05-2007 06:57 AM
Regular Expressions markjuggles Programming 2 05-05-2005 11:39 AM
help with REGULAR EXPRESSIONS ner Linux - General 23 10-31-2003 11:09 PM

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

All times are GMT -5. The time now is 10:16 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