Text-Processing

從兩個文件中提取數據

  • October 17, 2013

我有兩個文件,如下所示

輸出1.csv

201319107648361,12:27:04,12:27:14,0:0:10 secs
201319109089878,12:27:04,12:27:15,0:0:11 secs
201319108968961,12:27:15,12:27:54,0:0:9 secs
201319108686853,12:27:34,12:27:43,0:0:9 secs
.
.
.
n

輸出2.csv

201319107648361,200
201319109089878,129
201319108968961,719
201319108686853,412
.
.
.
n

我需要將這兩個文件合併到一個具有一個條件的文件中,即如果兩個文件的第一個欄位相同。

例如,

如果 Output1.csv - 201319107648361的第一個值或欄位等於 Output2.csv 的第一個欄位,則需要將文件的剩餘欄位列印到另一個文件中。

所需的輸出文件應包含:

201319107648361,12:27:04,12:27:14,0:0:10 secs,200
201319109089878,12:27:04,12:27:15,0:0:11 secs,129
201319108968961,12:27:15,12:27:54,0:0:9 secs,719
201319108686853,12:27:34,12:27:43,0:0:9 secs,412
.
.
.
n

所有你需要的是join

join -t\, <(sort Output1.csv) <(sort Output2.csv)
-or-
join -t "," <(sort Output1.csv) <(sort Output2.csv)

或者awk

awk -F, 'FNR==NR{a[$1]=$2;next}{ print $0 "," a[$1]}' Output2.csv Output1.csv

引用自:https://unix.stackexchange.com/questions/96474