By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,900 Members | 1,353 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,900 IT Pros & Developers. It's quick & easy.

Problem with searching files with egrep

P: 1
Hello, everybody. I have read some examples and manuals for the egrep command for Linux and I don't know if this egrep command is right for the particular files I am searching for. Here is what the question says:

Read documentation about the Unix egrep command. Then,
write an egrep command that will search all files, in the current
directory or any subdirectory (or sub-subdirectory, etc), such that
the file name has a 7 or 8 in the name; and within each such file,
print any line satisfying the following criterion: in the line there is
a word (consisting of entirely alphabetic characters, with no spaces,
quotes, hyphens or digits) that has the following three things, in
this order: (i ) some lower-case letter in the range d through k; (ii )
the letter e or the letter j; and (iii ) the word ends with ion some
examples are Evangelion, Dandelion, parhelion, triskelion.

I came up with this egrep command:
egrep '([:alpha:].[ion$])|(-v [:punc:][:space:])' "7|8" *.txt

Please tell me if this egrep command is correct or give me suggestions on how to improve it. Thank you.
Nov 2 '07 #1
Share this Question
Share on Google+
1 Reply

Expert 100+
P: 254
Well, this looks a lot like a homework problem to me, so I'm not going to "answer" your question, but I will give you some hints toward areas where I think you don't understand.

First, reread the section of the egrep man page about what [ and ] do. Then think about the part of your expression including the "[ion$]" Think about what that means. and think about the difference between:
Expand|Select|Wrap|Line Numbers
  1. ion
Expand|Select|Wrap|Line Numbers
  1. [ion]
There is a big difference here. (Also remember what "." means in a regular expression!)

Also, don't forget about parts (1) and (2). You don't seem to have covered those in your RE. And recheck what "$" means. It does NOT mean the end of a word it means the end of a line. You will need to rethink how to specify the end of the word.

You also need to rethink how to specify the part about filenames containing a "7" OR an "8" (hint: try using a different command, e.g, ls to find the right set of files) and certainly you will need to make sure you search files in sub (and sub-sub) directories. (Hint: check the "Options" section of the man page for a suitable keyword.)

Finally, I suggest working on all these parts of the problem separately and then putting them together. You have a lot of confusion packed into a single command here. I don't think you're quite ready to solve the whole problem at once. You really need to break it down into steps. Once you have done that, then you can put it back together.

Nov 2 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.