用字符串替换最后一个逗号后的最后一个字符 [英] Replacing last characters after last comma with a string

查看:386
本文介绍了用字符串替换最后一个逗号后的最后一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a huge text file which look like this:

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

我曾在这里和其他社区尝试过其他相关职位,但无法完全得到我想要的东西。

I have tried other relevant posts here and on other communities but could not exactly get what I want.

更新

这是交叉问题(为此,我既需要Unix / perl答案,又需要批处理/ powershell解决方案。 )的答案。

This is the cross-question (I wanted both Unix/perl answers and batch/powershell solutions for this.) that has interesting answers.

推荐答案

这是PowerShell的答案,以防您喜欢PS。

Here's a PowerShell answer in case you like PS.

Get-Content C:\Path\To\File.csv | 
    Where{$_ -match '^(.*,)([^,]*)$'} | 
    ForEach { "{0}MI-{1}" -f $Matches[1], $Matches[2].Padleft(2,'0') } |
    Set-Content C:\Path\To\NewFile.csv

这篇关于用字符串替换最后一个逗号后的最后一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆