grepps

How to get only a specific "row" of a string?


I'm trying to get the lowest PID of a specific process of a user. In my case the user is named as content and the specific process is named as gnome-terminal-server. I'm using the below command to get the desired output.

command

ps -eF|grep -F 'gnome-terminal-server'|grep -E '^content'|head -n1

output

content     1758    1659  0 116290 39408  0 09:07 ?        00:00:01 /usr/libexec/gnome-terminal-server

But I fail to format the output accordingly. I only want to get the PID no. 1758, so that I may assign it to a variable, like TERMINAL_PID=1758, for further processing.

So how do I format the output in bash/sh to get only the 2nd row (1758)?


Solution

  • In order to find a line, you use grep, which is ok.

    In order to show a part of that line, you can use cut or awk. If you choose for awk, you can add this to your command:

    | awk '{print $2}'
    

    (Show the second column)

    So, your command becomes:

    ps -eF | grep -F 'gnome-terminal-server' | grep -E '^content' | head -n1 | awk '{print $2}'