Where condition in grep or awk? | Unix Linux Forums | Shell Programming and Scripting

  Unix/Linux Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Where condition in grep or awk?

Shell Programming and Scripting


Tags
where clause awk

Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-15-2013
yadavricky yadavricky is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 26 March 2015, 1:11 PM EDT
Posts: 46
Thanks: 21
Thanked 0 Times in 0 Posts
Where condition in grep or awk?

Dear All,

I need help..

I am having a csv file.


Code:
Home_TITLE,People_TITLE,Repo_ALIAS
HMN5530,RKY5807,/mine_repo/rike001
HMN5530,SRY6443,/mine_repo/rike001
HMN5530,ARDY001,/mine_repo/rike001

If i have two value in varible RKY5807, HMN5530. how can fetch and store another value Repo_ALIAS (/mine_repo/rike001) in variable. its very much similar to put where clause but i have no idea

Thank you for your help.

Moderator's Comments:
Use code tags, thanks.

Last edited by zaxxon; 02-15-2013 at 08:07 AM.. Reason: code tags, see PM
Sponsored Links
    #2  
Old Unix and Linux 02-15-2013
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 26 March 2015, 5:58 AM EDT
Location: The Netherlands
Posts: 7,730
Thanks: 128
Thanked 544 Times in 512 Posts
Try something like this:

Code:
var1="RKY5807"
var2="HMN55303"

new_var=$(awk -F"," -v v1="$var1" -v v2="$var2" '$1==v1 && $2==v2{print $3}')

The Following User Says Thank You to Franklin52 For This Useful Post:
yadavricky (02-15-2013)
Sponsored Links
    #3  
Old Unix and Linux 02-15-2013
yadavricky yadavricky is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 26 March 2015, 1:11 PM EDT
Posts: 46
Thanks: 21
Thanked 0 Times in 0 Posts
RedHat Please suggest something else

Its not working Please let me know if you have some other option.

---------- Post updated at 10:56 AM ---------- Previous update was at 10:47 AM ----------


Code:
awk -F "," '/AVE5530/&&/EFC6145/ {print $3}' WiseReference.csv

If i give value directly then it is working fine

Code:
awk -F "," '/$var1/&&/$var2/ {print $3}' WiseReference.csv

it is not giving any output

please help

Last edited by Franklin52; 02-18-2013 at 02:33 AM.. Reason: Please use code tags for data and code samples
    #4  
Old Unix and Linux 02-15-2013
panyam panyam is offline Forum Advisor  
Registered User
 
Join Date: Sep 2008
Last Activity: 8 December 2014, 6:40 AM EST
Posts: 1,159
Thanks: 20
Thanked 105 Times in 100 Posts
Try this:



Code:
awk -F ","  -v var1="AVE5530" -v var2="EFC6145"  '/var1/&&/var2/ {print $3}' WiseReference.csv

Sponsored Links
    #5  
Old Unix and Linux 02-15-2013
yadavricky yadavricky is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 26 March 2015, 1:11 PM EDT
Posts: 46
Thanks: 21
Thanked 0 Times in 0 Posts
RedHat still not working

No its not working can you please check i am getting the blank value
Sponsored Links
    #6  
Old Unix and Linux 02-15-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 26 March 2015, 6:31 PM EDT
Location: Saskatchewan
Posts: 20,385
Thanks: 873
Thanked 3,574 Times in 3,339 Posts
Quote:
Originally Posted by yadavricky View Post
If i give value directly then it is working fine

awk -F "," '/$var1/&&/$var2/ {print $3}' WiseReference.csv

it is not giving any output

please help
Shell variables do not expand in single quotes, never have.

panyam's suggestion has a small mistake in it. Try this:
Code:
awk -F ","  -v var1="AVE5530" -v var2="EFC6145"  '(var1 ~ $0) &&(var2 ~ $0) {print $3}' WiseReference.csv

Sponsored Links
    #7  
Old Unix and Linux 02-15-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 25 March 2015, 7:07 PM EDT
Location: Galactic Empire
Posts: 3,401
Thanks: 240
Thanked 1,215 Times in 1,141 Posts
Quote:
Originally Posted by yadavricky View Post
No its not working can you please check i am getting the blank value
Note that this is an AND && condition, so both condition should satisfy. Make sure you have these values in 1st and 2nd field.

Try this:

Code:
awk -F, -v var1="HMN5530" -v var2="RKY5807"  '$1==var1&&$2==var2{print $3}' WiseReference.csv

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
grep not equal to condition prash358 Shell Programming and Scripting 6 08-22-2012 05:26 PM
HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ? shell_boy23 Shell Programming and Scripting 6 07-10-2012 05:20 AM
grep in the if condition boopal Shell Programming and Scripting 8 12-09-2011 11:17 AM
use awk pick value from lines as condition for grep jackoverflow Shell Programming and Scripting 2 06-24-2010 06:37 PM
grep command with AND condition prasperl Shell Programming and Scripting 6 01-26-2010 03:54 AM



All times are GMT -4. The time now is 09:58 PM.