-1

I am trying to match a shell variable with the second column separated by commas. So I've set $INOW to ls and the command looks like this

awk -F"," '$2 == $INOW {print "yeah it's there"}' commands.csv

I have tried putting quotes around $INOW. no difference. How do i make a match to the shell variable?

Hrishikesh
  • 335
  • 1
  • 13

2 Answers2

2

You cant use shell variables directly in awk. Rather you can create an awk variable using -v

Example

$ a_variable=hello
$ awk -v var="$a_variable" "{print var}"
hello

So in your case you can write like,

$ awk -F"," -v inow="$INOW" '$2 == inow {print "yeah its there"}' commands.csv
nu11p01n73R
  • 26,397
  • 3
  • 39
  • 52
0
awk -F"," '{if($2 == '$INOW') {print "yeah its there"}else{print "nope"}}' commands.csv
Andy
  • 49,085
  • 60
  • 166
  • 233
var
  • 37
  • 6
  • $INOW doesn't seem to be evaluated. because the else part is getting evaluated but the if part is not. $INOW is truthy. – Hrishikesh Jun 07 '16 at 15:52