Score:0

Using awk with column number in a bash script

cn flag

I'm using a command like this to get some connections from nf_conntrack:

awk '($3 == "tcp") && ($6 != "TIME_WAIT") && ($10 == "dport=1234")' /proc/net/nf_conntrack

How to use this command inside of a bash script and also add a script argument to it? If I just do:

awk '($3 == "tcp") && ($6 != "TIME_WAIT") && ($10 == "dport=$1")' /proc/net/nf_conntrack

It will suppose all $1,$3,$6,... are script arguments and fail. How do I separate awk column numbers from the script arguments to use this command properly?

Score:1
ru flag

It is not necessary, but it is the rule of thumb to explicitly declare variables you want to pass from your env to awk

awk -v myvar="dport=$1" '($3 == "tcp") && ($6 != "TIME_WAIT") && ($10 == myvar)' /proc/net/nf_conntrack
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.