This other question非常适合加入两个文件。我需要做相反的事情。我需要从文件A中删除文件B中的行。
此问题类似于this other question,但问题是针对unix,这是针对windows命令shell(cmd.exe)。
我想使用Windows 7 Pro原生的工具。
文件是主机文件。每一行都包含以下行:
127.0.0.1 host.domain.com
或
0.0.0.0 host.domain.com
或
# this is a comment
文件最多可包含200,000行。可能存在空格和制表符。
虽然我更喜欢保留它,但顺序不会影响功能。
以下是主机文件的一些示例:
(对于这个问题,不要担心0.0.0.0
vs' 127.0.0.1`。)
答案 0 :(得分:3)
findstr /v /x /g:"fileb" "filea">resultfile
查找文件A中没有(/v
)的字符串与/x
中的字符串完全匹配(fileb
。
答案 1 :(得分:0)
您没有为此问题提供单一规范!这些文件可能有特殊字符或空格吗?他们可能有多少行?输出文件必须与FileA的顺序相同吗?
此解决方案可能会或可能不会取决于一系列因素......
@echo off
setlocal
rem Load lines from File A into "fileA" array
for /F "delims=" %%a in (FileA.txt) do set "fileA["%%a"]=1"
rem Remove elements from "fileA" array that are in File B
for /F "delims=" %%b in (FileB.txt) do set "fileA["%%b"]="
rem Show remaining elements
for /F "tokens=2 delims=[]" %%a in ('set fileA[') do echo(%%~a
答案 2 :(得分:0)
dbenham在comment中指出,<root>
<SHPR_TOT_NRG_FCAST_OUT>
<METADATA>
<TMSTMP>2017-02-10T10:15:44</TMSTMP>
</METADATA>
<SHPR_TOT_NRG_FCAST_QRY>
<GAS_DAY>2017-02-10</GAS_DAY>
</SHPR_TOT_NRG_FCAST_QRY>
<DTL>
<NRG_FCAST_DTL>
<CRT_DT>2017-02-09</CRT_DT>
<CRT_TM>11:01:21</CRT_TM>
<TOT_FCAST>13847690</TOT_FCAST>
<CNG_SNCE_LST_NM>99.0</CNG_SNCE_LST_NM>
<NA_RNM_UPDTD>Y</NA_RNM_UPDTD>
</NRG_FCAST_DTL>
<NRG_FCAST_DTL>
<CRT_DT>2017-02-09</CRT_DT>
<CRT_TM>13:01:59</CRT_TM>
<TOT_FCAST>13780670</TOT_FCAST>
<CNG_SNCE_LST_NM>-0.5</CNG_SNCE_LST_NM>
<NA_RNM_UPDTD>Y</NA_RNM_UPDTD>
</NRG_FCAST_DTL>
<NRG_FCAST_DTL>
<CRT_DT>2017-02-09</CRT_DT>
<CRT_TM>16:01:03</CRT_TM>
<TOT_FCAST>13889719</TOT_FCAST>
<CNG_SNCE_LST_NM>0.8</CNG_SNCE_LST_NM>
<NA_RNM_UPDTD>Y</NA_RNM_UPDTD>
</NRG_FCAST_DTL>
<NRG_FCAST_DTL>
<CRT_DT>2017-02-10</CRT_DT>
<CRT_TM>00:01:07</CRT_TM>
<TOT_FCAST>13862376</TOT_FCAST>
<CNG_SNCE_LST_NM>-0.2</CNG_SNCE_LST_NM>
<NA_RNM_UPDTD>Y</NA_RNM_UPDTD>
</NRG_FCAST_DTL>
<NRG_FCAST_DTL>
<CRT_DT>2017-02-10</CRT_DT>
<CRT_TM>10:01:01</CRT_TM>
<TOT_FCAST>13679133</TOT_FCAST>
<CNG_SNCE_LST_NM>-1.3</CNG_SNCE_LST_NM>
<NA_RNM_UPDTD>Y</NA_RNM_UPDTD>
</NRG_FCAST_DTL>
</DTL>
</SHPR_TOT_NRG_FCAST_OUT>
<SHPR_TOT_NRG_FCAST_OUT>
<METADATA>
<TMSTMP>2017-02-10T10:15:44</TMSTMP>
</METADATA>
<SHPR_TOT_NRG_FCAST_QRY>
<GAS_DAY>2017-02-11</GAS_DAY>
</SHPR_TOT_NRG_FCAST_QRY>
<DTL>
<NRG_FCAST_DTL>
<CRT_DT>2017-02-10</CRT_DT>
<CRT_TM>11:01:21</CRT_TM>
<TOT_FCAST>338473240</TOT_FCAST>
<CNG_SNCE_LST_NM>99.0</CNG_SNCE_LST_NM>
<NA_RNM_UPDTD>Y</NA_RNM_UPDTD>
</NRG_FCAST_DTL>
<NRG_FCAST_DTL>
<CRT_DT>2017-02-10</CRT_DT>
<CRT_TM>13:01:59</CRT_TM>
<TOT_FCAST>13780670</TOT_FCAST>
<CNG_SNCE_LST_NM>10.5</CNG_SNCE_LST_NM>
<NA_RNM_UPDTD>Y</NA_RNM_UPDTD>
</NRG_FCAST_DTL>
<NRG_FCAST_DTL>
<CRT_DT>2017-02-10</CRT_DT>
<CRT_TM>16:01:03</CRT_TM>
<TOT_FCAST>23889719</TOT_FCAST>
<CNG_SNCE_LST_NM>3.8</CNG_SNCE_LST_NM>
<NA_RNM_UPDTD>Y</NA_RNM_UPDTD>
</NRG_FCAST_DTL>
<NRG_FCAST_DTL>
<CRT_DT>2017-02-11</CRT_DT>
<CRT_TM>00:01:07</CRT_TM>
<TOT_FCAST>13432376</TOT_FCAST>
<CNG_SNCE_LST_NM>10.2</CNG_SNCE_LST_NM>
<NA_RNM_UPDTD>Y</NA_RNM_UPDTD>
</NRG_FCAST_DTL>
<NRG_FCAST_DTL>
<CRT_DT>2017-02-11</CRT_DT>
<CRT_TM>10:01:01</CRT_TM>
<TOT_FCAST>13432133</TOT_FCAST>
<CNG_SNCE_LST_NM>-1.3</CNG_SNCE_LST_NM>
<NA_RNM_UPDTD>Y</NA_RNM_UPDTD>
</NRG_FCAST_DTL>
</DTL>
</SHPR_TOT_NRG_FCAST_OUT>
</root>
存在多个文字搜索字符串的错误 - 引用此帖子:Why doesn't this FINDSTR example with multiple literal search strings find a match?。要解决此问题,您可以在命令提示符中执行以下操作:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format" >
<xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
<xsl:template match="/">GAS_DAY,CRT_DT,CRT_TM,TOT_FCAST,CNG_SNCE_LST_NM,NA_RNM_UPDTD
<xsl:for-each select="//DTL//NRG_FCAST_DTL">
<xsl:value-of select="concat(SHPR_TOT_NRG_FCAST_QRY//GAS_DAY,',',CRT_DT, ',' ,CRT_TM,',' ,TOT_FCAST,',' ,CNG_SNCE_LST_NM,',' ,NA_RNM_UPDTD,'
')"/>
<xsl:text>
</xsl:text> <!-- Add a newline at the end of the record -->
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
或在批处理文件中:
findstr
首先将copy "fileB" "fileC" & (for /F usebackq^ delims^= eol^= %L in ("fileA") do findstr /V /X /C:"%L" "fileC" > "fileC") & move /Y "fileC" "fileB"
复制为copy "fileB" "fileC"
for /F usebackq^ delims^= eol^= %%L in ("fileA") do (
findstr /V /X /C:"%%L" "fileC" > "fileC"
)
move /Y "fileC" "fileB"
,然后从fileB
中逐行删除与fileC
匹配的行,然后将原始fileC
替换为已修改fileA
。