1

我有这个 2 csv 文件

旧的.csv

station,32145,80
station,32145,60

新的.csv

station,32145,80
station,32145,801

期待结果

结果.csv

station,32145,80,no change
station,32145,801,new
station,32145,60,Delete

我已经使用diffawk完成了这项工作,但我有一个小问题。该行没有更改或删除的行已正确更新,但新行未正确更新。任何人都可以告诉我我的错误在哪里?

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"}'

4

2 回答 2

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
于 2013-06-28T15:31:54.890 回答
2

的代码:


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
于 2013-06-28T16:03:47.147 回答