#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

    Grep search Help


    Lets say I had a text file called "file" and it looked like this

    1234567|HI|My name is Bob|Dave
    8911123|NI|My name is Dave|Bob

    And I wanted to search the file in the third bracket position (where it says "My name is Bob" and "My name is Dave") and I wanted it to search for "Bob". So only the "Bob" from the first line would show up, not the "Bob" from the second line.

    How would I do that?
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,995
    Rep Power
    9397
    Actually awk would be easier.
    Code:
    $ awk -F '|' '$3 ~ /Bob/' file
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0
    Originally Posted by requinix
    Actually awk would be easier.
    Code:
    $ awk -F '|' '$3 ~ /Bob/' file
    Thanks worked perfectly!

    If I were to add another search to this search and I wanted to search for the word "Alf" but it was in the 5th position (after the 5th '|') How would I add that onto it?

    Like this...?

    Code:
    awk -F '|' '$3 ~ /Bob/ $5 ~/Alf/' file
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0
    Also, lets say I have a file listing numbers on different lines from 1 - 100.

    For example...

    1
    2
    3
    4
    5
    ...
    100

    How would I search 56-95 (inclusive)?
  8. #5
  9. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,995
    Rep Power
    9397
    Originally Posted by johnhisenburg
    If I were to add another search to this search and I wanted to search for the word "Alf" but it was in the 5th position (after the 5th '|') How would I add that onto it?

    Like this...?

    Code:
    awk -F '|' '$3 ~ /Bob/ $5 ~/Alf/' file
    Check the documentation for the awk program. There's a very simple way of and-ing two conditions together. Or cheat and look below.

    Originally Posted by johnhisenburg
    Also, lets say I have a file listing numbers on different lines from 1 - 100.

    For example...

    1
    2
    3
    4
    5
    ...
    100

    How would I search 56-95 (inclusive)?
    Any program supporting scripting would work fine (awk, Perl, PHP, bash...). Continuing with awk,
    Code:
    $ awk '$0 >= 56 && $0 <= 95' file
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0
    Originally Posted by requinix
    Check the documentation for the awk program. There's a very simple way of and-ing two conditions together. Or cheat and look below.


    Any program supporting scripting would work fine (awk, Perl, PHP, bash...). Continuing with awk,
    Code:
    $ awk '$0 >= 56 && $0 <= 95' file
    Got it! I just had to use the && symbol for and-ing the two conditions. Thanks so much for your help.

IMN logo majestic logo threadwatch logo seochat tools logo