1

使用 AWK 比较两个不同的文件并将值从一个文件复制到另一个文件

大家好,我需要你的帮助。我有两个文件:

文件 1.csv:

"count","username","start-date","status","expire-date"
"000001","TEST1","2013-05-07-16:02:07","ACTIVE","2014-05-07-16:02:07"
"000002","TEST2","2013-05-08-16:02:07","ACTIVE","2014-05-08-16:02:07"
"000003","TEST3","2013-05-09-16:02:07","ACTIVE","2015-05-09-16:02:07"
"000004","TEST4","2013-05-10-16:02:07","ACTIVE","2017-05-10-16:02:07"
"000005","TEST5","2013-05-11-12:02:07","ACTIVE","2013-08-11-12:02:07"
"000006","TEST6","2013-05-12-16:02:07","ACTIVE","2018-05-12-16:02:07"
"000007","TEST7","2013-05-13-18:05:07","ACTIVE","2016-05-13-18:05:07"
"000008","TEST8","2013-05-06-16:02:07","ACTIVE","2099-05-06-16:02:07"
"000009","TEST9","2013-05-07-13:05:07","ACTIVE","2019-05-07-13:05:07"
"000010","TEST10","2013-05-07-15:02:07","ACTIVE","2027-05-07-15:02:07

文件 2.csv:

"id","username","start-date","expire-date"<br>
"x01","TEST1","2010-05-07-16:02:07","2088-05-07-16:02:07"
"x02","TEST2","2010-05-08-16:02:07","2099-05-08-16:02:07"
"x03","TEST3","2010-05-09-16:02:07","2077-05-09-16:02:07"
"x04","TEST8","2013-05-06-16:02:07","2066-01-01-00:00:00"
"x05","TEST9","2013-05-07-13:05:07","2025-05-07-00:05:07"

输出文件1.csv:

"count","username","start-date","status","expire-date"
"000001","TEST1","2013-05-07-16:02:07","ACTIVE","2088-05-07-16:02:07"
"000002","TEST2","2013-05-08-16:02:07","ACTIVE","2099-05-08-16:02:07"
"000003","TEST3","2013-05-09-16:02:07","ACTIVE","2077-05-09-16:02:07"
"000004","TEST4","2013-05-10-16:02:07","ACTIVE","2017-05-10-16:02:07"
"000005","TEST5","2013-05-11-12:02:07","ACTIVE","2013-08-11-12:02:07"
"000006","TEST6","2013-05-12-16:02:07","ACTIVE","2018-05-12-16:02:07"
"000007","TEST7","2013-05-13-18:05:07","ACTIVE","2016-05-13-18:05:07"
"000008","TEST8","2013-05-06-16:02:07","ACTIVE","2066-01-01-00:00:00"
"000009","TEST9","2013-05-07-13:05:07","ACTIVE","2025-05-07-00:05:07"
"000010","TEST10","2013-05-07-15:02:07","ACTIVE","2027-05-07-15:02:07

我需要检查展位文件(file1.csv、file2.csv)中的用户名,并将每个用户名的“过期日期”值从“file2.csv”复制到“file1.csv”。我希望这可以通过 AWK 或一些类似的命令来完成。谢谢。

4

1 回答 1

2

检查这个 awk 单行:(省略了标题,它是固定的,并且易于添加)

kent$  awk -F, -v OFS="," 'NR==FNR{d[$2]=$4;next}$2 in d{$5=d[$2]}7' file2 file1
于 2013-06-04T08:49:54.160 回答