用字符串替换最后一个逗号后的最后一个字符

时间:2017-04-13 18:59:26

标签: powershell text cmd

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

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

我在这里以及其他社区尝试了其他相关帖子,但无法完全得到我想要的内容。

更新

这是cross-question(我想要Unix / perl答案和批处理/ powershell解决方案。)这些答案很有趣。

4 个答案:

答案 0 :(得分:2)

如果你喜欢PS,这是一个PowerShell答案。

// Calculate Form Fields

$("input.price").on("keyup", function(){
  $(this).siblings("input.total").val((

    $(this).val() * $(this).siblings("input.qty").val()
  ).toFixed(2));
});

$("input.qty").on("keyup", function(){

  $(this).siblings("input.total").val((

    $(this).val() * $(this).siblings("input.price").val()
  ).toFixed(2));
});

答案 1 :(得分:1)

下一个代码会执行您想要的操作,除非在小于10时填充零最后一个标记,希望它有所帮助。

编辑:当最后一个数字小于10时,我想出了一种方法来插入一个前导零点。有点难看,但它确实如此。 :)

@echo off

setlocal EnableDelayedExpansion

for /F "delims=, tokens=1-8" %%A in (f.txt) do (
    set /a "t=%%H-10"
    if "!t:~0,1!" equ "-" (set "n=0%%H") else (set "n=%%H")
    echo(%%A,%%B,%%C,%%D,%%E,%%F,%%G,MI-!n!>>f.new.txt
)

move /Y f.new.txt f.txt >nul 2>&1

对于文件(在本例中为f.txt):

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

产生以下结果(也在f.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

答案 2 :(得分:1)

这是一个GET https://outlook.office.com/api/v1.0/me/MailFolders/INBOX/messages GET https://outlook.office.com/api/v1.0/me/MailFolders/SentItems/messages 批处理文件,依赖于一个很好的黑客来分割逗号分隔列表的最后一项,与字符串中出现的逗号数量无关。基本技术如下所示;请注意,这需要启用delayed expansion

cmd

以下是脚本的代码,将上述方法保存在名为set "x=This,is,the,original,list." set "y=" & set "z=%x:,=" & set "y=!y!,!z!" & set "z=%" & set "y=!y:~1!" echo ORIGINAL: %x% echo LAST ITEM: %z% echo REMAINDER: %y% 的子例程中:

:GET_LAST_ITEM

答案 3 :(得分:0)

这是@RomanPerekhrest在我的交叉问题中提供的答案(寻求unix / perl解决方案)here

  

使用sprintf函数进行aw​​k方法(添加前导零):

get '/', to: redirect('https://www.openhub.net/'), constraints: { subdomain: 'code' }
     

输出:

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