0

Is there a way to restrict the output of, say ps aux | grep word, so that each process is displayed on a separate single line?

I need this, because my processes descriptions take up a lot of lines and I don't have good visibility when listing them with the above command. I would be happy to see just the first line of each process' representation.

  • 2
    Configure your terminal to not wrap lines or cut the lines manually. – Ipor Sircer Dec 20 '17 at 9:55
1

Actually, each process is already displayed on a single line, that's why grep shows that much info.

If you are willing to sacrifice the amount of details that the "COMMAND" column of ps aux offers, you may trim the output as such:

ps aux | awk '/word/ {print $1"\t"$2"\t"$3"\t"$4"\t"$9"\t"$11}'

The awk looks for lines containing pattern 'word' and prints:

the USER, PID, %CPU, %MEM, START and first string (delimited by space)from the COMMAND column, all separated by TAB('\t')

| improve this answer | |
1

You may find the pstree command useful. It lists all processes as a tree diagram.

| improve this answer | |
  • This is for sure a very useful command, thanks for sharing it. Unfortunately, my java processes seem to be listed as a single line ├─java───98*[{java}] under systemd . I will definitely use it, though. – Mateva Dec 20 '17 at 9:55
1

When ps aux output is directed to a terminal, the command limits the command part to the width of the screen.

When the output is piped to a command, the behavior vary but usually, the width is unlimited. If you want to limit it the same way, you can use that command:

ps aux --witdh ${COLUMNS:-80} | grep word

Of course, if word isn't in the displayed characters but in the truncated ones, this won't fly.

Here are a couple of alternatives:

ps aux | awk '/word/ {print substr($0,1,'${COLUMNS:-80}')}'

ps -up $(pgrep -f word|tr '\n' ','| sed 's/,$//') 
| improve this answer | |

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.