Conditional AND in awk with environment variables

1

I found this command. If a line has interested word, the command will output the entire line:

temp="sample"
awk -F " " -v var="$temp" '$1 == var' /root/smaple.txt

smaple.txt contains:

sample demo 123 456
sample2 demo2 567 345
sample3 demo4 453 456

Now I want to check two values i.e. If those two values in a line, the command will output the line:

temp1="sample"
temp2="123"
awk -F " " -v var="$temp1" var2="$temp2" '$1 == temp1 && $3 == temp2' /root/smaple.txt

The expected output for my command:

sample demo 123 456

But its not working.

Veerendra

Posted 2015-10-06T12:50:37.983

Reputation: 261

Answers

1

You confused the names of the variables:

temp1="sample"
temp2="123"
awk -v temp1="$temp1" -v temp2="$temp2" '$1==temp1 && $3==temp2' file

Also the -v parameter has to be used multiple times, when you need multiple variables.

chaos

Posted 2015-10-06T12:50:37.983

Reputation: 3 704

0

Try this once

awk -F " " -v 'var="$temp1" var2="$temp2"' '$1 == var && $3 == var2' /root/smaple.txt

you were comparing with the value

Ali786

Posted 2015-10-06T12:50:37.983

Reputation: 690

Thanks for the replay, I tried with that also. It is not show any output or error – Veerendra – 2015-10-06T13:13:45.310