Working on a batch script that will modify package.json file.
Basically, I need to replace the "test"
portion of every json file.
This is a sample snippet:
"scripts": {
"test": "any text can go here"
},
What I want to do is replace the entire "test" line with:
"test": "jest --coverage"
I have only come up with a script that replaces a string (without spaces):
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=" %%a in (temp.txt) do (
SET s=%%a
SET s=!s:test=newText!
echo !s!>>yourtempfilename.txt
)
pause
Can anyone help me modify this so that: - it can accommodate a string with spaces - it can replace the entire line with "test"
considering "any text can go here"
value is always different
Please note though that I cannot use any other programs/commands such as sed or fnr.bat or something.
As Compo already commented, batch isn't the right tool for the job, but when you can't use anything other (like PowerShell):
@echo off
setlocal disabledelayedexpansion
set "newtext=some other text here"
(for /f "tokens=1* delims=]" %%a in ('find /n /v "" temp.txt') do (
echo %%b|findstr /rc:"\ *\"test\":\ \".*\"" >nul && (
for /f "delims=:" %%c in ("%%b") do echo %%c: "%newtext%"
) || echo/%%b
))>yourtempfilename.txt 2>nul
It keeps formatting and empty lines but has some problems with certain poison characters (that's why 2>nul
; the output is ok though)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.