用于删除重复行但希望忽略/跳过某些行的批处理脚本

时间:2013-07-25 13:45:44

标签: windows batch-file duplicates file-processing

在从xml文件中删除其他重复项时,是否也可以忽略一些重复的行,例如:如果我的abx.xml是 代码:

@echo off
setlocal disableDelayedExpansion
set "file=%~1"
set "line=%file%.line"
set "deduped=%file%.deduped"
::Define a variable containing a linefeed character
set LF=^


::The 2 blank lines above are critical, do not remove
>"%deduped%" (
  for /f usebackq^ eol^=^%LF%%LF%^ delims^= %%A in ("%file%") do (
    set "ln=%%A"
    setlocal enableDelayedExpansion
    >"%line%" (echo !ln:\=\\!)
    >nul findstr /xlg:"%line%" "%deduped%" || (echo !ln!)
    endlocal
  )
)
>nul move /y "%deduped%" "%file%"
2>nul del "%line%"

BATCH SCRIPT 请。

<bookstores>
   <book id="parent">
      <name="it1"/>
      <name="it1"/>
      <name="it2"/>
   </book>
   <book id="child">
      <name="it1"/>
      <name="it1"/>
      <name="it2"/>
      <name="it3"/>
   </book>     
</bookstores>

输出应为:

<bookstores>
   <book id="parent">
      <name="it1"/>
      <name="it2"/>
   </book>
   <book id="child">
      <name="it3"/>
   </book>     
</bookstores>

但我得到的输出是: 注意: </book>标记已删除。

<bookstores>
   <book id="parent">
      <name="it1"/>
      <name="it2"/>
   </book>
   <book id="child">
      <name="it3"/>

</bookstores>

我搜索了几个simillar请求,但大多数都删除了所有重复的行,但不确定如何忽略一些重复的行:

Batch to remove duplicate rows from text file

1 个答案:

答案 0 :(得分:3)

如果您在%dict%文件中始终将行打印,这可能对您有用:

@ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
SET "file=file"
SET "new=new"
SET "dict=dictionary"

(FOR /f "tokens=1*delims=:" %%a IN ('findstr /n "^" "%file%"') DO (
    SET "nr=%%a"
    SET "line=%%b"
    SET "this="
    FINDSTR /l "!line!" "%dict%" >NUL 2>&1&& ECHO(!line! || (
        FOR /f "tokens=1*delims==" %%x IN ('set "$" 2^>nul') DO IF !line!==%%y SET "this=1"
        IF "!this!"=="" (
            ECHO(!line!
            SET "$!nr!=!line!"
        )
    )
))>"%new%"
TYPE "%new%"

.. shell会话:

    >type file
    <bookstores>
       <book id="parent">
          <name="it1"/>
          <name="it1"/>
          <name="it2"/>
       </book>
       <book id="child">
          <name="it1"/>
          <name="it1"/>
          <name="it2"/>
          <name="it3"/>
       </book>
    </bookstores>

    >type dictionary
    </book>

    >script.bat
    <bookstores>
       <book id="parent">
          <name="it1"/>
          <name="it2"/>
       </book>
       <book id="child">
          <name="it3"/>
       </book>
    </bookstores>