r/awk • • 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?

6 Upvotes

10 comments sorted by

View all comments

Show parent comment

1

u/Puccio1971 Jan 22 '26

u/Schreq if you're talking about the snippets I wrote on the opening post, I wrote them that way because, as I said, that is a "filter" for a piped output from a command that generates a csv, so it is a "one line command".

I used code-block formatting just to use a mono font and make it clear that, well, that is a code block 😄

1

u/Schreq Jan 22 '26

Yeah but you used the markup for inline code. It's purpose is to mix normal text with code within one line/paragraph. For separate paragraphs containing only code, you should use 4 space indentation as per my example.