I am currently having problems with awk usage of shell variables. Here’s my problem,
when I run the following:
awk -F: ‘$1==”marco” {print $7}’ /etc/passwd
everything works fine and dandy. However, I want to be able to pass my $user shell variable instead of “marco”, so I try the following (after reading about -v flag):
awk -F: -v awkvar=$user ‘$1==”$awkvar” {print $7}’ /etc/passwd
and it doesn’t seem to work. Can anyone help me?
Thanks!
Use quotation marks on the shell side, and don’t use
$for awk variables.In
awk,$indicates that it’s a positional parameter (i.e. a field number), so as it was, it was looking for field “marco”, which reduces to$0, the whole line. So it would find any line with only one field.