[英]How to extract fields from 2 different files and store in an output file using shell script?
I found the below command works. 我发现以下命令有效。 But could someone explain me what it actually does?
但是有人可以解释一下它的实际作用吗?
awk 'NR==FNR{a[NR]=$2; next} {print a[FNR], $2}' file1 file2
It extracts the fields passed in the command from the files and display it in the terminal. 它从文件中提取命令中传递的字段,并将其显示在终端中。
awk 'NR==FNR{a[NR]=$2; next} {print a[FNR], $2}' file1 file2
As NR
stands for Number of Record and FNR
for Record Number in the current input File, they are equal when processing the first file. 由于
NR
代表当前输入文件中的记录号和FNR
代表记录号,所以在处理第一个文件时它们是相等的。 Hence, awk 'NR==FNR { things1; next } { things2 }' file1 file2
因此,
awk 'NR==FNR { things1; next } { things2 }' file1 file2
awk 'NR==FNR { things1; next } { things2 }' file1 file2
means: awk 'NR==FNR { things1; next } { things2 }' file1 file2
意思是:
things1
when processing the first file. things1
1。 things2
when processing the second file. things2
。 a[NR]=$2; next
a[NR]=$2; next
means: a[NR]=$2; next
意思是:
a[]
with the index being the number of record (number of line, generally, like in this case). a[]
,索引为记录数(通常为行数,在这种情况下为行数)。 print a[FNR], $2
means: print a[FNR], $2
表示:
This way, this will produce an output consisting in the 2nd field of both files, side by side. 这样,这将产生并排包含两个文件的第二个字段的输出。
$ cat f1
1 2 3
4 5 6
7 8 9
10 11 12
$ cat f2
a1 a2 a3
a4 a5 a6
a7 a8 a9
a10 a11 a12
$ awk 'NR==FNR{a[NR]=$2; next} {print a[FNR], $2}' f1 f2
2 a2
5 a5
8 a8
11 a11
You can find more information in Idiomatic awk . 您可以在Idiomatic awk中找到更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.