[英]Replacing last characters after last comma with a string
我有一個巨大的文本文件,看起來像這樣:
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 解決方案。)有有趣的答案。
如果您喜歡 PS,這里有一個 PowerShell 答案。
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
下一個代碼執行您想要的操作,除了在小於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
這是一個cmd
批處理文件,它依賴於一個不錯的 hack 來拆分逗號分隔列表的最后一項,而與字符串中出現的逗號數量無關。 基本技術如下所示; 請注意,這需要啟用延遲擴展:
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
的子例程中:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_FILE=%~1" & rem // (specify the CSV file by the first argument)
for /F "usebackq delims=" %%L in ("%_FILE%") do (
call :GET_LAST_ITEM LAST REST "%%L"
setlocal EnableDelayedExpansion
set "LAST=0!LAST!"
echo(!REST!,MI-!LAST:~-2!
endlocal
)
endlocal
exit /B
:GET_LAST_ITEM rtn_last rtn_without_last val_string
::This function splits off the last comma-separated item of a string.
::Note that exclamation marks must not occur within the given string.
::PARAMETERS:
:: rtn_last variable to receive the last item
:: rtn_without_last variable to receive the remaining string
:: val_string original string
setlocal EnableDelayedExpansion
set "STR=,%~3"
set "PRE=" & set "END=%STR:,=" & set "PRE=!PRE!,!END!" & set "END=%"
endlocal & set "%~1=%END%" & set "%~2=%PRE:~2%"
exit /B
這就是答案@RomanPerekhrest我的交叉問題提供了(我也求UNIX / perl的解決方案), 在這里:
帶有 sprintf 函數的 awk 方法(添加前導零):
awk -F, -v OFS=',' '$8="MI-"sprintf("%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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.