Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse string with bash and extract number

I've got supervisor's status output, looking like this.

frontend                         RUNNING    pid 16652, uptime 2:11:17
nginx                            RUNNING    pid 16651, uptime 2:11:17
redis                            RUNNING    pid 16607, uptime 2:11:32

I need to extract nginx's PID. I've done it via grep -P command, but on remote machine grep is build without perl regular expression support.

Looks like sed or awk is exactly what I need, but I don't familiar with them.

Please help me to find a way how to do it, thanks in advance.

like image 225
cleg Avatar asked Jun 15 '10 13:06

cleg


Video Answer


2 Answers

sed 's/.*pid \([0-9]*\).*/\1/'
like image 179
reko_t Avatar answered Oct 14 '22 15:10

reko_t


Using AWK alone:

awk -F'[ ,]+' '{print $4}' inputfile
like image 25
Dennis Williamson Avatar answered Oct 14 '22 16:10

Dennis Williamson