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 ...
- 05-16-2008 #1Just 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?
- 05-16-2008 #2Linux 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
- 05-17-2008 #3Just Joined!
- Join Date
- Apr 2008
- Posts
- 13
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
- 05-17-2008 #4
you go like this
Code:grep -i nov myfile | grep 2007 | grep -E "^12$"
Linux and me it's a love story
- 05-17-2008 #5Just Joined!
- Join Date
- Apr 2008
- Posts
- 13
not too sure what that does but thanks anyway
- 05-17-2008 #6
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
-w tells grep to search the exact word "12".Code:grep -i nov myfile | grep 2007 | grep -w 12
Linux and me it's a love story
- 05-17-2008 #7Linux User
- Join Date
- Jun 2007
- Posts
- 318
You can do the search with one grep command as follows:
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:Code:grep -i "\b12\b.*nov.*2007" myfile
'\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.
- 05-17-2008 #8Just 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
- 05-17-2008 #9Linux User
- Join Date
- Jun 2007
- Posts
- 318
That line is matching because of the ' 12 ' before 'Nov'.
- 05-17-2008 #10Just 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


Reply With Quote
