Find the answer to your Linux question:
Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 23
how do you make grep search for multiple variables? for example i have a file with the contents: 123456 12 Nov Sun 2007 123456 14 Nov Sun 2006 123456 12 ...
  1. #1
    Just Joined!
    Join Date
    Apr 2008
    Posts
    13

    Multiple variables in grep command

    how do you make grep search for multiple variables?

    for example i have a file with the contents:

    123456 12 Nov Sun 2007
    123456 14 Nov Sun 2006
    123456 12 Nov Sun 2007

    say i only want to get the text which consist of text with Nov 2007

    awk '{print $2, $3}' myfile | grep "$var1 $var2"

    doesnt seem to work

    where do i go from here?

  2. #2
    Linux Guru
    Join Date
    Nov 2007
    Posts
    1,695
    You mean the lines that have Nov 2007? Q&D - grep it twice:

    Code:
    grep -i nov myfile | grep 2007 > results

  3. #3
    Just Joined!
    Join Date
    Apr 2008
    Posts
    13
    Quote Originally Posted by HROAdmin26 View Post
    You mean the lines that have Nov 2007? Q&D - grep it twice:

    Code:
    grep -i nov myfile | grep 2007 > results
    Thanks! pipeing a grep to another grep worked

    But heres another problem, say i wanted to limit my searches even more and find all text with 12 Nov 2007. But the problem with that is grep will search for all text with "12" therefore the first column 123456 will be included in the results.

    so some results should not be there but are i.e.

    grep -i nov myfile | grep 2007 | grep 12

    123456 12 NOV 2007
    123456 14 NOV 2007
    123456 15 NOV 2007

    how do i tell grep to not search the first column

  4. #4
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    you go like this
    Code:
    grep -i nov myfile | grep 2007 | grep -E "^12$"
    Linux and me it's a love story

  5. #5
    Just Joined!
    Join Date
    Apr 2008
    Posts
    13
    not too sure what that does but thanks anyway

  6. #6
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    Quote Originally Posted by eeezeepeezee View Post
    Thanks! pipeing a grep to another grep worked

    But heres another problem, say i wanted to limit my searches even more and find all text with 12 Nov 2007. But the problem with that is grep will search for all text with "12" therefore the first column 123456 will be included in the results.

    so some results should not be there but are i.e.

    grep -i nov myfile | grep 2007 | grep 12

    123456 12 NOV 2007
    123456 14 NOV 2007
    123456 15 NOV 2007

    how do i tell grep to not search the first column
    sorry for the above post. i made a mistake( the command in the above post does not work since it will match only lines that contains nothing but 12).

    for you question this is the answer
    Code:
    grep -i nov myfile | grep 2007 | grep -w 12
    -w tells grep to search the exact word "12".
    Linux and me it's a love story

  7. #7
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    You can do the search with one grep command as follows:

    Code:
    grep -i "\b12\b.*nov.*2007" myfile
    Remember that grep searches for patterns. Patterns can be described as 'regular expressions'. So that weird string "\b12\b.*nov.*2007" tells grep to search for the following:

    '\b12\b" search for '12' that is by itself.
    ".*" followed by any no. of any characters.
    "nov" followed by the string 'nov'.
    ".*" followed by any no. of any characters.
    "2007" followed by the string '2007'.

    The -i option specifies to ignore case. Refer to the grep manpage (# man grep) for details.

  8. #8
    Just Joined!
    Join Date
    Apr 2008
    Posts
    13
    im afraid thats still not working as some of the information in the files contain:

    123.123.123.123 12 Nov 2007 14:12:08

    therefore it still matches the number 12 in the first column and the fifth column which is a 24 hour time

  9. #9
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    That line is matching because of the ' 12 ' before 'Nov'.

  10. #10
    Just Joined!
    Join Date
    Apr 2008
    Posts
    13
    yes it does match that line but it also matches other lines which should not be there:

    123.123.123.123 12 Nov 2007 14:12:08
    123.123.123.123 14 Nov 2007 14:14:08
    123.123.123.123 16 Nov 2007 14:11:08
    153.173.103.153 18 Nov 2007 14:12:08

    i told grep to match me all text which contain 12 Nov 2007 as you can see some lines resulted back are 16 Nov 2007. it is because in that line of text it contains "12" in the first column. also the line 18 Nov 2007 is matched because it contains "12" in the minutes on the 24 hour time in column 5.

    Thanks for the help so far

Page 1 of 3 1 2 3 LastLast

Posting Permissions

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