2014-01-20 37 views
2

我在一個名爲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 

回答

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 
+0

多麼真棒的解決方案!你能解釋一下這個命令嗎? – Bentoy13

+0

添加了一些評論。讓我知道如果有什麼還不清楚。 – user000001

+1

完美的,很好的答案。 – Bentoy13

相關問題