Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux Hosts
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Linux Programming & Scripting
Reload this Page Multiple variables in grep command
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Linux Programming & Scripting C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Reply
 
Thread Tools Display Modes
Old 05-16-2008   #1 (permalink)
eeezeepeezee
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?
eeezeepeezee is offline   Reply With Quote
Old 05-16-2008   #2 (permalink)
HROAdmin26
Linux User
 
Join Date: Nov 2007
Posts: 388
You mean the lines that have Nov 2007? Q&D - grep it twice:

Code:
grep -i nov myfile | grep 2007 > results
HROAdmin26 is offline   Reply With Quote
Old 05-17-2008   #3 (permalink)
eeezeepeezee
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
eeezeepeezee is offline   Reply With Quote
Old 05-17-2008   #4 (permalink)
khafa
Linux Enthusiast
 
khafa's Avatar
 
Join Date: Apr 2008
Location: Tokyo, Japan
Posts: 581
you go like this
Code:
grep -i nov myfile | grep 2007 | grep -E "^12$"
__________________
Linux and me it's a love story
khafa is offline   Reply With Quote
Old 05-17-2008   #5 (permalink)
eeezeepeezee
Just Joined!
 
Join Date: Apr 2008
Posts: 13
not too sure what that does but thanks anyway
eeezeepeezee is offline   Reply With Quote
Old 05-17-2008   #6 (permalink)
khafa
Linux Enthusiast
 
khafa's Avatar
 
Join Date: Apr 2008
Location: Tokyo, Japan
Posts: 581
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
khafa is offline   Reply With Quote
Old 05-17-2008   #7 (permalink)
vsemaska
Linux Newbie
 
Join Date: Jun 2007
Posts: 190
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.
vsemaska is offline   Reply With Quote
Old 05-17-2008   #8 (permalink)
eeezeepeezee
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
eeezeepeezee is offline   Reply With Quote
Old 05-17-2008   #9 (permalink)
vsemaska
Linux Newbie
 
Join Date: Jun 2007
Posts: 190
That line is matching because of the ' 12 ' before 'Nov'.
vsemaska is offline   Reply With Quote
Old 05-17-2008   #10 (permalink)
eeezeepeezee
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
eeezeepeezee is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


All times are GMT. The time now is 12:26 AM.

Powered by vBulletin 3.6.8 ©2000 - 2007, content relevant URLs by vBSEO, Property of Core Root.

Content Relevant URLs by vBSEO 3.0.0