awkを使用したテキストのフィルタリング

awkを使用したテキストのフィルタリング
echo "Start 2A25.20090401.64809.7.HDF 6420 6751" 
echo "dimensions 9249 49"  
echo "New Cell"   
grep "6621,38" out.2A25.20090401.64809.7.HDF.txt.text = 36.58   7.52  77.82 210
grep "6623,41" out.2A25.20090401.64809.7.HDF.txt.text = 28.62   7.59  77.96 210
grep "6624,40" out.2A25.20090401.64809.7.HDF.txt.text = 33.74   7.52  77.96 210
grep "6624,41" out.2A25.20090401.64809.7.HDF.txt.text = 42.25   7.56  77.99 210
grep "6625,41" out.2A25.20090401.64809.7.HDF.txt.text = 30.58   7.54  78.02 210
grep "6627,40" out.2A25.20090401.64809.7.HDF.txt.text = 34.5   7.46  78.05 200
grep "6628,39" out.2A25.20090401.64809.7.HDF.txt.text = 35.57   7.40  78.05 200
grep "6628,40" out.2A25.20090401.64809.7.HDF.txt.text = 44.82   7.44  78.08 200
grep "6628,41" out.2A25.20090401.64809.7.HDF.txt.text = 21.35   7.47  78.11 210
grep "6629,40" out.2A25.20090401.64809.7.HDF.txt.text = 26.67   7.41  78.11 210
grep "6630,41" out.2A25.20090401.64809.7.HDF.txt.text = 28.12   7.43  78.17 210
grep "6631,40" out.2A25.20090401.64809.7.HDF.txt.text = 21.4   7.37  78.17 272
grep "6631,41" out.2A25.20090401.64809.7.HDF.txt.text = 47.3   7.41  78.20 200
grep "6631,42" out.2A25.20090401.64809.7.HDF.txt.text = 27.42   7.45  78.22 210
grep "6632,41" out.2A25.20090401.64809.7.HDF.txt.text = 39.42   7.39  78.23 200
grep "6632,42" out.2A25.20090401.64809.7.HDF.txt.text = 26.53   7.42  78.25 210
grep "6632,43" out.2A25.20090401.64809.7.HDF.txt.text = 27.72   7.46  78.28 210
grep "6632,44" out.2A25.20090401.64809.7.HDF.txt.text = 41.01   7.50  78.31 210
grep "6631,44" out.2A25.20090401.64809.7.HDF.txt.text = 34.26   7.52  78.28 210
grep "6632,45" out.2A25.20090401.64809.7.HDF.txt.text = 23.02   7.54  78.33 210
grep "6633,41" out.2A25.20090401.64809.7.HDF.txt.text = 32.5   7.36  78.26 272
grep "6633,42" out.2A25.20090401.64809.7.HDF.txt.text = 32.82   7.40  78.28 210
grep "6633,43" out.2A25.20090401.64809.7.HDF.txt.text = 39.28   7.44  78.31 210
grep "6634,41" out.2A25.20090401.64809.7.HDF.txt.text = 26.09   7.34  78.29 272
grep "6634,42" out.2A25.20090401.64809.7.HDF.txt.text = 41.54   7.38  78.31 210
grep "6634,43" out.2A25.20090401.64809.7.HDF.txt.text = 42.26   7.42  78.34 210
grep "6635,42" out.2A25.20090401.64809.7.HDF.txt.text = 31.53   7.36  78.34 210
grep "6635,43" out.2A25.20090401.64809.7.HDF.txt.text = 34.19   7.40  78.37 210
grep "6636,43" out.2A25.20090401.64809.7.HDF.txt.text = 29.9   7.37  78.40 292
echo "New Cell"   
grep "6622,14" out.2A25.20090401.64809.7.HDF.txt.text = 22.24   6.61  77.22 291
grep "6622,15" out.2A25.20090401.64809.7.HDF.txt.text = 29.63   6.65  77.25 291
grep "6623,15" out.2A25.20090401.64809.7.HDF.txt.text = 30.63   6.63  77.28 291
echo "New Cell"   
grep "6630,45" out.2A25.20090401.64809.7.HDF.txt.text = 31.44   7.59  78.27 292
echo "New Cell"   
grep "6646,47" out.2A25.20090401.64809.7.HDF.txt.text = 27.52   7.31  78.81 210
grep "6647,47" out.2A25.20090401.64809.7.HDF.txt.text = 38.67   7.29  78.84 210
grep "6648,47" out.2A25.20090401.64809.7.HDF.txt.text = 37.08   7.27  78.87 210
echo "New Cell"   

私のテキストファイルは上記のとおりです。 Column5は私が興味のある値です。上記の最後の行の値は37.08です。

少なくとも1つの値が35より大きいグループ(echo "New Cell"で区切られた)だけを維持したいと思います。

私は試した:

awk '$5>=35 || /New Cell/ || /Start/ || /dimen/ || /Done/ || /End/' \
  CWoSpix.apr2009.sh*

ただし、5番目の列の値が<35のすべての行を削除します。

予想される出力は次のとおりです。

echo "Start 2A25.20090401.64809.7.HDF 6420 6751" 
echo "dimensions 9249 49"  
echo "New Cell"   
grep "6621,38" out.2A25.20090401.64809.7.HDF.txt.text = 36.58   7.52  77.82 210
grep "6623,41" out.2A25.20090401.64809.7.HDF.txt.text = 28.62   7.59  77.96 210
grep "6624,40" out.2A25.20090401.64809.7.HDF.txt.text = 33.74   7.52  77.96 210
grep "6624,41" out.2A25.20090401.64809.7.HDF.txt.text = 42.25   7.56  77.99 210
grep "6625,41" out.2A25.20090401.64809.7.HDF.txt.text = 30.58   7.54  78.02 210
grep "6627,40" out.2A25.20090401.64809.7.HDF.txt.text = 34.5   7.46  78.05 200
grep "6628,39" out.2A25.20090401.64809.7.HDF.txt.text = 35.57   7.40  78.05 200
grep "6628,40" out.2A25.20090401.64809.7.HDF.txt.text = 44.82   7.44  78.08 200
grep "6628,41" out.2A25.20090401.64809.7.HDF.txt.text = 21.35   7.47  78.11 210
grep "6629,40" out.2A25.20090401.64809.7.HDF.txt.text = 26.67   7.41  78.11 210
grep "6630,41" out.2A25.20090401.64809.7.HDF.txt.text = 28.12   7.43  78.17 210
grep "6631,40" out.2A25.20090401.64809.7.HDF.txt.text = 21.4   7.37  78.17 272
grep "6631,41" out.2A25.20090401.64809.7.HDF.txt.text = 47.3   7.41  78.20 200
grep "6631,42" out.2A25.20090401.64809.7.HDF.txt.text = 27.42   7.45  78.22 210
grep "6632,41" out.2A25.20090401.64809.7.HDF.txt.text = 39.42   7.39  78.23 200
grep "6632,42" out.2A25.20090401.64809.7.HDF.txt.text = 26.53   7.42  78.25 210
grep "6632,43" out.2A25.20090401.64809.7.HDF.txt.text = 27.72   7.46  78.28 210
grep "6632,44" out.2A25.20090401.64809.7.HDF.txt.text = 41.01   7.50  78.31 210
grep "6631,44" out.2A25.20090401.64809.7.HDF.txt.text = 34.26   7.52  78.28 210
grep "6632,45" out.2A25.20090401.64809.7.HDF.txt.text = 23.02   7.54  78.33 210
grep "6633,41" out.2A25.20090401.64809.7.HDF.txt.text = 32.5   7.36  78.26 272
grep "6633,42" out.2A25.20090401.64809.7.HDF.txt.text = 32.82   7.40  78.28 210
grep "6633,43" out.2A25.20090401.64809.7.HDF.txt.text = 39.28   7.44  78.31 210
grep "6634,41" out.2A25.20090401.64809.7.HDF.txt.text = 26.09   7.34  78.29 272
grep "6634,42" out.2A25.20090401.64809.7.HDF.txt.text = 41.54   7.38  78.31 210
grep "6634,43" out.2A25.20090401.64809.7.HDF.txt.text = 42.26   7.42  78.34 210
grep "6635,42" out.2A25.20090401.64809.7.HDF.txt.text = 31.53   7.36  78.34 210
grep "6635,43" out.2A25.20090401.64809.7.HDF.txt.text = 34.19   7.40  78.37 210
grep "6636,43" out.2A25.20090401.64809.7.HDF.txt.text = 29.9   7.37  78.40 292
echo "New Cell"   
echo "New Cell"   
grep "6630,45" out.2A25.20090401.64809.7.HDF.txt.text = 31.44   7.59  78.27 292
echo "New Cell"   
grep "6646,47" out.2A25.20090401.64809.7.HDF.txt.text = 27.52   7.31  78.81 210
grep "6647,47" out.2A25.20090401.64809.7.HDF.txt.text = 38.67   7.29  78.84 210
grep "6648,47" out.2A25.20090401.64809.7.HDF.txt.text = 37.08   7.27  78.87 210
echo "New Cell"   

awkを使用して目的の出力を取得するにはどうすればよいですか?

答え1

Awk解決策:

awk '/Start|End|Done|dimen/{ print }
     /echo "New Cell"/{ 
         if (f) { 
             if (max >= 35) { 
                 for (i=1; i<=c; i++) print a[i]; 
                 print 
             } 
             delete a; c=max=0 
         } 
         f=1 
     }f{ a[++c]=$0; if ($5 > max) max=$5 }' file

詳細:

  • /Start|End|Done|dimen/{ print }Start- またはまたは、または部分End文字Done列を含む行をそのまま印刷します。dimen
  • /echo "New Cell"/{ ... }- パターン化されたラインに会うときecho "New Cell"
    • if (f) { ... }- 最初のモードラインではなく、以前にその部品をすでに処理している場合:
      • if (max >= 35)- 最後の部分の5番目のフィールド値のうち、最も大きい値が次のようなまたは大きい場合35
        • for (i=1; i<=c; i++) print a[i]- 最後のセクションのすべての行を印刷します(配列cの最後のインデックスであり、配列のa長さを表します)。
      • delete a- 配列の消去a(配列内のすべての項目を削除a
      • c=max=0- 主要変数のリセット
    • f=1- アクティブな表示と処理するアクティブ部分(1つの束)を示します。
  • f{ ... }- 処理中のアクティブ部分から:
    • a[++c]=$0$0- セクションのすべてのレコード/行を配列としてキャプチャしますa。配列は連続した数値を使用してインデックス付けされます。++c
    • if ($5 > max) max=$5- 処理されたレコードセット内のmax5番目のフィールド値の最大値を決定します。$5

出力:

echo "Start 2A25.20090401.64809.7.HDF 6420 6751" 
echo "dimensions 9249 49"  
echo "New Cell"   
grep "6621,38" out.2A25.20090401.64809.7.HDF.txt.text = 36.58   7.52  77.82 210
grep "6623,41" out.2A25.20090401.64809.7.HDF.txt.text = 28.62   7.59  77.96 210
grep "6624,40" out.2A25.20090401.64809.7.HDF.txt.text = 33.74   7.52  77.96 210
grep "6624,41" out.2A25.20090401.64809.7.HDF.txt.text = 42.25   7.56  77.99 210
grep "6625,41" out.2A25.20090401.64809.7.HDF.txt.text = 30.58   7.54  78.02 210
grep "6627,40" out.2A25.20090401.64809.7.HDF.txt.text = 34.5   7.46  78.05 200
grep "6628,39" out.2A25.20090401.64809.7.HDF.txt.text = 35.57   7.40  78.05 200
grep "6628,40" out.2A25.20090401.64809.7.HDF.txt.text = 44.82   7.44  78.08 200
grep "6628,41" out.2A25.20090401.64809.7.HDF.txt.text = 21.35   7.47  78.11 210
grep "6629,40" out.2A25.20090401.64809.7.HDF.txt.text = 26.67   7.41  78.11 210
grep "6630,41" out.2A25.20090401.64809.7.HDF.txt.text = 28.12   7.43  78.17 210
grep "6631,40" out.2A25.20090401.64809.7.HDF.txt.text = 21.4   7.37  78.17 272
grep "6631,41" out.2A25.20090401.64809.7.HDF.txt.text = 47.3   7.41  78.20 200
grep "6631,42" out.2A25.20090401.64809.7.HDF.txt.text = 27.42   7.45  78.22 210
grep "6632,41" out.2A25.20090401.64809.7.HDF.txt.text = 39.42   7.39  78.23 200
grep "6632,42" out.2A25.20090401.64809.7.HDF.txt.text = 26.53   7.42  78.25 210
grep "6632,43" out.2A25.20090401.64809.7.HDF.txt.text = 27.72   7.46  78.28 210
grep "6632,44" out.2A25.20090401.64809.7.HDF.txt.text = 41.01   7.50  78.31 210
grep "6631,44" out.2A25.20090401.64809.7.HDF.txt.text = 34.26   7.52  78.28 210
grep "6632,45" out.2A25.20090401.64809.7.HDF.txt.text = 23.02   7.54  78.33 210
grep "6633,41" out.2A25.20090401.64809.7.HDF.txt.text = 32.5   7.36  78.26 272
grep "6633,42" out.2A25.20090401.64809.7.HDF.txt.text = 32.82   7.40  78.28 210
grep "6633,43" out.2A25.20090401.64809.7.HDF.txt.text = 39.28   7.44  78.31 210
grep "6634,41" out.2A25.20090401.64809.7.HDF.txt.text = 26.09   7.34  78.29 272
grep "6634,42" out.2A25.20090401.64809.7.HDF.txt.text = 41.54   7.38  78.31 210
grep "6634,43" out.2A25.20090401.64809.7.HDF.txt.text = 42.26   7.42  78.34 210
grep "6635,42" out.2A25.20090401.64809.7.HDF.txt.text = 31.53   7.36  78.34 210
grep "6635,43" out.2A25.20090401.64809.7.HDF.txt.text = 34.19   7.40  78.37 210
grep "6636,43" out.2A25.20090401.64809.7.HDF.txt.text = 29.9   7.37  78.40 292
echo "New Cell"   
echo "New Cell"   
grep "6646,47" out.2A25.20090401.64809.7.HDF.txt.text = 27.52   7.31  78.81 210
grep "6647,47" out.2A25.20090401.64809.7.HDF.txt.text = 38.67   7.29  78.84 210
grep "6648,47" out.2A25.20090401.64809.7.HDF.txt.text = 37.08   7.27  78.87 210
echo "New Cell"

関連情報