Text-Processing
從另一個文件替換 csv 文件中的列值
我有一個包含 85 個欄位的 csv 文件。我想用另一個文件中的數據替換第 52 列的值。第二個文件僅包含 1 列,並且與第一個文件中的記錄數相同。
例如
data.CSV
文件(第一個 csv 文件)0,126,,2,0,904CEE,0,0,1,0,0,,7638.raw,0,0,20210515,111937,10,0,540,540,0,,,,,,,,,,,0,,,,,,,,,,,,,0,,,07822000655,,,**07822000656**,0,,,,0B020D,358605075357339 ,234307822000655,11,,01,00,0,,,0,2,1,0,1101,,1,0,23430,,,11,5,,0A03,,,0, 0,126,,2,0,904CEE,0,0,1,0,0,,7638.raw,0,0,20210515,111938,10,0,540,540,0,,,,,,,,,,,0,,,,,,,,,,,,,0,,,07822000655,,,**07822000656**,0,,,,0B020D,358605075357339 ,234307822000655,11,,01,00,0,,,0,2,1,0,1101,,1,0,23430,,,11,5,,0A03,,,0, 0,126,,2,0,904CEE,0,0,1,0,0,,7638.raw,0,0,20210515,111939,10,0,540,540,0,,,,,,,,,,,0,,,,,,,,,,,,,0,,,07822000655,,,**07822000656**,0,,,,0B020D,358605075357339 ,234307822000655,11,,01,00,0,,,0,2,1,0,1101,,1,0,23430,,,11,5,,0A03,,,0, 0,126,,2,0,904CEE,0,0,1,0,0,,7638.raw,0,0,20210515,111939,10,0,540,540,0,,,,,,,,,,,0,,,,,,,,,,,,,0,,,07822000655,,,**07822000656**,0,,,,0B020D,358605075357339 ,234307822000655,11,,01,00,0,,,0,2,1,0,1101,,1,0,23430,,,11,5,,0A03,,,0, 0,126,,2,0,904CEE,0,0,1,0,0,,7638.raw,0,0,20210515,111939,10,0,540,540,0,,,,,,,,,,,0,,,,,,,,,,,,,0,,,07822000655,,,**07822000656**,0,,,,0B020D,358605075357339 ,234307822000655,11,,01,00,0,,,0,2,1,0,1101,,1,0,23430,,,11,5,,0A03,,,0, 0,126,,2,0,904CEE,0,0,1,0,0,,7638.raw,0,0,20210515,111939,10,0,540,540,0,,,,,,,,,,,0,,,,,,,,,,,,,0,,,07822000655,,,**07822000656**,0,,,,0B020D,358605075357339 ,234307822000655,11,,01,00,0,,,0,2,1,0,1101,,1,0,23430,,,11,5,,0A03,,,0,
第二個文件(僅包含 1 列)
6228205 6225214 6225211 6225206 5206 87777
我想換
07822000656
來自第一個文件 ( ) 的第 52 列值 ( ) 與文件data.csv
中的第一6228205
行data.csv
- 第 52 列值 (
07822000656
) 與文件6225214
中的第 2 行data.csv
- 第 52 列值 (
07822000656
) 與6225211
第 3 行…等等…
所以輸出應該是
0,126,,2,0,904CEE,0,0,1,0,0,,7638.raw,0,0,20210515,111937,10,0,540,540,0,,,,,,,,,,,0,,,,,,,,,,,,,0,,,07822000655,,,**6228205**,0,,,,0B020D,358605075357339 ,234307822000655,11,,01,00,0,,,0,2,1,0,1101,,1,0,23430,,,11,5,,0A03,,,0, 0,126,,2,0,904CEE,0,0,1,0,0,,7638.raw,0,0,20210515,111938,10,0,540,540,0,,,,,,,,,,,0,,,,,,,,,,,,,0,,,07822000655,,,**6225214**,0,,,,0B020D,358605075357339 ,234307822000655,11,,01,00,0,,,0,2,1,0,1101,,1,0,23430,,,11,5,,0A03,,,0, 0,126,,2,0,904CEE,0,0,1,0,0,,7638.raw,0,0,20210515,111939,10,0,540,540,0,,,,,,,,,,,0,,,,,,,,,,,,,0,,,07822000655,,,**6225211**,0,,,,0B020D,358605075357339 ,234307822000655,11,,01,00,0,,,0,2,1,0,1101,,1,0,23430,,,11,5,,0A03,,,0, 0,126,,2,0,904CEE,0,0,1,0,0,,7638.raw,0,0,20210515,111939,10,0,540,540,0,,,,,,,,,,,0,,,,,,,,,,,,,0,,,07822000655,,,**6225206**,0,,,,0B020D,358605075357339 ,234307822000655,11,,01,00,0,,,0,2,1,0,1101,,1,0,23430,,,11,5,,0A03,,,0, 0,126,,2,0,904CEE,0,0,1,0,0,,7638.raw,0,0,20210515,111939,10,0,540,540,0,,,,,,,,,,,0,,,,,,,,,,,,,0,,,07822000655,,,**5206**,0,,,,0B020D,358605075357339 ,234307822000655,11,,01,00,0,,,0,2,1,0,1101,,1,0,23430,,,11,5,,0A03,,,0, 0,126,,2,0,904CEE,0,0,1,0,0,,7638.raw,0,0,20210515,111939,10,0,540,540,0,,,,,,,,,,,0,,,,,,,,,,,,,0,,,07822000655,,,**87777**,0,,,,0B020D,358605075357339 ,234307822000655,11,,01,00,0,,,0,2,1,0,1101,,1,0,23430,,,11,5,,0A03,,,0,
我設法做到瞭如下:
awk -F , '{$1, $2, $3, $4...$51}' data.csv >temp1.csv awk -F , '{$53, $54, $55....$85}' data.csv >temp2.csv paste -d "," temp1.csv 2nd_file temp2.csv
但是,我正在尋找一種更好的方法來處理這個問題
您可以使用
awk
在第二個文件上建構條目映射,以行號為鍵並替換第一個文件上的值,awk -v FS=, -v OFS=, 'FNR==NR{hash[FNR]=$0; next}{$52 = hash[FNR]}1' file2 file1
你標記了這個問題/awk,但是使用類似的工具
awk
會在 csv 文件帶有類似的欄位時失敗"embed , in a string"
,所以最好使用為它設計的工具,甚至讓它變得非常簡單:csvtool pastecol 52 1 data.CSV value.CSV
這將第 52
data.CSV
列替換為第 1 列value.CSV