我有这个2 csv文件
old.csv
station,32145,80
station,32145,60
new.csv
station,32145,80
station,32145,801
期待结果
result.csv
station,32145,80,no change
station,32145,801,new
station,32145,60,Delete
我已经使用diff
和awk
来完成这项工作,但我有轻微的问题。该行没有更改或删除的一行正确更新,但新的行没有更新。任何人都可以告诉我我的错误在哪里?
diff -W999 --side-by-side old.csv new.csv |
awk '/[|][\t]/{split($0,a,"[|][\t]");print a[2]" No Change"};/[\t] *<$/{split($0,a,"[|][\t]* *<$");print a[1]" Delete"};/>[\t]/{split($0,a,">[\t]");print a[2]" New"}'
答案 0 :(得分:3)
这应该有效:
awk -F, '
NR==FNR && NF {a[$0","]++; next}
NF {print ($0 in a) ? $0"no change" : $0"new"; delete a[$0]}
END {for (x in a) print x"delete"}' old.csv new.csv
station,32145,80,no change
station,32145,801,new
station,32145,60,delete
.
awk 'BEGIN{FS=OFS=","}
NR==FNR {gsub(/[.]/,"",$2);a[$0","]++; next}
NF {gsub(/[.]/,"",$2); print ($0 in a) ? $0"no change" : $0"new"; delete a[$0]}
END {for (x in a) print x"delete"}' old.csv new.csv
答案 1 :(得分:2)
awk的代码:
new
没有逗号:
awk -v OFS="," 'NR==FNR {a[$0]=$0;next};{b[$0]=$0};$0==a[$0] {print $0, "no change"};a[$0]==0 {print $0, "new"};END {for (x in a) {if (b[x]==0) {print a[x], "Delete"}}}' old new
带尾随逗号的 new
:
$awk -v OFS="" 'NR==FNR {a[$0","]=$0",";next};{b[$0]=$0};$0==a[$0] {print $0, "no change"};a[$0]==0 {print $0, "new"};END {for (x in a) {if (b[x]==0) {print a[x], "Delete"}}}' old new