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

Sed 基于另一个文件从文件中删除特定行,sed,awk,Sed,Awk,我在一个名为ff的文件夹中有一些文本文件,如下所示。我需要根据另一个文件aa.txt删除这些文件中的行 32bm.txt: 2fok.txt: aa.txt: 例如,在32bm.txt中,我只需要具有B(第3列)和143到145(第2列)的数字的行 期望输出: 32bm.txt 2fok.txt 使用awk,您可以执行以下操作: #!/usr/bin/awk -f NR == FNR { # If we are in the first file low[$1,$2]=$3 # stor

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

32bm.txt:

2fok.txt:

aa.txt:

例如,在32bm.txt中,我只需要具有B(第3列)和143到145(第2列)的数字的行

期望输出:

32bm.txt

2fok.txt


使用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
或者,如果您更喜欢单衬里:

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

awk'NR==FNR{low[$1,$2]=$3;hi[$1,$2]=$4;next}$2>=low[FILENAME,$3]&&&$2多么棒的解决方案!你能解释一下这个命令吗?添加了一些注释。如果还有什么不清楚的地方,请告诉我。非常好的回答。@user000001谢谢你的回答。我试过你的密码。我收到错误bash:./script.awk:权限被拒绝。如何解决此错误?@user
chmod+x script.awk
32bm    B   143 145
2fok    X   361 363
2moj    B   361 367
-
-
-
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
#!/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 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