Line search in Unix

Discussion in 'Mac Programming' started by dukebound85, Sep 14, 2006.

  1. macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #1
    How would one go about searching many lines of a file for a word, or more specifically, searching for a line that doesnt contain some desired word.

    thanks
     
  2. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #2
    grep is your friend.

    man grep will give you the basic usage.

    For what you want:
    grep [word] [filename]

    finds the lines that match word

    grep -v [word] [filename]

    finds the lines that don't match word

    You can mix and match too with pipes

    e.g. grep [word1] [filename] | grep -v [word2]

    will match the lines that have word1 but not word2.

    I won't even touch regular expressions here...

    B
     
  3. thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #3
    Thanks, that is exactly what I was looking for!
     
  4. thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #4
    what about copying certain line numbers from one file and exporting to another?

    I am guessing sed may be an option

    sed -c "1,5" file > new

    this will copy lines 1-5 of file and export to new right?


    thanks for all the help so far



    EDIT: I figured it out sed -n '18,22pp' file will get me lines 18-22
     
  5. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #5
    For just getting the first or last lines in a file, head and tail are far more straightforward.

    e.g. head -5 [filename] | grep [words]

    You can play all kinds of lovely games with grep, sed and awk. So much so they wrote a nice O'Reilly book about them.

    e.g. grep can output X many lines before and Y many lines after the match with -A and -B so you can look for strings in lines adjacent to matches etc...

    B
     
  6. macrumors 65816

    stcanard

    Joined:
    Oct 19, 2003
    Location:
    Vancouver
    #6
    grep, sed, and awk are probably the 3 most underused commands in unix (in that people may use them, but rarely truly exercise what they can do).

    Although find and xargs are pretty good candidates too.

    Master those 5 commands, and it is amazing what you can do in a single line.

    I run my own mailserver, and let any address come in, so when I give emails I can make them up on the fly (company name) so I can track spam. I run this periodically on my Spam folder:

    grep -a -C15 "personal@myemail.com" Spam | grep "Subject: " | sort | uniq -c | egrep "^[ ]+1"

    To check for any personal emails that got caught. It shows me any unique subjects (so it filters the 100 vi@gra spams I have) that came to my personal address so I can quickly scan for somebody that sent me an message that got binned wrongly. Cuts out an incredible amount of chaff.
     
  7. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #7
    I only recently started using xargs (to moitor and clean up the Spam Maildirs on our mail gateway), so I know exactly what you mean!

    Is it even possible to really exercise grep, awk and sed. Or perl for that matter.

    B
     

Share This Page