- 论坛徽章:
- 0
|
想用AWK实现EXCEL的VLOOUP功能,test1.csv和test2.csv文件内容如下
[root@localhost test]# cat test2.csv
aaa|111
bbb|222
[root@localhost test]# cat test1.csv
aaa|a|a1
bbb|b|a2
ccc|c|c1
需要的效果:
aaa|111|a|a1
bbb|222|b|a2
ccc||c|c1
用AWK命令输出的却是下面这种效果,不知道哪里出问题了。
[root@localhost test]# awk -F "|" -vOFS="|" '{if(NR==FNR){if($1!="" && $2!=""){eci[$1]=$2}}else{if($1 in eci){$1=$1"|"eci[$1]}else{$1=$2"|"} {print $0}}}' test2.csv test1.csv
|a|a111
|b|a222
c||c|c1
单独打印$1、$2、$3没有问题,如果打印$0就出问题
[root@localhost test]# awk -F "|" -vOFS="|" '{if(NR==FNR){if($1!="" && $2!=""){eci[$1]=$2}}else{if($1 in eci){$1=$1"|"eci[$1]}else{$1=$2"|"} {print $1}}}' test2.csv test1.csv
aaa|111
bbb|222
c|
[root@localhost test]# awk -F "|" -vOFS="|" '{if(NR==FNR){if($1!="" && $2!=""){eci[$1]=$2}}else{if($1 in eci){$1=$1"|"eci[$1]}else{$1=$2"|"} {print $2}}}' test2.csv test1.csv
a
b
c
[root@localhost test]# awk -F "|" -vOFS="|" '{if(NR==FNR){if($1!="" && $2!=""){eci[$1]=$2}}else{if($1 in eci){$1=$1"|"eci[$1]}else{$1=$2"|"} {print $3}}}' test2.csv test1.csv
a1
a2
c1
|
|