Find the answer to your Linux question:
Results 1 to 3 of 3

Thread: grep or awk

Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2004

    grep or awk

    hi Dears

    i am trying to search from following data. serarch by dept (2nd field), or search by first name or last name (3rd & 4th field). i know that awk have options that u can search by fields but it doens not fullfil my requirements. can anybody tell me how to use grep for this purpose..

    e.g. if serch is by dept and user inputs 'main' then it should display all Maintenance records. (ignore case sensitivity)

    905-508-6393 Maintenance Bloomfield, Michael
    905-508-6331 Tool Shop Brooks, Garth
    416-967-2212 Sales Brown, Junior

  2. #2
    Linux Guru
    Join Date
    Oct 2001
    Täby, Sweden
    How about this?
    egrep -i '[0-9-]* *main'
    The main problem with that file is that it doesn't just have 4 fields. If you define field as being a textual entity seperated by the space character, then the first and third lines have 4 fields, while the second line has 5 fields.

    Oh, and rest assured that if grep can do something, then awk can do it as well. The functionality set of awk is a superset of the functionality set of grep.

  3. #3
    Just Joined!
    Join Date
    Jun 2004
    London, UK

    AWK is far superior to grep, especially in your case.
    Just use basic pattern matching in awk.

    tolower($2) ~ "your_department" { print }
    Note that, using large files, awk is something like 50 times faster then grep.

    Another thing that dolda2000 already pointed out is that the structure of your data is not very easy to handle. Try using a field separator not used anywhere else in the file, perhaps ;.

  4. $spacer_open

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts