ps -edf | grep mongod | cut -d/ -f5 | cut -d- -f4,5
$ echo 'mongod 2530 1 3 Jan23 ? 23:08:29 /var/lib/mongodb-mms-automation/mongodb-linux-x86_64-3.2.6-ent/bin/mongod -f /var/lib/mongodb/automation-mongod.conf' > /tmp/edf
$ cat /tmp/edf | sed -ne 's:.*mongodb-[ ^-][ ^-]*-\([ ^/-][ ^/-]*\).*:\1:p'
$ cat /tmp/edf | sed -ne 's:.*mongodb-[^-][^-]*-\([^/-][^/-]*\).*:\1:p'
x86_64
$ echo 'mongod 2530 1 3 Jan23 ? 23:08:29 /var/lib/mongodb-mms-automation/mongodb-linux-x86_64-3.2.6-ent/bin/mongod -f /var/lib/mongodb/automation-mongod.conf'| sed -e 's/ */ /g' | cut -d' ' -f 8 | cut -d'/' -f 5 | cut -d'-' -f 4,5
3.2.6-ent
$
So it works... please check space count s/ */ /g reads: es slash space space star slash space slash gee ps -edf | grep '^mongod ' | cut -d/ -f5 | cut -d- -f4,5
(Note: I've now added '^' above to ensure the match is at the start of the line only, (and presumably speed up the matching process minutely), and quotes with a space after to prevent matches with (admittedly unlikely) usernames like 'mongod2'.)
$ cat /tmp/edf | sed -ne 's:.*mongodb-[^-][^-]*-[^-][^-]*-\([^/-][^/-]*\).*:\1:p'
3.2.6
ps -edf | grep -i mongod | sed -e 's/ */ /g' | cut -d' ' -f 8 | cut -d'/' -f 5 | cut -d'-' -f 4,5
Open in new window
sed with reduce all multiple spaces to one space.
then cut (space) will get the program part,
then cut (/) will get the directory part and
then cut (-) get the last elements