I use the following awk line in order to capture the remote machine that using the port 2181

netstat -nape | awk '{if ($4 == "") print $4, $5 ,$6 ,$9;}' ESTABLISHED 9027/java

but when I use parameter - a that represented the ip address value in the awk seems that a parameter not get the real ip address

 netstat -nape | awk -v a=""  '{if ($4 == a:2181) print $4, $5 ,$6 ,$9;}'

no output

when I am wrong here ?

  • 2
    Are you sure it's the correct command? $4 == a:2181 will be syntax error. You want to use $4 == a":2181" – cuonglm Jan 17 at 15:06
  • what I mean is that we want a contain the ip = – yael Jan 17 at 15:07

Your command should have given you a syntax error.

With GNU awk:

awk: cmd. line:1: {if ($4 == a:2181) print $4, $5 ,$6 ,$9;}
awk: cmd. line:1:             ^ syntax error

With BSD awk:

awk: syntax error at source line 1
 context is
        {if ($4 == >>>  a: <<<
awk: illegal statement at source line 1

With mawk:

mawk: line 1: syntax error at or near :

All of these are pointing to the syntax error at a:2181.

You are correctly setting the awk variable a to a string containing your IP number, but if you want to concatenate its value with :2181 to form a new string you would have to use :2181 as a string: a ":2181"

You should also consider using the idiomatic way of matching against the input and performing an action rather than using explicit if statements:

awk -a="" '$4 == a ":2181" { print $4, $5, $6, $9 }'

Note that there is no need to end a statement with a ; in awk if there is no further statements on the same line.


we solved it by the following syntax

netstat -nape | awk -v a=""  '{if ($4 == a":2181") print $4, $5 ,$6 ,$9;}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.