如何通过批处理脚本读取文件的最后一个字符并更新文件中的最后一个字符

如何通过批处理脚本读取文件的最后一个字符并更新文件中的最后一个字符

我有一个非常大的 json 格式文件,其中每条记录都以“}}}”结尾,最后一条记录也是如此,最后一条记录的格式为“}}}”,结尾处有“逗号(,)”和两个空格。现在我想要实现的是,最后一条记录应更新为“}}}”,最后一条记录不应有“逗号”和空格。

附上最后一张唱片的样本,我需要通过 Bach 脚本来完成。如能尽快提供帮助,我将不胜感激 !!!

    [{
{"availabilityDate":"2020-10-07","status":"ACT"},"organization":{"id":"TMO1","code":"TMO1","type":"SUP","name":"TMO Process & Data","subType":"DEP",
"status":"ACTIVE","availablityDate":"2020-10-07","orgLevelFromTop":0,"orgLevels":[{"level":"0","orgCode":"TMO1","orgName":"TMO Process & Data"}]},
}}},
{
{"availabilityDate":"2020-10-07","status":"ACT"},"organization":{"id":"TMO1","code":"TMO1","type":"SUP","name":"TMO Process & Data","subType":"DEP",
"status":"ACTIVE","availablityDate":"2020-10-07","orgLevelFromTop":0,"orgLevels":[{"level":"0","orgCode":"TMO1","orgName":"TMO Process & Data"}]},
"job":{"profileId":2423532,"titleOfPosition":"Data Steward","managementLevel":"MG1","familyId":"32532532","familyGroup":"JOB1","familyName":"Data Mgmt",
"familyStatus":"ACT","status":"ACT"},"unionMembership":{"id":"21424221","name":"CAN - WEST - CALGARY"}}},  
]

这是我尝试过的:-

@echo off
    setlocal enabledelayedexpansion
        for /F %%A in (%newfile) do (
        set mvar=%%A 
        set kvar=!mvar:~1,-1!
        echo !kvar!
        )

答案1

@echo off && cd /d "%~dp0"

setlocal enabledelayedexpansion && set /a "_line=0, _skip=0"
set "_json=Source_File.json" && set "_json_tmp=%temp%\Json_Temp.json"
for /f tokens^=2delims^=^: %%i in ('find/v /c "" "%_json%"')do set/a "_skip=%%i-1"

for /l %%L in (!_skip! -1 1)do for /f tokens^=*delims^=^\* %%A in ('more +%%~L ^<"!_json!"
')do set "_str=%%A" && cmd /v /c "echo=!_str:~-6!|findstr "}}},.."|find ",  "">nul && (
     set/a "_line=%%~L+1" && goto %:^)
    )

%:^)
for /f ^eol^=*^usebackq^tokens^=*delims^=^\* %%A in ("!_json!"
)do set _str=<nul & set "_str=%%A" & >nul set/a "_cnt+=1" && 2>nul (
     >>"!_json_tmp!" (if !_cnt! equ !_line! (echo=!_str:~,-3!
        )else echo=!_str!)
    )

type "!_json_tmp!" >"!_json!" && del/q "!_json_tmp!"
timeout /t -1|type "!_json!" && endlocal & goto :EOF

  • 观察:假设与 _ _file_script.bat位于同一文件夹中file.json

  • 基本上使用数字循环反转列表,它以负的方式操作,如果文件有 10 行,则循环将从 1 中的 10 -1 开始,从最后一行到第一行列出,然后另一个for /f循环检查行是否以需要删除的这些字符结尾:


1.输入文件所在的文件夹.json

... cd /d "%~dp0"

2.对于双for循环,您需要启用enabledelayedexpansion

setlocal enabledelayedexpansion ...

3.将变量的值定义为0,预防中止执行时可能出现的错误等等......

... set /a "_line=0, _skip=0"

rem :: is the same ::
set _line=0
set _skip=0

4.编辑并添加文件名,或者最好是完整路径

set "_json=Source_File.json" ...

5.仅临时使用的路径和文件名将收到相关版本:

set "_json_tmp=%temp%\Json_Temp.json" ...

6.计算文件中的行数,并设置(-1)以在当前循环中按照从最后一行到第一行的顺序开始搜索最后一次出现的行}}},,搜索最后一行、倒数第二行,依此类推:

for /f tokens^=2delims^=^: %%i in ('find/v /c "" "%_json%"')do set /a "_skip=%%i-1"

7.使用for /L从总数开始的循环(从总行数 -1 开始),允许您从最后一行滚动到第一行,已经使用 with for /F,按上述顺序跳过这些行,也可以使用和定义变量中的最后一行(“时间”):[more SKIP %%L]_str

for /l %%L in (!_skip! -1 1)do for /f tokens^=*delims^=^\* %%A in ('more +%%~L ^<"!_json!"
')do set "_str=%%A"...

8.一旦_str包含当前行,按照文件中列出的行的相反顺序,仅使用最后 6 位数字来检查要从行中删除的字符串的第一次出现,如果fidnstr+ find returns 0,则保存行出现(最后一个/行号),并添加+1到此行变量,以便立即离开当前循环以使用该行/编号,使用条件:label %:^)if | else

... cmd /v /c "echo=!_str:~-6!|findstr "}}},.."|find ",  "">nul && (
     set /a "_line=%%~L+1" && goto %:^)
    )

9.一旦进入标签,就会逐行保存在中_str,使用for /f,就像实现计数器(_cnt)一样,以便可以相应地过滤行......

%:^)
for /f ^eol^=*^usebackq^tokens^=*delims^=^\* %%A in ("!_json!"
)do set _str=<nul & set "_str=%%A" & >nul set/a "_cnt+=1"

10.对于此for /F循环中的每一行,if | else检查该行是否需要更改,并进行相应的回显,减去最后 3 位(echo=!_str:~,-3!)数字,如果该行不同,则回显当前行而不进行更改:

>>"!_json_tmp!" (if !_cnt! equ !_line! (echo=!_str:~,-3!
        )else echo=!_str!)

11.它们临时保存有/无更改的行,并在执行结束时覆盖原来的文件:

type "!_json_tmp!" >"!_json!" ...

12.删除临时文件,然后使用超时wait indefinitely for a key press.而是在执行完成后重定向到type源文件更新文件的命令,以便在屏幕上简要显示结果。

del/q "!_json_tmp!"
timeout /t -1|type "!_json!" && endlocal & goto :EOF

观察:如果你不想看到这个显示:

del/q "!_json_tmp!"
timeout /t -1|type "!_json!" &&  endlocal & goto :EOF


其他资源:

答案2

也许是这样的:

@echo off
SetLocal EnableDelayedExpansion

:: Put the path or name of old json if in same folder as batch here:
set OldJson=Old.json

:: Put the path or name of new json if in same folder as batch here:
set NewJson=New.json

For /f "usebackq delims=" %%a in ("%OldJson%") do (
echo %%a | find /i "}}}"
IF !errorlevel! EQU 0 set /a Occurances+=1
)

For /f "usebackq delims=" %%a in ("%OldJson%") do (
set "Line=%%a"
set "Line:&=^&"
set "Line:<=^<"
set "Line:>=^>"
set "Linechange=!Line:}}},  =}}}!"
echo %%a | find /i "}}}"
IF !errorlevel! EQU 0 set /a Counter+=1
IF not !Counter! EQU !Occurances! (echo !Line!>>"%NewJson%") else (echo !LineChange!>>"%NewJson%")
)

相关内容