Hi,
I've a problem with wariables in awk.
My example:
$ cat test.txt
12|aaaa
13|bbbb
012|cccc
0123|dddd
$ cat test.awk
$1 == var {print $0}
$ cat test.sh
/usr/xpg4/bin/awk -F"|" -v var="012" -f test.awk test.txt
$ ./test.sh
12|aaaa
012|cccc
The correct output would be only "012|cccc", it looks a type problem (12 and 012 are the same number)
If I use a constant with " " in awk, it works good: $1 ==
"012" { print $0}
But I need use a variable, and if I write $1 == "var" { print $0} it doesn't work.
Someone can help me?
Thanks in advance,
Antonio.
Your pattern should be
^012$
to get what you want, and your program action should be
$1 ~ var {print $0}
Sorry but it doesn't work me. My problem is how put these pattern using a variable. If I put a constant I don't have any problem.
Did you try changing your prog action into this?
$1 ~ var {print $0}
TEST>var1="012"
TEST>awk -F"|" -v var="^"$var1"$" '$1 ~ var {print $0}' file.txt
012|cccc
$1 ~ var {print $0}
I bet you don't like to have to say -v var="^012$" and can't accept that -v var="." does match any line instead of the lines having exactly "." in the first field.
Then the following trick may work:
"z"$1 == "z"var {print $0}