1

2 つの異なるファイルを AWK で比較し、あるファイルから別のファイルに値をコピーする

こんにちは、あなたの助けが必要です。私は2つのファイルを持っています:

ファイル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

ブース ファイル (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 に答える