Problem with searching files with egrep

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SSJVEGETA
    New Member
    • Nov 2007
    • 1

    #1

    Problem with searching files with egrep

    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.
  • prn
    Recognized Expert Contributor
    • Apr 2007
    • 254

    #2
    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:
    Code:
    ion
    and
    Code:
    [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.

    HTH,
    Paul

    Comment

    Working...