在bash脚本中使用awk实现vlookup功能
以下是输入: 文件1.csv 文件2.csv 预期产出: 文件3.csv 目前正在尝试使用join在bash脚本中使用awk实现vlookup功能,awk,vlookup,Awk,Vlookup,以下是输入: 文件1.csv 文件2.csv 预期产出: 文件3.csv 目前正在尝试使用join join -a1 -t '|' -1 1 -2 1 -o 1.1,2.2,1.2,1.3 file1.csv file2.csv > file3.csv 但它发现一些行没有匹配,所以我将我的概念转变为对这两个文件使用最有可能的vlookup功能。请帮忙 谢谢大家请您尝试使用GNUawk编写和测试的awk并显示样本 awk ' BEGIN{ FS=OFS="|" }
join -a1 -t '|' -1 1 -2 1 -o 1.1,2.2,1.2,1.3 file1.csv file2.csv > file3.csv
但它发现一些行没有匹配,所以我将我的概念转变为对这两个文件使用最有可能的vlookup功能。请帮忙
谢谢大家请您尝试使用GNU
awk
编写和测试的awk
并显示样本
awk '
BEGIN{
FS=OFS="|"
}
FNR==NR{
arr[$1]=$2
next
}
($1 in arr){
$1=($1 OFS arr[$1])
}
1
' file2.csv file1.csv
说明:添加上述内容的详细说明
awk ' ##Starting awk program from here.
BEGIN{ ##Starting BEGIN section from here of this program.
FS=OFS="|" ##Setting | as field separator and output field separator.
}
FNR==NR{ ##Checking condition if FNR==NR which will be TRUE when file2.csv is being read.
arr[$1]=$2 ##Creating arr with index of 1st field and value of 2nd field.
next ##next will skip all further statements from here.
}
($1 in arr){ ##checking condition if $1 is present in arr then do following.
$1=($1 OFS arr[$1]) ##Saving current $1 OFS and value of arr with index of $1 in $1.
}
1 ##1 will print the current line.
' file2.csv file1.csv ##Mentioning Input_file names here.
我测试了您提供的
join
命令,我认为它在我的机器上产生了预期的输出(FreeBSD 12.2-RELEASE):
您可能需要首先对两个文件的要连接的列进行排序(或者在这种情况下,如果您连接了第一列,那么整行也应该工作),即
join-a1-t'|'-11-21-o1.1,2.2,1.2,1.3,似乎预期输出中缺少包含ddd
的行。不确定这是不是故意的?否则,您的join
命令似乎可以工作…在侧节点上,它是files1
和files3
,但仅是file2
。
join -a1 -t '|' -1 1 -2 1 -o 1.1,2.2,1.2,1.3 file1.csv file2.csv > file3.csv
awk '
BEGIN{
FS=OFS="|"
}
FNR==NR{
arr[$1]=$2
next
}
($1 in arr){
$1=($1 OFS arr[$1])
}
1
' file2.csv file1.csv
awk ' ##Starting awk program from here.
BEGIN{ ##Starting BEGIN section from here of this program.
FS=OFS="|" ##Setting | as field separator and output field separator.
}
FNR==NR{ ##Checking condition if FNR==NR which will be TRUE when file2.csv is being read.
arr[$1]=$2 ##Creating arr with index of 1st field and value of 2nd field.
next ##next will skip all further statements from here.
}
($1 in arr){ ##checking condition if $1 is present in arr then do following.
$1=($1 OFS arr[$1]) ##Saving current $1 OFS and value of arr with index of $1 in $1.
}
1 ##1 will print the current line.
' file2.csv file1.csv ##Mentioning Input_file names here.
21|A800|AAAAA|1023
21|A800|BBBBB|1203
21|A800|CCCCC|2533
22|B900|DDDDD|1294
22|B900|EEEEE|1249
22|B900|FFFFF|4129
22A|C1000|GGGGG|4121
22A|C1000|HHHHH|1284
31B|D1000|IIIII|5403
31B|D1000|JJJJJ|1249