Separate lsof output by column

lsoftext processing

I am using this command below and trying to separate the columns I only want to get the PID to use it in my python script.

I can easily get this line by line but then how to separate into columns in a non hacky way?

I can easily split by space but lets face it that is a terrible idea, any suggestions?

root@python-VirtualBox:/var/python# lsof | grep TCP
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
      Output information may be incomplete.
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd    3449 root    3u  IPv4  24248      0t0  TCP *:22 (LISTEN)
sshd    3449 root    4u  IPv6  24257      0t0  TCP *:22 (LISTEN)

Best Answer

I think awk is good for this because it splits fields for you:

lsof | awk '$8 == "TCP" { print $2 }'

If field 8 is "TCP", then print field 2.

Related Question