3

Below is Output of command:

netstat -plten|grep  45678
tcp        0      0 :::45678                    :::*                        LISTEN      0          129367622  109746/java

However, I want to get ‘109746’ as output.

I tried:

netstat -plten|grep  45678|awk ' {print$9}'

But this one gives 109746/java as an output; is there any way, I can use $9 to match with some regex such that I get desired output.

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
muhdchoaib
  • 33
  • 5

1 Answers1

4

You can tell awk to treat /, in addition to blank, as a field separator. That way, java is considered a separate field and not included in field 9:

$ netstat -plten | awk -F'[ /]+' ' /45678/{print$9}'
109746

-F'[ /]+' tells awk to use a blank, a /, or any combination of those two as the field separator.

Also, the use of grep 45678 in front of awk is not necessary. In the above, the awk condition /45678/ selects for lines containing 45678.

John1024
  • 109,961
  • 14
  • 137
  • 171
  • 1
    Thank you so much, I really appreciate your response. – muhdchoaib Sep 14 '15 at 20:01
  • Produced blank output for me but here's what helped me: `netstat -ptan | awk -F'[ /]+' '{print$7}'` Also, bonus: print full command line for found processes `netstat -ptan | grep 45678 | awk -F'[ /]+' '{print$7}' | sed -e 's/^/pidstat -l -p /g' | bash -s` – GDR Jan 16 '20 at 08:02