How can we extract a column from output in which whitespace is not in a regular format?

For example, when we type ps -ef, it shows all the process, process id's.... etc. How can we get only the last column or any specific column?

  • Even if we go for ls -l in any directory the number of white spaces between the columns would be uneven. – monu Feb 26 '13 at 10:36

Using -o option you can specify what fields should be displayed. List of available fields you can find in man ps or using ps L.

For example using ps -eo user,command will display usernames and commands - something like this:

root /bin/login --
root /sbin/getty 

You can also use -O and parameters which will cause using some default fields and appending that specified by you, but if you want to parse output I will recommend o.


Maybe you want:

ps -eo comm

To get the command name or

ps -eo args

To get the command args or

ps -eo args="Last-Arg" | awk '{print $NF}'

To get the last argument.

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.