查找和删除符合条件的行

reg*_*x99 2 linux bash perl awk sed

我有三个不同的文件,其中包含数字列.这些文件非常大(其中包含50,000,000多行)

例如,数据格式如下

1.2 22.333 10002.3432 223.2111
50.2166 2.873 15402.3432 322.1
.
.
.
Run Code Online (Sandbox Code Playgroud)

对于每个文件(file1,file2和file3),我需要执行以下操作:

FILE1 查找包含任何数字x <= 1000的行并从file1中删除行

FILE2 找到包含任何数字x> = 1800的行,并从file2中删除这些行

FILE3 查找包含任何数字1000 <= x <= 1800的行,并从file3中删除这些行.

我对REGEX的了解不足以弄清楚如何快速实现这一目标.任何帮助深表感谢.

Sha*_*hin 6

正如其他人在评论中提到的那样,正则表达式在这种情况下并不理想.

以下是使用以下方法的一种方法awk:

awk '{for (i=1;i<=NF;i++) {if ($i<=1000) next}; if (NF) print}' file1 > new1
Run Code Online (Sandbox Code Playgroud)

解析file和抑制任何包含数字<= 1000(和空行)的行.然后将输出传送到新文件.

对于file2file3,只需更改相关if语句中的条件即可满足您的要求.


这是一个快速解释:

         This is repeated for each line in the input file
                                |
      -------------------------------------------------------
     /                                                       \
awk '{for (i=1;i<=NF;i++) {if ($i<=1000) next}; if (NF) print}'
      ------------------   ------------------   -------------
             |                     |                  |
     for each field/column         |                  |
                                   |                  |
                      If condition is met, skip       |
                             this line                |
                                                      |
                                          otherwise, if the line is
                                          not empty (number of fields != 0)
                                          print out the whole line.
Run Code Online (Sandbox Code Playgroud)


Mat*_*ttH 5

输入文件"sample"的位置是:

500 500 500
1000 1000 1000
2000 2000 2000
3000 3000 3000
Run Code Online (Sandbox Code Playgroud)

地带x <= 1000:

$ awk '{ for (i=1; i<=NF; i++) { if ($i <= 1000) next } print }' < sample
2000 2000 2000
3000 3000 3000
Run Code Online (Sandbox Code Playgroud)

地带x >= 1800:

$ awk '{ for (i=1; i<=NF; i++) { if ($i >= 1800) next } print }' < sample
500 500 500
1000 1000 1000
Run Code Online (Sandbox Code Playgroud)

地带1000 <= x <= 1800:

$ awk '{ for (i=1; i<=NF; i++) { if (1000 <= $i && $i <= 1800) next } print }' < sample
500 500 500
2000 2000 2000
3000 3000 3000
Run Code Online (Sandbox Code Playgroud)