Parse string with bash and extract number

后端 未结 6 1243
猫巷女王i
猫巷女王i 2021-01-04 04:41

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

frontend                         RUNNING    pid 16652, uptime 2:11:17
nginx                                


        
相关标签:
6条回答
  • 2021-01-04 04:50

    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
    
    0 讨论(0)
  • 2021-01-04 04:51

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

    0 讨论(0)
  • 2021-01-04 04:57

    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]\+$'
    
    0 讨论(0)
  • 2021-01-04 04:58
    sed 's/.*pid \([0-9]*\).*/\1/'
    
    0 讨论(0)
  • 2021-01-04 05:08

    Using AWK alone:

    awk -F'[ ,]+' '{print $4}' inputfile
    
    0 讨论(0)
  • 2021-01-04 05:08
    $ cat $your_output | sed -s 's/.*pid \([0-9]\+\),.*/\1/'
    16652
    16651
    16607
    
    0 讨论(0)
提交回复
热议问题