[英]Windows batch file to replace nth line with new string or extract nth line and store string in variable
请帮我构建一个Windows批处理文件来替换文件的第n行。 或者,或者提取第n行并将其存储在变量中。 这将用于自动安装Mozilla扩展。
我知道如何在Linux中使用SED执行此操作,但我缺乏用于字符串操作的批处理技能。
例如,我使用此批处理脚本为Microsoft Windows机器安装Mozilla Thunderbird Enigmail扩展:
if %PROCESSOR_ARCHITECTURE% equ x86 (
set SOFTWARE=SOFTWARE
set PROGRAMFILES=C:\Program Files
) else (
set SOFTWARE=SOFTWARE\Wow6432Node
set PROGRAMFILES=C:\Program Files ^(x86^)
)
wget --no-check-certificate https://addons.mozilla.org/en-US/thunderbird/downloads/file/219050/enigmail-1.5.2-tb+sm.xpi -O %TEMP%\enigmail.xpi
mkdir "%PROGRAMFILES%\Mozilla Thunderbird\extensions\enigmail"
unzip -o %TEMP%\enigmail.xpi -d "%PROGRAMFILES%\Mozilla Thunderbird\extensions\enigmail"
reg add HKLM\%SOFTWARE%\Mozilla\Thunderbird\Extensions /v enigmail@example.com ^
/t REG_SZ /d "%PROGRAMFILES%\Mozilla Thunderbird\extensions\enigmail" /f
我遇到的问题是我需要使用匹配的注册表项更新Mozilla扩展的%PROGRAMFILES%\\ Mozilla Thunderbird \\ extensions \\ enigmail \\ install.rdf清单以获取要安装的扩展。 以下安装清单包含多个Mozilla ID(例如, <em:id>{GUID}</em:id>
)。
<?xml version="1.0"?>
<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:em="http://www.mozilla.org/2004/em-rdf#">
<Description about="urn:mozilla:install-manifest">
<em:id>{daf44bf7-a45e-4450-979c-91cf07434c3d}</em:id>
<em:version>1.0</em:version>
<em:type>2</em:type>
<!-- Target Application this extension can install into,
with minimum and maximum supported versions. -->
<em:targetApplication>
<Description>
<em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
<em:minVersion>1.5</em:minVersion>
<em:maxVersion>4.0.*</em:maxVersion>
</Description>
</em:targetApplication>
<!-- Front End MetaData -->
<em:name>sample</em:name>
<em:description>A test extension</em:description>
<em:creator>Your Name Here</em:creator>
<em:homepageURL>http://www.example.com/</em:homepageURL>
</Description>
</RDF>
我需要用<em:id>enigmail@example.com</em:id>
替换此install.rdf的第一个<em:id>
<em:id>enigmail@example.com</em:id>
。 或者,或者我需要通过删除周围的“ <em:id></em:id>
”并将ID与大括号一起存储来提取第一个GUID“{daf44bf7-a45e-4450-979c-91cf07434c3d}”在%GUID%变量中,并使用该变量作为注册表项的名称(例如,reg add HKLM \\ SOFTWARE \\ Wow6432Node \\ Mozilla \\ Thunderbird \\ Extensions / v%GUID%)。
有关详细信息,请参阅https://developer.mozilla.org/en-US/docs/Adding_Extensions_using_the_Windows_Registry和https://developer.mozilla.org/en-US/docs/Building_an_Extension#Create_the_install_manifest 。
替换或提取GUID的解决方案将满足我的需求。 它只取决于是否更容易从install.rdf清单中提取第一个GUID,去除周围的<em:id></em:id>
并将此{GUID}存储在%GUID%变量中,或者替换带有<em:id>enigmail@example.com</em:id>
ID格式的第一个GUID。
针对Windows的sed解决方案,针对第7行:
sed -i.bak "7 s#.*#<em:id>enigmail@example.com</em:id>#" file.html
对于模式<em:id>{daf44bf7-a45e-4450-979c-91cf07434c3d}</em:id>
:
sed -i.bak "7 s#<em:id>{daf44bf7-a45e-4450-979c-91cf07434c3d}</em:id>#<em:id>enigmail@example.com</em:id>#" file.html
编辑 :我修改了下面的批处理程序,以显示两个解决方案作为单独的方法:
@echo off
setlocal DisableDelayedExpansion
rem Extract GUID part of 7th line and use it in the registry key
for /F "skip=6 tokens=1* delims=:" %%a in ('findstr /N "^" install.rdf') do set "line7=%%b" & goto continue
:continue
set "GUID=%line7:*>=%"
set "GUID=%GUID:~0,-8%"
ECHO reg add HKLM\SOFTWARE\Wow6432Node\Mozilla\Thunderbird\Extensions /v %GUID%
rem Replace 7th line
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" install.rdf') do (
if "%%a" neq "7" (
echo(%%b
) else (
echo ^<em:id^>enigmail@example.com^</em:id^>
)
)) > install.tmp
move /Y install.tmp install.rdf > NUL
例如:
> test
reg add HKLM\SOFTWARE\Wow6432Node\Mozilla\Thunderbird\Extensions /v {daf44bf7-a45e-4450-979c-91cf07434c3d}
> type install.rdf
<?xml version="1.0"?>
<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:em="http://www.mozilla.org/2004/em-rdf#">
<Description about="urn:mozilla:install-manifest">
<em:id>enigmail@example.com</em:id>
<em:version>1.0</em:version>
<em:type>2</em:type>
<!-- Target Application this extension can install into,
with minimum and maximum supported versions. -->
<em:targetApplication>
<Description>
<em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
<em:minVersion>1.5</em:minVersion>
<em:maxVersion>4.0.*</em:maxVersion>
</Description>
</em:targetApplication>
<!-- Front End MetaData -->
<em:name>sample</em:name>
<em:description>A test extension</em:description>
<em:creator>Your Name Here</em:creator>
<em:homepageURL>http://www.example.com/</em:homepageURL>
</Description>
</RDF>
@ECHO OFF
SETLOCAL
SET "guid="
FOR /f "tokens=2delims=<> " %%i IN ('find/i "<em:id>" ^<q19063900.rdf') DO SET guid=%%i&GOTO useguid
:useguid
ECHO guid found=%guid%
GOTO :EOF
这应该从包含<en:id>
的第一行中提取GUID字符串。
然后可以按照您的建议在批次中使用%guid%
。 显然,如果批次通过FOR
,则不会设置%guid%
这意味着找不到<em:id>
。
( q19063900.rdf
是源.rdf
文件的名称)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.