从基于另一个文件的文件中删除特定行

时间:2014-01-20 07:17:38

标签: sed awk

我在名为ff的文件夹中有一些文本文件,如下所示。我需要根据另一个文件 aa.txt 删除这些文件中的行。

32bm.txt

249  253 A P        -     0   0    8      0, 0.0     6,-1.4     0, 0.0     2,-0.4  -0.287  25.6-102.0 -74.4 161.1   37.1   13.3   10.9
250  254 A K  B      Z  254   0E  77    -48,-2.5   -48,-0.3     4,-0.2     4,-0.3  -0.720 360.0 360.0 -93.4 135.2   38.1   11.1    8.1
252        !*             0   0    0      0, 0.0     0, 0.0     0, 0.0     0, 0.0   0.000 360.0 360.0 360.0 360.0    0.0    0.0    0.0
253  143 B R              0   0   96      0, 0.0    -2,-3.7     0, 0.0     2,-0.2   0.000 360.0 360.0 360.0 110.4   38.4   10.4    3.0
254  144 B Q  B     -Z  250   0E  62     -4,-0.3    -4,-0.2    -3,-0.1     2,-0.1  -0.347 360.0-157.5 -58.1 119.5   39.4   13.6    4.8
255  145 B T        -     0   0   22     -6,-1.4     2,-0.3    -2,-0.2    -7,-0.2  -0.396   7.8-127.4 -91.5 173.9   36.3   15.7    5.4

2fok.txt

1  361 X G              0   0  137      0, 0.0     2,-0.2     0, 0.0     3,-0.0   0.000 360.0 360.0 360.0  97.3   25.2  -16.6   -6.6
2  362 X A        -     0   0   98      1,-0.0     0, 0.0     0, 0.0     0, 0.0  -0.649 360.0 -33.9-148.3  84.1   28.0  -18.6   -4.8
3  363 X R        -     0   0  226     -2,-0.2     2,-0.0     1,-0.1    -1,-0.0   1.000  68.7-149.8  66.4  76.9   31.1  -16.5   -4.0
1  361 B G              0   0  137      0, 0.0     2,-0.2     0, 0.0     3,-0.0   0.000 360.0 360.0 360.0  97.3   25.2  -16.6   -6.6
2  362 B A        -     0   0   98      1,-0.0     0, 0.0     0, 0.0     0, 0.0  -0.649 360.0 -33.9-148.3  84.1   28.0  -18.6   -4.8
3  363 B R        -     0   0  226     -2,-0.2     2,-0.0     1,-0.1    -1,-0.0   1.000  68.7-149.8  66.4  76.9   31.1  -16.5   -4.0`enter code here`

aa.txt文件

32bm    B   143 145
2fok    X   361 363
2moj    B   361 367
-
-
-

例如,在 32bm.txt 中,我只需要有B(column3)的行和143到145(column2)的数字。

期望的输出:

32bm.txt

253  143 B R              0   0   96      0, 0.0    -2,-3.7     0, 0.0     2,-0.2   0.000 360.0 360.0 360.0 110.4   38.4   10.4    3.0
254  144 B Q  B     -Z  250   0E  62     -4,-0.3    -4,-0.2    -3,-0.1     2,-0.1  -0.347 360.0-157.5 -58.1 119.5   39.4   13.6    4.8
255  145 B T        -     0   0   22     -6,-1.4     2,-0.3    -2,-0.2    -7,-0.2  -0.396   7.8-127.4 -91.5 173.9   36.3   15.7    5.4 

2fok.txt

1  361 X G              0   0  137      0, 0.0     2,-0.2     0, 0.0     3,-0.0   0.000 360.0 360.0 360.0  97.3   25.2  -16.6   -6.6
2  362 X A        -     0   0   98      1,-0.0     0, 0.0     0, 0.0     0, 0.0  -0.649 360.0 -33.9-148.3  84.1   28.0  -18.6   -4.8
3  363 X R        -     0   0  226     -2,-0.2     2,-0.0     1,-0.1    -1,-0.0   1.000  68.7-149.8  66.4  76.9   31.1  -16.5   -4.0

1 个答案:

答案 0 :(得分:2)

使用awk你会做这样的事情:

#!/usr/bin/awk -f
NR == FNR { # If we are in the first file
    low[$1,$2]=$3 # store the low value in a map
    hi[$1,$2]=$4 # store the high value in another map
    next # skip the remaining commands
}
# We are not in the first file
($2 >= low[FILENAME,$3]) && ($2 <= hi[FILENAME,$3])
# The FILENAME variable holds the name of the current file
# If the number we read is within the range, do the 
# default action (which is to print the current line

将脚本放在名为script.awk的文件中,并按以下方式运行:

$ ./script.awk aa.txt 32bm 2fok
  253  143 B R              0   0   96      0, 0.0    -2,-3.7     0, 0.0     2,-0.2   0.000 360.0 360.0 360.0 110.4   38.4   10.4    3.0
  254  144 B Q  B     -Z  250   0E  62     -4,-0.3    -4,-0.2    -3,-0.1     2,-0.1  -0.347 360.0-157.5 -58.1 119.5   39.4   13.6    4.8
  255  145 B T        -     0   0   22     -6,-1.4     2,-0.3    -2,-0.2    -7,-0.2  -0.396   7.8-127.4 -91.5 173.9   36.3   15.7    5.4

    1  361 X G              0   0  137      0, 0.0     2,-0.2     0, 0.0     3,-0.0   0.000 360.0 360.0 360.0  97.3   25.2  -16.6   -6.6
    2  362 X A        -     0   0   98      1,-0.0     0, 0.0     0, 0.0     0, 0.0  -0.649 360.0 -33.9-148.3  84.1   28.0  -18.6   -4.8
    3  363 X R        -     0   0  226     -2,-0.2     2,-0.0     1,-0.1    -1,-0.0   1.000  68.7-149.8  66.4  76.9   31.1  -16.5   -4.0

或者如果您更喜欢一个班轮:

awk 'NR==FNR{low[$1,$2]=$3;hi[$1,$2]=$4;next}$2>=low[FILENAME,$3]&&$2<=hi[FILENAME,$3]' aa.txt 32bm 2fok