Wednesday, December 22, 2010

use variable refeences in awk

I need to compare 1st column of user.csv with value, it works well with following

value=2
awk -F':' '$1==$value {print}' user.csv

now I have a case where column number is also a variable for me, in that case, the solution would be:

value=2
column=1
awk -F':' ''\$$column==$value' {print}' filename

1. You need to put backslash (\) before first $
2. You need to put comparison in single quote