0

I want to retrieve the second column from ps result:

test@pc:~$ ps -e | head -5 | cut -d '' -f1
  PID TTY          TIME CMD
    1 ?        00:00:03 systemd
    2 ?        00:00:00 kthreadd
    3 ?        00:00:00 rcu_gp
    4 ?        00:00:00 rcu_par_gp

It does not work, I learned that it could be solve the a complicated way as:

test@pc:~$ ps -e | head -5 | gawk '{print $2}'
TTY
?
?
?
?

How could solve the problem with the generic tool of cut?

  • 2
    If you are especially after the output of ps: you can control what columns it prints with the -o (for output) switch like so: ps -e -o tname. Here tname is the column ID to print. – PerlDuck Jan 18 at 10:43
1

First we replace each sequence of a repeated spaces using tr -s ' ' then we grab the 3th column:

ps -e | head -5 | tr -s ' ' | cut -d ' ' -f3

3th column because there is a space at the beginning of each line. We can remove that too:

ps -e | head -5 | tr -s ' ' | sed 's/ //' | cut -d ' ' -f2
  • 1
    so amazing your answer. – JawSaw Jan 18 at 10:15

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.