Windows批处理文件用新字符串替换第n行或提取第n行并在变量中存储字符串

时间:2013-09-28 06:05:11

标签: batch-file

请帮我构建一个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>。或者我需要通过删除周围的“<em:id></em:id>”来提取第一个GUID“{daf44bf7-a45e-4450-979c-91cf07434c3d}”,并将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,去掉周围的<em:id></em:id>并将此{GUID}存储在%GUID%变量中,或者将第一个GUID替换为{ {1}} ID格式。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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>

答案 2 :(得分: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字符串。

然后,

%guid%可以在您的批处理中使用。显然,如果批次落入FOR,则%guid%将不会设置,意味着找不到<em:id>

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