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-17-2008   #11 (permalink)
vsemaska
Linux Newbie
 
Join Date: Jun 2007
Posts: 207
What is the grep command(s) that you are using? Mine:

grep -i "\b12\b.*nov.*2007" myfile

doesn't match those lines.
vsemaska is offline   Reply With Quote
Old 05-17-2008   #12 (permalink)
eeezeepeezee
Just Joined!
 
Join Date: Apr 2008
Posts: 13
heres the code in got for the grep im actually using a few variables in place of the strings

grep -i "\b$VAR1\B.*$VAR2.*$VAR3" myfile

is it because it cant take those variables?
eeezeepeezee is offline   Reply With Quote
Old 05-17-2008   #13 (permalink)
vsemaska
Linux Newbie
 
Join Date: Jun 2007
Posts: 207
It must be how you're setting those variables. I tried it and it works:

Code:
solt:/root> ./a.bash
#!/bin/bash -vx

VAR1="12"
+ VAR1=12
VAR2="nov"
+ VAR2=nov
VAR3="2007"
+ VAR3=2007
grep -i "\b$VAR1\b.*$VAR2.*$VAR3" myfile
+ grep -i '\b12\b.*nov.*2007' myfile
123456 12 NOV 2007
123.123.123.123 12 Nov 2007 14:12:08
123.123.123.123 12 Nov 2007 14:12:08
Put a '-vx' at the end of the 1st line like I did to see what the script is doing.

I just noticed you have a \B instead of \b in your pattern

\b$VAR1\B instead of \b$VAR1\b

That might be the problem.
vsemaska is offline   Reply With Quote
Old 05-17-2008   #14 (permalink)
khafa
Linux Enthusiast
 
khafa's Avatar
 
Join Date: Apr 2008
Location: Tokyo, Japan
Posts: 681
and given the fact that the pattern is known (12 + one space + nov + one space + 2007)
i think that it can be as simple as
Code:
grep -i "12 nov 2007" myfile
__________________
Linux and me it's a love story
khafa is offline   Reply With Quote
Old 05-17-2008   #15 (permalink)
eeezeepeezee
Just Joined!
 
Join Date: Apr 2008
Posts: 13
nope, afraid none of the solutions work for me

the only way i can think of doing it is to awk that specific column then search for "12" then redirect the output to another file, and again grep the contents

But of course that would be a very long and inefficient way of solving this problem.
eeezeepeezee is offline   Reply With Quote
Old 05-17-2008   #16 (permalink)
HROAdmin26
Linux User
 
Join Date: Nov 2007
Posts: 472
Quote:
not too sure what that does but thanks anyway
Code:
man grep
Google: grep


If you are searching for a literal 12 with nothing on either side:

Code:
grep " 12 " myfile | grep -i nov | grep 2007 > results
HROAdmin26 is offline   Reply With Quote
Old 05-17-2008   #17 (permalink)
khafa
Linux Enthusiast
 
khafa's Avatar
 
Join Date: Apr 2008
Location: Tokyo, Japan
Posts: 681
Quote:
Originally Posted by eeezeepeezee View Post
nope, afraid none of the solutions work for me

the only way i can think of doing it is to awk that specific column then search for "12" then redirect the output to another file, and again grep the contents

But of course that would be a very long and inefficient way of solving this problem.
are you sure none of the solutions work ?
please tell us why these solutions dont work

i think there is a misunderstanding between you and us here. can you give us the fields in the records of the file (are all the records of the same type?)

and tell us what you want to search. because if you just need "12 nov 2007" i dont see the reason why it does not work

sorry if im missing something in your problem description
__________________
Linux and me it's a love story
khafa is offline   Reply With Quote
Old 05-17-2008   #18 (permalink)
seanmckinney
Just Joined!
 
Join Date: Feb 2005
Posts: 11
If I am up a gum tree, sorry, but I am just getting back into this, would

gawk ' {if (($2 ~/12/ ) && ($3 ~/Nov/) && ($4 ~/2007/)) print$0} ' < inputfilename > outputfilename


not work? It does for me BUT watch the case sense of the letters in nov.
If what you want to scan is the result of another progamme the couldn't you just pipe the output to the above gawk script
eg
commandxyz | gawk ' {if (($2 ~/12/ ) && ($3 ~/NOV/) && ($4 ~/2007/)) print$0} ' > outputfilename

Ummm, with just spaces between 12 and Nov and 2007

grep "12 Nov 2007"

also works for me but that could just be a fluke.
seanmckinney is offline   Reply With Quote
Old 05-17-2008   #19 (permalink)
scm
Linux Engineer
 
Join Date: Feb 2005
Posts: 977
Quote:
Originally Posted by HROAdmin26 View Post
If you are searching for a literal 12 with nothing on either side:

Code:
grep " 12 " myfile | grep -i nov | grep 2007 > results
That's not a literal 12 with nothing on either side, that's a literal 12 with a space character on each side.
scm is offline   Reply With Quote
Old 05-17-2008   #20 (permalink)
HROAdmin26
Linux User
 
Join Date: Nov 2007
Posts: 472
Quote:
Originally Posted by scm View Post
That's not a literal 12 with nothing on either side, that's a literal 12 with a space character on each side.
Golf clap for you. Does this not pull out the lines that contain " 12 ", "Nov", and "2007", as listed in the original example:

Quote:
123456 12 Nov Sun 2007
123456 14 Nov Sun 2006
123456 12 Nov Sun 2007
The 'problem' was with the 12 searches pulling other numbers such as 1234 or 34125. My comment of "nothing" on either side referred to no other number or letter. Yes, this is encoded as an ASCII space character. Besides the semantics, it changes the results not.
HROAdmin26 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

BB 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 07:47 AM.




© 2000 - 2008 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.0.0