如何根据前一列中的变量从 txt 文件中删除多行?

如何根据前一列中的变量从 txt 文件中删除多行?

想法是这样的:我有一个文档,其中的数据按以下方式列出(如下所示),我想删除第 6 列(最后一列)中值为 70 或更小的任何行。除此之外,我还想删除其后具有匹配的第 3 列(纬度坐标)的任何行。
问题在于,一旦纬度发生变化(第 3 列),我想重新开始寻找第一个低于 70 的值,并以类似的方式删除之后的任何行,直到我达到新的纬度值。

问题在于,并非每个相同纬度的“分组”都具有相同数量的线,而且它们也并不总是以 48.15(第 1 列)结束。

我一直在尝试使用 sed -z 选项,但是我很难创建一个适当的字符串模式来识别我想要删除的内容,并且在重新启动字符串之前只删除下一个分组。

6.15 21.31750 29.11549 -70.2565 28.7203 99.00000
8.15 21.31750 29.11549 -90.3238 35.0326 99.00000
10.15 21.31750 29.11549 -84.9625 43.2992 100.00000
12.15 21.31750 29.11549 -77.0993 44.3515 99.00000
14.15 21.31750 29.11549 -70.9164 49.5554 96.00000
16.15 21.31750 29.11549 -82.2717 38.6834 98.00000
18.15 21.31750 29.11549 -83.7156 35.6462 99.00000
20.15 21.31750 29.11549 -83.9505 35.1276 95.00000
22.15 21.31750 29.11549 -167.575 66.1472 57.00000
24.15 21.31750 29.11549 -94.5072 38.15 98.00000
26.15 21.31750 29.11549 -38.9739 69.8479 71.00000
28.15 21.31750 29.11549 -94.5072 38.15 98.00000
30.15 21.31750 29.11549 -28.6407 51.3899 24.00000
32.15 21.31750 29.11549 -43.7269 119.979 2.00000
34.15 21.31750 29.11549 -69.6645 150 0.00000
36.15 21.31750 29.11549 -103.964 150 1.00000
38.15 21.31750 29.11549 42.6041 142.656 0.00000
40.15 21.31750 29.11549 109.056 150 0.00000
42.15 21.31750 29.11549 -14.6037 150 45.00000
44.15 21.31750 29.11549 -118.694 53.7305 94.00000
46.15 21.31750 29.11549 -167.053 115.74 92.00000
48.15 21.31750 29.11549 -171.917 150 66.00000
6.15 20.38500 29.12283 -87.9018 36.2993 100.00000
8.15 20.38500 29.12283 -98.356 43.8404 100.00000
10.15 20.38500 29.12283 -88.9825 46.6824 100.00000
12.15 20.38500 29.12283 -78.2202 44.7168 97.00000
14.15 20.38500 29.12283 -78.1702 42.5794 97.00000
16.15 20.38500 29.12283 -76.6382 40.3678 98.00000
18.15 20.38500 29.12283 -79.449 49.3087 95.00000
20.15 20.38500 29.12283 -137.565 45.7575 66.00000
22.15 20.38500 29.12283 -112.652 37.5735 100.00000
24.15 20.38500 29.12283 -55.8986 43.9287 54.00000
26.15 20.38500 29.12283 -50.4227 48.2312 70.00000
28.15 20.38500 29.12283 -55.8986 43.9287 54.00000
30.15 20.38500 29.12283 -57.3999 98.6111 8.00000
32.15 20.38500 29.12283 -74.2068 150 6.00000
36.15 20.38500 29.12283 17.7038 117.808 5.00000
38.15 20.38500 29.12283 -5.36164 96.0492 0.00000
40.15 20.38500 29.12283 -98.5051 99.8733 42.00000
42.15 20.38500 29.12283 -149.328 41.7056 96.00000
44.15 20.38500 29.12283 -172.026 126.696 92.00000
46.15 20.38500 29.12283 -174.664 150 76.00000
48.15 20.38500 29.12283 -176.269 139.467 31.00000

因此,我想要删除第一个分组中从 22.15 到 48.15 的所有内容,以及第二个分组中从 20.15 到 48.15 的所有内容。

bash 格式是理想的,因为我使用的是 GMT5(仅接受 bash)。

任何帮助将不胜感激。

答案1

使用 Perl:

perl -lane 'if($F[5] < 70 || $F[2] == $x) {$x = $F[2]; next} undef $x; print' file
perl -lane '
    if($F[5] < 70 || $F[2] == $x) {
        $x = $F[2];
        next
    }
    undef $x;
    print
' file
  • -l[octnum]: 启用自动行结束处理。它有两个单独的效果。首先,当与或$/一起使用时,它会自动 chomps(输入记录分隔符)。其次,它分配(输出记录分隔符)以具有 octnum 的值,以便任何打印语句都会重新添加该分隔符。如果省略 octnum,则设置为 的当前值。-n-p$\$\$/
  • -a-n:与或一起使用时打开自动拆分模式-p。对数组的隐式拆分命令@F是 或 产生的隐式 while 循环中的第一-n件事-p
  • -n:导致 Perl 假设您的程序中存在以下循环,这使得它像sed -n或一样迭代文件名参数awk

    LINE:
      while (<>) {
          ...             # your program goes here
      }
    
  • -e:可用于输入一行程序;

  • if($F[5] < 70 || $F[2] == $x) {$x = $F[2]; next} print:如果第六个字段包含的数字小于70或第二个字段包含的数字等于$x,则将第二个字段分配给$x并跳到下一个记录;否则取消设置$x并打印该记录。
% cat file
6.15 21.31750 29.11549 -70.2565 28.7203 99.00000
8.15 21.31750 29.11549 -90.3238 35.0326 99.00000
10.15 21.31750 29.11549 -84.9625 43.2992 100.00000
12.15 21.31750 29.11549 -77.0993 44.3515 99.00000
14.15 21.31750 29.11549 -70.9164 49.5554 96.00000
16.15 21.31750 29.11549 -82.2717 38.6834 98.00000
18.15 21.31750 29.11549 -83.7156 35.6462 99.00000
20.15 21.31750 29.11549 -83.9505 35.1276 95.00000
22.15 21.31750 29.11549 -167.575 66.1472 57.00000
24.15 21.31750 29.11549 -94.5072 38.15 98.00000
26.15 21.31750 29.11549 -38.9739 69.8479 71.00000
28.15 21.31750 29.11549 -94.5072 38.15 98.00000
30.15 21.31750 29.11549 -28.6407 51.3899 24.00000
32.15 21.31750 29.11549 -43.7269 119.979 2.00000
34.15 21.31750 29.11549 -69.6645 150 0.00000
36.15 21.31750 29.11549 -103.964 150 1.00000
38.15 21.31750 29.11549 42.6041 142.656 0.00000
40.15 21.31750 29.11549 109.056 150 0.00000
42.15 21.31750 29.11549 -14.6037 150 45.00000
44.15 21.31750 29.11549 -118.694 53.7305 94.00000
46.15 21.31750 29.11549 -167.053 115.74 92.00000
48.15 21.31750 29.11549 -171.917 150 66.00000
6.15 20.38500 29.12283 -87.9018 36.2993 100.00000
8.15 20.38500 29.12283 -98.356 43.8404 100.00000
10.15 20.38500 29.12283 -88.9825 46.6824 100.00000
12.15 20.38500 29.12283 -78.2202 44.7168 97.00000
14.15 20.38500 29.12283 -78.1702 42.5794 97.00000
16.15 20.38500 29.12283 -76.6382 40.3678 98.00000
18.15 20.38500 29.12283 -79.449 49.3087 95.00000
20.15 20.38500 29.12283 -137.565 45.7575 66.00000
22.15 20.38500 29.12283 -112.652 37.5735 100.00000
24.15 20.38500 29.12283 -55.8986 43.9287 54.00000
26.15 20.38500 29.12283 -50.4227 48.2312 70.00000
28.15 20.38500 29.12283 -55.8986 43.9287 54.00000
30.15 20.38500 29.12283 -57.3999 98.6111 8.00000
32.15 20.38500 29.12283 -74.2068 150 6.00000
36.15 20.38500 29.12283 17.7038 117.808 5.00000
38.15 20.38500 29.12283 -5.36164 96.0492 0.00000
40.15 20.38500 29.12283 -98.5051 99.8733 42.00000
42.15 20.38500 29.12283 -149.328 41.7056 96.00000
44.15 20.38500 29.12283 -172.026 126.696 92.00000
46.15 20.38500 29.12283 -174.664 150 76.00000
48.15 20.38500 29.12283 -176.269 139.467 31.00000
user@user-X550CL ~/tmp % perl -lane 'if($F[5] < 70 || $F[2] == $x) {$x = $F[2]; next} undef $x; print' file
6.15 21.31750 29.11549 -70.2565 28.7203 99.00000
8.15 21.31750 29.11549 -90.3238 35.0326 99.00000
10.15 21.31750 29.11549 -84.9625 43.2992 100.00000
12.15 21.31750 29.11549 -77.0993 44.3515 99.00000
14.15 21.31750 29.11549 -70.9164 49.5554 96.00000
16.15 21.31750 29.11549 -82.2717 38.6834 98.00000
18.15 21.31750 29.11549 -83.7156 35.6462 99.00000
20.15 21.31750 29.11549 -83.9505 35.1276 95.00000
6.15 20.38500 29.12283 -87.9018 36.2993 100.00000
8.15 20.38500 29.12283 -98.356 43.8404 100.00000
10.15 20.38500 29.12283 -88.9825 46.6824 100.00000
12.15 20.38500 29.12283 -78.2202 44.7168 97.00000
14.15 20.38500 29.12283 -78.1702 42.5794 97.00000
16.15 20.38500 29.12283 -76.6382 40.3678 98.00000
18.15 20.38500 29.12283 -79.449 49.3087 95.00000

相关内容