我有一个巨大的文本文件,如下所示:
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