修改 For 循环以从某个字符串向前移动并使用批处理删除选定的行
Modify For loop to go from certain string forward and delete a selection of lines using batch
好的,所以我正在编写一个批处理文件,在其中我删除了在找到字符串后包含“,”的每一行:
“插件”:{
是否可以在 for 循环中设置此条件?
现在我知道你可以使用 ^ 避免引号,但我就是不能让它工作。
我现在做的是:
@echo OFF
::removePlugins
setlocal enabledelayedexpansion
echo. 2>tempPackage.json
SET @FOUND=0
for /F "delims=" %%G in (./package.json) do (
echo %%G|find "," > nul
if not errorlevel 1 (SET @FOUND=1)
if !@FOUND!==1(
@echo ON
SET @FOUND=0
ECHO: >> tempPackage.json
@echo OFF
)
ECHO %%G>>tempPackage.json
)
move "./tempPackage.json" ".package.json"
所以在这种情况下,我只会在包含“,”的每一行中换行。
那么如何编写一个只从这个字符串向前而不是换行而是删除它的for循环呢?
该文件包含如下内容:
{
"scripts"{ random scripts},
"dependencies"{ random dependencies},
"cordova": {
"platforms": [],
"plugins": {
"cordova-plugin-app-version": {},
"cordova-plugin-crosswalk-data-migration": {},
"cordova-plugin-customurlscheme": {
"URL_SCHEME": "scheme",
"ANDROID_SCHEME": " ",
"ANDROID_HOST": " ",
"ANDROID_PATHPREFIX": "/"
},
"cordova-plugin-device": {},
"cordova-plugin-inappbrowser": {},
"cordova-plugin-ionic-keyboard": {},
"cordova-plugin-ionic-webview": {
"ANDROID_SUPPORT_ANNOTATIONS_VERSION": "27.+"
},
"cordova-plugin-network-information": {},
"cordova-plugin-splashscreen": {},
"cordova-plugin-statusbar": {},
"cordova-plugin-whitelist": {},
"cordova-support-google-services": {},
"cordova.plugins.diagnostic": {},
"phonegap-plugin-barcodescanner": {
"CAMERA_USAGE_DESCRIPTION": "Used for osmething",
"ANDROID_SUPPORT_V4_VERSION": "27.+"
},
"phonegap-plugin-multidex": {},
"phonegap-plugin-push": {
"SENDER_ID": "id",
"ANDROID_SUPPORT_V13_VERSION": "27.+",
"FCM_VERSION": "16.0.+"
},
"im.ltdev.cordova.UserAgent": {}
}
}
}
运行批处理后的预期结果为:
{
"scripts"{ still the same scripts},
"dependencies"{ still the same dependencies},
"cordova": {
"platforms": [],
"plugins": {}
}
}
}
我尝试使用@Stephan 的代码,如下所示:
@echo OFF
::removePlugins
setlocal enabledelayedexpansion
SET @findPlugins=0
find /i /c "plugins" ./package.json >NUL
if not errorlevel 1 (SET @findPlugins=1)
IF !@findPlugins!==1 (
SET "@FOUND=1"
>tempPackage.json (
for /F "delims=" %%G in (./package.json) do (
echo %%G|findstr /b "}" >nul && SET "@FOUND=1"
if defined @FOUND ECHO %%G
echo %%G|findstr /b "\"plugins\": {" >nul && SET "@FOUND="
)
)
type tempPackage.json
)
它只是将 json 重写为 temp 并且没有删除任何内容...我做错了什么?
请记住,批处理无法解释 .json
文件并将它们作为纯文本处理。因此,任何批处理解决方案都高度依赖于文件的确切格式。格式的任何更改(杂散 space 可能就足够了)可能会导致垃圾。
也就是说:使用一个标志,该标志在以 "plugins":
开头的(每)行发生变化,并在命中以 }
开头的行(块的末尾)时变回并写入该行取决于标志:
@echo OFF
::removePlugins
setlocal
SET "@FLAG=1"
>tempPackage.json (
for /F "delims=" %%G in (./package.json) do (
echo %%G|findstr /e "[^{]}" >nul && SET "@FLAG=1"
if defined @FLAG ECHO %%G
echo %%G|findstr "\"plugins\":" >nul && SET "@FLAG="
)
)
type tempPackage.json
这使用正则表达式来捕获“插件”行之前的所有内容。
powershell -NoLogo -NoProfile -Command ^
Get-Content -Raw .\pi.txt ^| ^
ForEach-Object { if ($_ -match '(?sm)(.*\"plugins\": {).*') { $Matches[1] + \"`n`n}\" ^| Out-File -FilePath '.package.json'}}
好的,所以我正在编写一个批处理文件,在其中我删除了在找到字符串后包含“,”的每一行: “插件”:{ 是否可以在 for 循环中设置此条件?
现在我知道你可以使用 ^ 避免引号,但我就是不能让它工作。
我现在做的是:
@echo OFF
::removePlugins
setlocal enabledelayedexpansion
echo. 2>tempPackage.json
SET @FOUND=0
for /F "delims=" %%G in (./package.json) do (
echo %%G|find "," > nul
if not errorlevel 1 (SET @FOUND=1)
if !@FOUND!==1(
@echo ON
SET @FOUND=0
ECHO: >> tempPackage.json
@echo OFF
)
ECHO %%G>>tempPackage.json
)
move "./tempPackage.json" ".package.json"
所以在这种情况下,我只会在包含“,”的每一行中换行。 那么如何编写一个只从这个字符串向前而不是换行而是删除它的for循环呢?
该文件包含如下内容:
{
"scripts"{ random scripts},
"dependencies"{ random dependencies},
"cordova": {
"platforms": [],
"plugins": {
"cordova-plugin-app-version": {},
"cordova-plugin-crosswalk-data-migration": {},
"cordova-plugin-customurlscheme": {
"URL_SCHEME": "scheme",
"ANDROID_SCHEME": " ",
"ANDROID_HOST": " ",
"ANDROID_PATHPREFIX": "/"
},
"cordova-plugin-device": {},
"cordova-plugin-inappbrowser": {},
"cordova-plugin-ionic-keyboard": {},
"cordova-plugin-ionic-webview": {
"ANDROID_SUPPORT_ANNOTATIONS_VERSION": "27.+"
},
"cordova-plugin-network-information": {},
"cordova-plugin-splashscreen": {},
"cordova-plugin-statusbar": {},
"cordova-plugin-whitelist": {},
"cordova-support-google-services": {},
"cordova.plugins.diagnostic": {},
"phonegap-plugin-barcodescanner": {
"CAMERA_USAGE_DESCRIPTION": "Used for osmething",
"ANDROID_SUPPORT_V4_VERSION": "27.+"
},
"phonegap-plugin-multidex": {},
"phonegap-plugin-push": {
"SENDER_ID": "id",
"ANDROID_SUPPORT_V13_VERSION": "27.+",
"FCM_VERSION": "16.0.+"
},
"im.ltdev.cordova.UserAgent": {}
}
}
}
运行批处理后的预期结果为:
{
"scripts"{ still the same scripts},
"dependencies"{ still the same dependencies},
"cordova": {
"platforms": [],
"plugins": {}
}
}
}
我尝试使用@Stephan 的代码,如下所示:
@echo OFF
::removePlugins
setlocal enabledelayedexpansion
SET @findPlugins=0
find /i /c "plugins" ./package.json >NUL
if not errorlevel 1 (SET @findPlugins=1)
IF !@findPlugins!==1 (
SET "@FOUND=1"
>tempPackage.json (
for /F "delims=" %%G in (./package.json) do (
echo %%G|findstr /b "}" >nul && SET "@FOUND=1"
if defined @FOUND ECHO %%G
echo %%G|findstr /b "\"plugins\": {" >nul && SET "@FOUND="
)
)
type tempPackage.json
)
它只是将 json 重写为 temp 并且没有删除任何内容...我做错了什么?
请记住,批处理无法解释 .json
文件并将它们作为纯文本处理。因此,任何批处理解决方案都高度依赖于文件的确切格式。格式的任何更改(杂散 space 可能就足够了)可能会导致垃圾。
也就是说:使用一个标志,该标志在以 "plugins":
开头的(每)行发生变化,并在命中以 }
开头的行(块的末尾)时变回并写入该行取决于标志:
@echo OFF
::removePlugins
setlocal
SET "@FLAG=1"
>tempPackage.json (
for /F "delims=" %%G in (./package.json) do (
echo %%G|findstr /e "[^{]}" >nul && SET "@FLAG=1"
if defined @FLAG ECHO %%G
echo %%G|findstr "\"plugins\":" >nul && SET "@FLAG="
)
)
type tempPackage.json
这使用正则表达式来捕获“插件”行之前的所有内容。
powershell -NoLogo -NoProfile -Command ^
Get-Content -Raw .\pi.txt ^| ^
ForEach-Object { if ($_ -match '(?sm)(.*\"plugins\": {).*') { $Matches[1] + \"`n`n}\" ^| Out-File -FilePath '.package.json'}}