GNU/Linux >> Linux の 問題 >  >> Linux

パイプラインで`ping`からの出力の各ラインをすぐに処理しますか?

ping -c 10 google.comからタイミング情報を抽出するさまざまな方法の例をいくつか示します。 。これらのパイプラインの一部では、pingの出力と同じように、出力の行が頻繁に生成されます。その他の場合、すべての出力ラインが処理された後、出力ラインが一度に出力されます。最初の行動をいつ見るか、2番目の行動をいつ見るかについての良いルールはありますか?

# prints output for each line as soon as it is received
# on OS X and Linux.
ping -c 10 google.com | grep -o 'time=S*'

# prints output for each line as soon as it is received on OS X
# but not on Linux 
# (the output of ping is slightly different so it's $8 on Linux to get the time)
ping -c 10 google.com | awk '{ print $7 }'

# waits until all input is received on OS X and Linux
ping -c 10 google.com | awk -F ':' '{ print $2 }'

# prints output for line as soon as it is received on Linux and OS X
ping -c 10 google.com | sed -e 's/^.*time=(.*) .*$/1/'

# waits for the end of input on OS X and Linux
ping -c 10 google.com | grep -o 'timeS*' | sed -e 's/time=//'

# as a quick check, this prints each line of output immediately 
# on OS X and Linux
ping -c 10 google.com | sed -e 's/time=//' 

少し調べてみると、これはラインバッファリングの問題であり、インタラクティブに使用した場合と非インタラクティブに使用した場合の動作が異なる標準ユーティリティの一部のようです。

承認された回答:

これらのプログラムでバッファリングがどのように処理されるかについてです。

grepでパイプされたデータをすぐに出力する場合は、オプション–line-bufferedとともに使用します。

ping -c 10 google.com | grep --line-buffered -o 'timeS*' | sed -e 's/time=//'

awkでパイプされたデータをすぐに出力したい場合は、オプション-Wインタラクティブを使用できます。

ping -c 10 google.com | awk -W interactive '{ print $7 }'

詳細については、これらのアプリのマニュアルページをお読みください。


Linux
  1. Lsからの出力には改行がありますが、1行で表示されます。なんで?

  2. サブシェルの出力をプロセスにリダイレクトしますか?

  3. Bashスクリプト:コマンド出力の各行を配列内の値に割り当てる方法は?

  1. 出力列から冗長性を削除しますか?

  2. C hello world のアセンブリ出力の各行の意味は何ですか?

  3. バックグラウンド プロセスとしてコマンド ラインから PHP スクリプトを実行する

  1. シェル スクリプトの各行から最初の 5 文字を取得する

  2. コマンドラインからGNU/Linuxで特定のプロセスの親PIDを取得するには?

  3. コマンド ライン:出力から部分文字列を抽出する