解释有关合并csv文件的awk命令

时间:2014-12-01 17:27:40

标签: csv awk

我找到了与我awk command相同的人problem

awk -F, 'NR==FNR{a[$2]=$0;next}$2 in a{ print a[$2],$4, $5 }' OFS=, file1.csv file2.csv

我正在尝试修改它以适应我们的csv格式,但我很难理解它的作用。我很遗憾不得不在很短的时间内做到这一点,我希望你们能帮助我。

谢谢!

2 个答案:

答案 0 :(得分:3)

-F,

FS设置为,进行字段拆分。

NR==FNR{a[$2]=$0;next}

当前处理的行号(NR)等于当前文件的行号(FNR)(即处理第一个非空文件时)。将输入行存储到行{2}的第二个字段的键下的a数组中,并跳过处理下一行($2)。

next

当当前行的第二个字段($2 in a{ print a[$2],$4, $5 } )位于数组$2中时,请在此键a后面的数组中打印字段,后跟a[$2](逗号)后跟当前行的第4行(OFS),后跟$4,后跟当前行的第5行(OFS)。

$5

在处理输入文件之前将OFS=, 设置为OFS

tl; dr将,中的第4列和第5列附加到file2.csv的匹配行(基于字段2)。

答案 1 :(得分:3)

-F,          # Set the field separator to a comma

NR==FNR      # Test if we are looking the first file
             # NR is incremented for every line read across all input files
             # FNR is incremented for every line read in current file and resets to 0
             # The only time NR==FNR is when we are looking at the first file

a[$2]=$0     # Create a lookup for the line based on the value in the 2nd column

next         # Short circuit the script and get the next input line

$2 in a      # If we are here we are looking at the second file 
             # Check if we have seen the second field in the first file

a[$2],$4,$5  # Print the whole matching line from the first file
             # with the 4th & 5th fields from the second

OFS=,        # Separate the output with a comma