使用 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。

我只想保留其中至少一个值 >=35 的组(由 echo“New Cell”分隔)

我试过:

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

但它正在删除第五列值 <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 }-按原样打印包含StartEndDone或子字符串的行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- 标记已激活并指示要处理的活动部分(一堆行)
  • f{ ... }- 在正在处理的活动部分上:
    • a[++c]=$0$0- 将某个部分中的每个记录/行捕获到数组中a。该数组使用连续数字进行索引++c
    • if ($5 > max) max=$5- 确定已处理的一组记录中max第五个字段值中的最大值$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"

相关内容