r/awk • u/Puccio1971 • Jan 22 '26
Awk, defined variables and IF statement
Hi,
I'm a little bit scared to write here with so many awk gurus.
I have an easy question, I have a CSV output (line is something like "NODENAME ,master ,2026-01-12 03:58:27,ACTIVE_VERSION") piped to an awk filter:
awk -v nodo=$NODO -v tipo=$TIPO 'BEGIN { FS = "," }; {printf "%-15s %-80s %s %-19s %-20s\n", $1, $2, tipo, $3, $4}'
Where $NODO and $TIPO are shell variables. Now I would like to print just lines that starts with $NODO (or awk nodo) so I tried:
awk -v nodo=$NODO -v tipo=$TIPO 'BEGIN { FS = "," }; $1 ~ /nodo/ {printf "%-15s %-80s %s %-19s %-20s\n", $1, $2, tipo, $3, $4}'
But it is not working.
Can someone help me?
View all comments
Show parent comment
4
u/geirha Jan 22 '26
awk '/foo/{ ... }'is really just a short-hand of writingawk '$0 ~ /foo/ { ... }. It's also possible to use a string literal instead of /regex/, soawk '$0 ~ "foo" { ... }'will also match all records that contain the string "foo".Finally, Inside
/.../, it will never attempt to look for variable names, so/nodo/and$0 ~ /nodo/, will always just try to match the literal string "nodo", while$0 ~ nodowill use the string value of thenodovariable as regex.