Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
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
add comment

2 Answers

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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