Search code examples
bashpidtaillsof

How to extract exact data from a file?


I have used this command - lsof +D /usr/bin and generated following output -

COMMAND    PID USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
metacity  1347  gdm  txt    REG    8,1   609760 278555 /usr/bin/metacity
tail      1474 root  txt    REG    8,1    59704 269067 /usr/bin/tail

Now I want to extract 1474 from above output and I am trying to use following command

lsof +D /usr/bin | grep "tail">tailfind.txt

But this command is injecting following information -

tail      1474 root  txt    REG    8,1    59704 269067 /usr/bin/tail

Now help me to get only specific column from the above operations.

Thanks and regards, Shah9il


Solution

  • Better to use awk for searching input and printing specific column(s):

    lsof +D /usr/bin | awk '/tail/{print $2}'