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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityHow 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?
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.