简体   繁体   English

如何使用Shell脚本从2个不同的文件中提取字段并将其存储在输出文件中?

[英]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意思是:

  • do things1 when processing the first file. 处理第一个文件时要做的things1 1。
  • do things2 when processing the second file. 处理第二个文件时做的things2

a[NR]=$2; next a[NR]=$2; next means: a[NR]=$2; next意思是:

  • store the 2nd field in the array 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表示:

  • print the corresponding stored field from the previous file together with the 2nd field from the current file. 打印前一个文件的相应存储字段以及当前文件的第二个字段。

This way, this will produce an output consisting in the 2nd field of both files, side by side. 这样,这将产生并排包含两个文件的第二个字段的输出。

Test 测试

$ 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.

相关问题 如何比较两个文件,一个有不同的字段,另一个是 ac 文件,使用 shell 脚本? - How to compare two files, one has different fields and other is a c file, using shell script? 如何从子目录提取文件名和路径,并需要使用unix shell脚本将输出写入到csv文件中 - How to extract the filename and path from sub directories and need to write the output into a csv file using unix shell script 使用traceroute输出中的shell脚本从文件中提取和查找最小值,最大值和平均值的脚本 - Script to extract and find min, max and avg from a file using a shell script from traceroute output 如何使用 shell 脚本从 xml 文件中提取值? - How to extract values from an xml file using shell script? 如何在Ubuntu中使用Shell脚本从文件中提取十进制值? - How to extract a decimal value from file using shell script in Ubuntu? 使用Shell脚本从文件中提取字符串 - extract string from a file using shell script 如何从bash / shell脚本中的java属性文件中提取值并存储在变量中并cd到该变量 - How to extract values from a java properties files in a bash/shell script and store in a variable and cd to that variable 如何从shell脚本中的文件中提取子字符串 - How to extract a substring from file in shell script 使用unix shell脚本将数字分割并存储在不同的文件中 - split numbers in and store them in different files using unix shell script Shell脚本:如何从字符串中按名称提取变量位置中的字段 - Shell Script : How to extract fields in variable positions by name from a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM