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

  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 Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-15-2013
yadavricky yadavricky is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 17 June 2013, 7:44 AM EDT
Posts: 40
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 02-15-2013
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 31 July 2014, 9:09 AM EDT
Location: The Netherlands
Posts: 7,692
Thanks: 120
Thanked 531 Times in 502 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 02-15-2013
yadavricky yadavricky is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 17 June 2013, 7:44 AM EDT
Posts: 40
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 02-15-2013
panyam panyam is offline Forum Advisor  
Registered User
 
Join Date: Sep 2008
Last Activity: 24 July 2014, 3:48 AM EDT
Posts: 1,156
Thanks: 20
Thanked 104 Times in 99 Posts
Try this:



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

Sponsored Links
    #5  
Old 02-15-2013
yadavricky yadavricky is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 17 June 2013, 7:44 AM EDT
Posts: 40
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 02-15-2013
Corona688 Corona688 is online now Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 31 July 2014, 1:33 PM EDT
Location: Saskatchewan
Posts: 19,023
Thanks: 755
Thanked 3,158 Times in 2,962 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 02-15-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 31 July 2014, 1:25 PM EDT
Location: Galactic Empire
Posts: 3,358
Thanks: 230
Thanked 1,191 Times in 1,124 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 Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 01:35 PM.