Where condition in grep or awk?

Dear All,

I need help..

I am having a csv file.

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.

Try something like this:

var1="RKY5807"
var2="HMN55303"

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

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 ----------

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

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

Try this:

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

No its not working can you please check i am getting the blank value

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

panyam's suggestion has a small mistake in it. Try this:

awk -F ","  -v var1="AVE5530" -v var2="EFC6145"  '(var1 ~ $0) &&(var2 ~ $0) {print $3}' WiseReference.csv

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:

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