1

请帮我构建一个 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>install.rdf替换为<em:id>enigmail@example.com</em:id>. 要么,要么我需要通过删除周围的“”来提取第一个 GUID“{daf44bf7-a45e-4450-979c-91cf07434c3d}”<em:id></em:id>并将 ID 与花括号一起存储在 %GUID% 变量中,并将该变量用作注册表项的名称(例如,reg add HKLM\SOFTWARE\Wow6432Node\Mozilla\Thunderbird\Extensions /v %GUID%)。

有关详细信息,请参阅https://developer.mozilla.org/en-US/docs/Adding_Extensions_using_the_Windows_Registryhttps://developer.mozilla.org/en-US/docs/Building_an_Extension#Create_the_install_manifest

替换或提取 GUID 的解决方案将满足我的需求。这仅取决于是否更容易从 install.rdf 清单中提取第一个 GUID,剥离周围并将此 {GUID} 存储在 %GUID% 变量中,还是用ID 格式<em:id></em:id>替换第一个 GUID 。<em:id>enigmail@example.com</em:id>

4

3 回答 3

2

使用sed for Windows的解决方案,第 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-28T10:26:12.543 回答
1

编辑:我修改了下面的批处理程序,将两种解决方案显示为单独的方法:

@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>
于 2013-09-28T14:54:53.767 回答
0
@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%然后可以按照您的建议在您的批次中使用。显然,如果批次落入FOR,%guid%将不会被设置为没有<em:id>找到的意思。

q19063900.rdf是您的源.rdf文件的名称)

于 2013-09-28T09:01:51.070 回答