I want to filter and format the output of ps -ax -o pid,command
, but not sure what pipe to use for it.
I want to filter those lines matching a fixed string at a fixed position, just keep the pids for the matched lines, and output them as a space-separated list such as 1234 5678 33121
.
I can trivially do the first step using | egrep 'regex'
, but the second step probably needs sed or something, and I can't work it out. Since sed (and awk) also handle regex filtering, I probably don't need grep.
Multiple space characters are fine, and sort order isn't important, so there's no need to trim the PIDs or order the lines. Example:
$ ps -ax -o pid,command
PID COMMAND
0 [kernel]
1 /sbin/init --
...
255 /usr/local/sbin/check_status
268 /sbin/devd -q
1435 (unlinkd) (unlinkd)
1974 sleep 60
7414 /bin/sh /var/db/test/update.sh
21848 ps -ax -o pid,command
39207 /usr/local/sbin/syslog-ng -p /var/run/syslog-ng.pid
find all processes whose command begins /usr/ (ie lines matching /usr/ in chars 7-11
or the regex ^[0-9 ]{5}\s\/usr\/
) and return their pids (or first 5 chars
) on a single line space-separated. Output:
255 39207 (no \n's, amount of spacing unimportant).
I've tried but sed isn't the easiest command to figure out. How do I do it?