LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Regular Expressions with egrep! (https://www.linuxquestions.org/questions/linux-newbie-8/regular-expressions-with-egrep-943083/)

MikeHasLinuxQuestions 05-03-2012 07:15 AM

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

syg00 05-03-2012 07:34 AM

Until the gurus get here, I think you should read the manpage more carefully. Particularly the "-f" switch.

millgates 05-03-2012 07:38 AM

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?

MikeHasLinuxQuestions 05-03-2012 07:59 AM

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?

MikeHasLinuxQuestions 05-03-2012 08:03 AM

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!

millgates 05-03-2012 08:07 AM

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.

Rohit_4739 05-03-2012 08:30 AM

Quote:

Originally Posted by MikeHasLinuxQuestions (Post 4669340)
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.

syg00 05-03-2012 08:39 AM

You reckon ???.
Quote:

Originally Posted by MikeHasLinuxQuestions (Post 4669340)
Ok so I'm looking for files in the /etc directory (not subdirectories)

(my emphasis)

grail 05-03-2012 09:13 AM

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 :)

MikeHasLinuxQuestions 05-03-2012 12:09 PM

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

chrism01 05-03-2012 06:27 PM

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 ...


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