请帮助我构造一个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 (例如,<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>替换这个install.rdf的第一个<em:id>enigmail@example.com</em:id>。或者,我需要提取第一个GUID "{daf44bf7-a45e-4450-979c-91cf07434c3d}“,方法是删除周围的"<em:id></em:id>”,并将ID连同花括号一起存储在%GUID%变量中,并使用该变量作为注册表项的名称(例如,reg add <em:id></em:id> /v %GUID%)。
更换或提取GUID的解决方案将满足我的需要。这取决于是否更容易从install.rdf清单中提取第一个GUID,去掉周围的<em:id></em:id>并将此{ GUID }存储在%GUID%变量中,还是用<em:id>enigmail@example.com</em:id> ID格式替换第一个GUID。
发布于 2013-09-28 09:01:51
@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的名称)
发布于 2013-09-28 10:26:12
使用面向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发布于 2013-09-28 14:54:53
编辑:我修改了下面的批处理程序,将这两个解决方案显示为单独的方法:
@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>https://stackoverflow.com/questions/19063900
复制相似问题