21

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.

6 Answers 6

26
sed 's/.*pid \([0-9]*\).*/\1/'
Sign up to request clarification or add additional context in comments.

1 Comment

In order for this to work you'll need to specify the option to enable extended regular expressions. This is usually '-r' (GNU sed) or '-E' (BSD sed). See: stackoverflow.com/questions/16637799/…
8
$ cat $your_output | sed -s 's/.*pid \([0-9]\+\),.*/\1/'
16652
16651
16607

2 Comments

This is known as a UUOC (Useless use of cat)
@mikerobi : that was just an example of how to pipe stuff into the command... he didn't say how he had his output... but whatever.
8

Using AWK alone:

awk -F'[ ,]+' '{print $4}' inputfile

2 Comments

Works great with a pipe: $ status |grep nginx |awk -F'[ ,]+' '{print $4}' -
@Keith: It's not necessary to pipe grep into awk. AWK can do it all by itself: status | awk -F'[ ,]+' '/nginx/ {print $4}'
6

Solution with awk and cut

vinko@parrot:~$ cat test
frontend                         RUNNING    pid 16652, uptime 2:11:17
nginx                            RUNNING    pid 16651, uptime 2:11:17
redis                            RUNNING    pid 16607, uptime 2:11:32
vinko@parrot:~$ awk '{print $4}' test | cut -d, -f 1
16652
16651
16607

for nginx only:

vinko@parrot:~$ grep nginx test | awk '{print $4}' | cut -d, -f 1
16651

4 Comments

there's no need to use awk + cut + grep. Just awk will do the job. awk -F"," '/nginx/{print $1}' file
@ghostdog74: your command outputs "nginx RUNNING pid 16651". The correct awk only solution is in the answer by Dennis (stackoverflow.com/questions/3045493/…), using -F '[ ,]+' instead of -F ','
@vinko, its either that, or just use gsub() to get rid of the comma for awk versions that does not support multiple field delimiters. I am just highlighting the point that awk can do the job and there's no need to use other tools.
@ghostdog74: Right, you can do it with awk only.
2

Take a look at pgrep, a variant of grep specially tailored for grepping process tabless.

Comments

1

assuming that the grep implementation supports the -o option, you could use two greps:

output \
  | grep -o '^nginx[[:space:]]\+[[:upper:]]\+[[:space:]]\+pid [0-9]\+' \
  | grep -o '[0-9]\+$'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.