2

I've got output coming from lsof -F that looks like this:

p7646
g7646
R8300
csocat
u1000
Labe
f3
au
l
tIPv4
G0x80002;0x0
d640391
o0t0
PTCP
n*:51352
TST=LISTEN
TQR=0
TQS=0

I am trying to capture the value 51352, which is a bound port I'm interested in knowing.

I'm close in that I can get the n*:51352 value with this:

awk '/^n/ { print $1 }'

and can actually get the exact 51352 value I need with two separate awks:

awk '/^n/ { print $1 }' | awk -F':' '{print $2}'

but is there a better way, either a single-command awk or a cleaner non-awk solution?

There should only ever be one line starting with n* so I don't need to worry about handling multiple lines.

4

Combine the two:

awk -F: '/^n\*:/ {print $2}'
3

Other possibilities:

  • sed

    sed -n 's/^n\*:\(.*\)/\1/p'
    
  • grep

    grep -Po '^n\*:\K.*'
    

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.