parse multiple lines of text bash

1

I need to parse

0 R username+ 13790 13743  0  80   0 - 28162 -      19:07 pts/0    00:00:00 grep --color=auto pm2
4 S root     25197     1  0  80   0 - 237119 ep_pol Apr05 ?       00:00:32 node /usr/local/lib/node_modules/pm2/bin/pm2

type of output and find the PID for the /usr/local/lib/node_modules/pm2/bin/pm2 process.

so far I have

PROCESS_ID=$(ps -elf | grep pm2 | grep -v grep | awk 'FNR<2{print $4}')

but this only get's the PID for the first line. There's no guarantee what line the correct PID will be, as the upstart program sometimes shows 2-3 results for this grep. I need a way to filter which line to read to awk I guess, since the desired line will only ever be there one at a time. Any help appreciated

codyc4321

Posted 2017-04-07T19:11:01.350

Reputation: 298

1Look up pgrep command - it should be much simpler (single command) – Marek Rost – 2017-04-07T19:18:59.890

Answers

1

Take a look at pgrep:

pgrep -f /usr/local/lib/node_modules/pm2/bin/pm2

Cyrus

Posted 2017-04-07T19:11:01.350

Reputation: 4 356

0

awk can be filtered by starting the expression with /regex/

I changed

ps -elf | grep pm2 | grep -v grep | awk 'FNR<2{print $4}'

to

ps -elf | grep pm2 | grep -v grep | awk '/node_modules/FNR<2{print $4}'

and it worked. You can also search for longer path by escaping slashes:

ps -elf | grep pm2 | grep -v grep | awk '/\/lib\/node_modules/FNR<2{print $4}'

also works

As terrifying as awk is this article was very easy http://www.tecmint.com/use-linux-awk-command-to-filter-text-string-in-files/

codyc4321

Posted 2017-04-07T19:11:01.350

Reputation: 298