Replacing last characters after last comma with a string

前端 未结 4 1142
庸人自扰
庸人自扰 2021-01-27 23:24

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.8344975984         


        
4条回答
  •  耶瑟儿~
    2021-01-27 23:51

    The next code does what you want except for filling with zero the last token when is less than 10, hope it helps.

    EDIT: I figured out a way to insert a leading zero when the last number is less than 10. A little bit ugly but does it. :)

    @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
    

    For file (f.txt in this case):

    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
    

    Produces the following result (also in f.txt): updated

    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
    

提交回复
热议问题