gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:23:42 26 4
gpt4 key购买 nike

使用 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

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"

输出文件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

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

最佳答案

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

kent$  awk -F, -v OFS="," 'NR==FNR{d[$2]=$4;next}$2 in d{$5=d[$2]}7' file2 file1

关于linux - 使用 AWK 比较两个不同的文件并将值从一个文件复制到另一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16913235/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com