清理ps -ef输出

时间:2016-10-29 06:26:08

标签: bash awk

这是我的ps -ef

的输出
$ ps -ef | grep logstash  
hdfs      72561      1 58 10:08 pts/5    00:59:35 /usr/java/default/bin/java -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -Djava.awt.headless=true -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+HeapDumpOnOutOfMemoryError -Xmx1g -Xss2048k -Djffi.boot.library.path=/grid01/es/CF_STAGE_ES/logstash-2.2.2/vendor/jruby/lib/jni -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -Djava.awt.headless=true -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/grid01/es/CF_STAGE_ES/logstash-2.2.2/heapdump.hprof -Xbootclasspath/a:/grid01/es/CF_STAGE_ES/logstash-2.2.2/vendor/jruby/lib/jruby.jar -classpath : -Djruby.home=/grid01/es/CF_STAGE_ES/logstash-2.2.2/vendor/jruby -Djruby.lib=/grid01/es/CF_STAGE_ES/logstash-2.2.2/vendor/jruby/lib -Djruby.script=jruby -Djruby.shell=/bin/sh org.jruby.Main --1.9 /grid01/es/CF_STAGE_ES/logstash-2.2.2/lib/bootstrap/environment.rb logstash/runner.rb agent -f /grid01/es/CF_STAGE_ES/CF_STAGE.es.config  
hdfs      96424      1 38 10:27 pts/5    00:31:41 /usr/java/default/bin/java -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -Djava.awt.headless=true -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+HeapDumpOnOutOfMemoryError -Xmx1g -Xss2048k -Djffi.boot.library.path=/grid01/es/CSL_STAGE/logstash-2.2.2/vendor/jruby/lib/jni -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -Djava.awt.headless=true -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/grid01/es/CSL_STAGE/logstash-2.2.2/heapdump.hprof -Xbootclasspath/a:/grid01/es/CSL_STAGE/logstash-2.2.2/vendor/jruby/lib/jruby.jar -classpath : -Djruby.home=/grid01/es/CSL_STAGE/logstash-2.2.2/vendor/jruby -Djruby.lib=/grid01/es/CSL_STAGE/logstash-2.2.2/vendor/jruby/lib -Djruby.script=jruby -Djruby.shell=/bin/sh org.jruby.Main --1.9 /grid01/es/CSL_STAGE/logstash-2.2.2/lib/bootstrap/environment.rb logstash/runner.rb agent -f /grid01/es/CSL_STAGE/CSL_STAGE.es.config  
hdfs     136554      1 10 10:57 pts/5    00:05:36 /usr/java/default/bin/java -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -Djava.awt.headless=true -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+HeapDumpOnOutOfMemoryError -Xmx1g -Xss2048k -Djffi.boot.library.path=/grid01/es/SENSE_STAGE/logstash-2.2.2/vendor/jruby/lib/jni -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -Djava.awt.headless=true -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/grid01/es/SENSE_STAGE/logstash-2.2.2/heapdump.hprof -Xbootclasspath/a:/grid01/es/SENSE_STAGE/logstash-2.2.2/vendor/jruby/lib/jruby.jar -classpath : -Djruby.home=/grid01/es/SENSE_STAGE/logstash-2.2.2/vendor/jruby -Djruby.lib=/grid01/es/SENSE_STAGE/logstash-2.2.2/vendor/jruby/lib -Djruby.script=jruby -Djruby.shell=/bin/sh org.jruby.Main --1.9 /grid01/es/SENSE_STAGE/logstash-2.2.2/lib/bootstrap/environment.rb logstash/runner.rb agent -f /grid01/es/SENSE_STAGE/SENSE_STAGE.es.config    
hdfs     203513      1 82 11:37 pts/5    00:10:08 /usr/java/default/bin/java -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -Djava.awt.headless=true -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+HeapDumpOnOutOfMemoryError -Xmx1g -Xss2048k -Djffi.boot.library.path=/grid01/es/SMARTSCAN_STAGE/logstash-2.2.2/vendor/jruby/lib/jni -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -Djava.awt.headless=true -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/grid01/es/SMARTSCAN_STAGE/logstash-2.2.2/heapdump.hprof -Xbootclasspath/a:/grid01/es/SMARTSCAN_STAGE/logstash-2.2.2/vendor/jruby/lib/jruby.jar -classpath : -Djruby.home=/grid01/es/SMARTSCAN_STAGE/logstash-2.2.2/vendor/jruby -Djruby.lib=/grid01/es/SMARTSCAN_STAGE/logstash-2.2.2/vendor/jruby/lib -Djruby.script=jruby -Djruby.shell=/bin/sh org.jruby.Main --1.9 /grid01/es/SMARTSCAN_STAGE/logstash-2.2.2/lib/bootstrap/environment.rb logstash/runner.rb agent -f /grid01/es/SMARTSCAN_STAGE/SMARTSCAN_STAGE.es.config  
hdfs     602508      1 10 Aug25 ?        6-18:21:48 /usr/java/default/bin/java -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -Djava.awt.headless=true -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+HeapDumpOnOutOfMemoryError -Xmx1g -Xss2048k -Djffi.boot.library.path=/grid01/es/logstash-2.2.2/vendor/jruby/lib/jni -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -Djava.awt.headless=true -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/grid01/es/logstash-2.2.2/heapdump.hprof -Xbootclasspath/a:/grid01/es/logstash-2.2.2/vendor/jruby/lib/jruby.jar -classpath : -Djruby.home=/grid01/es/logstash-2.2.2/vendor/jruby -Djruby.lib=/grid01/es/logstash-2.2.2/vendor/jruby/lib -Djruby.script=jruby -Djruby.shell=/bin/sh org.jruby.Main --1.9 /grid01/es/logstash-2.2.2/lib/bootstrap/environment.rb logstash/runner.rb agent -f /grid01/es/logstash-2.2.2/AERO_STAGE_es.config  

我正在尝试在每个进程的命令行中提取-f选项之后的第二列和路径的最后一个组件。

我正在使用以下代码进行清理:

ps -ef | grep logstash | grep -v grep| awk -F" " '{print "Logstash is running for " substr($NF,7,10) " & PID number is " $2 }'

看起来像这样:

Logstash is running for S/CF_STAGE.es.config & PID number is 72561  
Logstash is running for CSL_STAGE.es.config & PID number is 96424  
Logstash is running for E/SENSE_STAGE.es.config & PID  is  
Logstash is running for STAGE/SMARTSCAN_STAGE.es.co & PID number is 203513  
Logstash is running for .2.2/AERO_STAGE_es.config & PID number is 602508  

现在的问题是我只想让它看起来像这样(只是主题名称和PID):

Logstash is running for CF_STAGE & PID number is 72561  
Logstash is running for CSL_STAGE & PID number is 96424  
Logstash is running for SENSE_STAGE & PID number 136554  
Logstash is running for SMARTSCAN_STAGE & PID number is 203513  
Logstash is running for AERO_STAGE & PID number is 602508 

3 个答案:

答案 0 :(得分:0)

如果你总是希望摆脱相同的文本块,那么你可以将输出到目前为止的几个sed:

ps -ef | grep logstash | grep -v grep |
awk -F" " '{print "Logstash is running for " substr($NF,7,10) " & PID number is " $2 }' |
sed 's/S\///g' | sed 's/\.es\.config//g' | sed 's/E\///g' |
sed 's/STAGE\///g' | sed 's/\.es\.co//g' | sed 's/\.2\.2//g'

我希望我没有错过任何东西。 一般命令是

sed 's/SUBSTITUTE_THIS/WITH_THIS/g'

并且许多反斜杠只是为了逃避某些符号。

答案 1 :(得分:0)

使用function可能。你可以试试这个命令。

ps -ef | grep logstash | grep -v grep| awk -F" " 'function subst(str){
    split(str, arr, "/") ;
    split(arr[5], arr2, ".") ;
    return arr2[1] }   {print "Logstash is running for " subst($NF) " & PID number is " $2 }'
通过这个你会得到这样的正确答案。

Logstash is running for CF_STAGE & PID number is 72561
Logstash is running for CSL_STAGE & PID number is 96424
Logstash is running for SENSE_STAGE & PID number is 136554
Logstash is running for SMARTSCAN_STAGE & PID number is 203513
Logstash is running for AERO_STAGE_es & PID number is 602508

答案 2 :(得分:0)

使用split()

ps -ef | grep logstash | grep -v grep | awk -F" " '{split($NF, a, "/"); split(a[5], b, "."); print "Logstash is running for " b[1] " & PID number is " $2 }'