Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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?
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.
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.
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.
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
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
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 ...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.