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 |
Until the gurus get here, I think you should read the manpage more carefully. Particularly the "-f" switch.
|
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? |
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?
|
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! |
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. |
Quote:
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 |
You reckon ???.
Quote:
|
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 :) |
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 |
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. |