使用AWK比较两个不同的文件,并将值从一个文件复制到其他文件
大家好,我需要你的帮助。我有两个文件:
file1.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
file2.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"
输出file1.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
我需要检查booth文件(file1.csv,file2.csv)中的用户名,并将每个用户名的“expired-date”值从“file2.csv”复制到“file1.csv”。我希望可以用AWK或类似的命令来完成。 THX。
答案 0 :(得分:2)
检查这个awk单行:(标题被省略,它是固定的,很容易添加)
kent$ awk -F, -v OFS="," 'NR==FNR{d[$2]=$4;next}$2 in d{$5=d[$2]}7' file2 file1