猫文件1
487
cat file2
4.999286 12.669064 0.0000005.999343 12.753258 0.0000006.999401 12.654514 0.0000007.999458 12.774485 0.0000008.999515 12.662147 0.0000009.999572 12.700071 0.00000010.999629 12.717721 0.000000
我希望以相同的顺序和另外两个连续的行来grep file1。我试过了:
grep -A 2 -Ff file1 file2
我想要这样的输出:
4.999286 12.669064 0.0000005.999343 12.753258 0.0000006.999401 12.654514 0.0000008.999515 12.662147 0.0000009.999572 12.700071 0.00000010.999629 12.717721 0.0000007.999458 12.774485 0.0000008.999515 12.662147 0.0000009.999572 12.700071 0.000000
grep
快速进行少量文件扫描
$ while IFS= read -r n; do grep -A2 "^$n." file2; done < file1
4.999286 12.669064 0.000000
5.999343 12.753258 0.000000
6.999401 12.654514 0.000000
8.999515 12.662147 0.000000
9.999572 12.700071 0.000000
10.999629 12.717721 0.000000
7.999458 12.774485 0.000000
8.999515 12.662147 0.000000
9.999572 12.700071 0.000000
[请您尝试以下操作,这将以与$1
在Input_file1中出现的相同顺序给出结果。
awk '
BEGIN{
s1="\""
}
FNR==NR{
a[$0]
next
}
(int($1) in a){
system("grep -A2 " s1 $0 s1 OFS FILENAME)
}
' Input_file1 Input_file2
输出将如下。
4.999286 12.669064 0.000000
5.999343 12.753258 0.000000
6.999401 12.654514 0.000000
7.999458 12.774485 0.000000
8.999515 12.662147 0.000000
9.999572 12.700071 0.000000
8.999515 12.662147 0.000000
9.999572 12.700071 0.000000
10.999629 12.717721 0.000000