2016-10-29 16 views
0

をクリーンアップこれは私が2列と、各プロセスのコマンドラインで-fオプション次のパスの最後の成分を抽出しようとしている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 

-ef私のPSの出力です。

私はそれをクリーンアップするために以下のコードを使用しています。このようになります

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 
+1

[宿題](http://stackoverflow.com/q/40309305/3776858)? – Cyrus

+0

明らかに、 'substr($ NF、7,10)'を最初からオフセットをハードコーディングするのではなく、フィールドの終わりから固定長のものを取り除くものに置き換えたいとします。 – tripleee

+0

も参照してください。[grep'の無駄な使い方](http://www.iki.fi/era/unix/award.html#grep) – tripleee

答えて

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 }' 
+0

これはちょうど素晴らしい作品です! – joker

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' 

であり、多くのバックスラッシュは、特定の記号をエスケープするだけです。

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 
+0

も参照してください! – joker

関連する問題