鉈

我有一個巨大的文本文件,如下所示:

36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,3
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,8
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,12

期望的輸出是這樣的:

36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12

我在這裡和其他社區嘗試過其他相關帖子,但無法完全獲得我想要的。

更新

這是交叉問題(我想要 Unix/perl 答案和批次/powershell 解決方案。)有有趣的答案。

答案1

awk接近與衝刺函數函數(新增前導零):

awk -F, -v OFS=',' '$8=sprintf("MI-%02d",$8);' file

輸出:

36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12

-F,- 設定逗號,作為欄位分隔符

$8- 指向第八個字段

%02d- 將函數參數視為的格式2-數字


筆記,記錄中的最後一個欄位可以透過 呈現$NF

核因子是一個預定義變量,其值為目前記錄中的字段數

所以,與(對於您的輸入)$NF相同$8

awk -F, -v OFS=',' '$(NF)=sprintf("MI-%02d", $(NF))' file

答案2

您可以嘗試使用awk

awk 'BEGIN { FS = OFS = "," } { $NF = sprintf("MI-%02d", $NF); } 1' file

答案3

這是 perl 解決方案:

$ perl -F',' -lane '$last=$#F;$F[$last]=sprintf("MI-%02d",$F[$last]);print join ",", @F' input.txt                                       
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12

-a標誌允許我們根據 指定的分隔符號將輸入視為數組-F。基本上我們改變該數組中的最後一項,並透過join命令重建它。

答案4

這是我的解決方案,使用 Tcl 完成,它從 input.csv 文件讀取並將結果放入 output.csv 文件

set in [open input.csv]
set out [open output.csv w]

while {![eof $in]} {
   set line [gets $in]
   set last_comma_pos [string last , $line]
   puts $out [string range $line 0 $last_comma_pos][format MI-%02d [string range $line $last_comma_pos+1 end]]
}

close $in
close $out

示範

相關內容