Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 检查文本文件中的行与另一个文本文件中的行_Powershell_Batch File_Text Files - Fatal编程技术网

Powershell 检查文本文件中的行与另一个文本文件中的行

Powershell 检查文本文件中的行与另一个文本文件中的行,powershell,batch-file,text-files,Powershell,Batch File,Text Files,我有两个文本文件,如何比较它们??基本上,我所追求的是从文本文件1中获取第一行,并将其与文本文件2中的所有行进行比较,如果该行没有出现,则将该行写入文本文件3 然后检查文本文件1中的下一行,列出文本文件2中的所有行,依此类推。如果您有一份grep for Windows的副本,则问题很小。一个好的免费来源是。您可以从该链接下载单个实用程序,如grep,也可以使用该链接获得整个GnuWin套件(单击该页面开头的下载按钮) -v=反转匹配逻辑-列出不匹配的行 -x=整行必须完全匹配 -F=搜索字符串

我有两个文本文件,如何比较它们??基本上,我所追求的是从文本文件1中获取第一行,并将其与文本文件2中的所有行进行比较,如果该行没有出现,则将该行写入文本文件3


然后检查文本文件1中的下一行,列出文本文件2中的所有行,依此类推。

如果您有一份grep for Windows的副本,则问题很小。一个好的免费来源是。您可以从该链接下载单个实用程序,如grep,也可以使用该链接获得整个GnuWin套件(单击该页面开头的下载按钮)

-v
=反转匹配逻辑-列出不匹配的行

-x
=整行必须完全匹配

-F
=搜索字符串是字符串文字,而不是正则表达式

-f file1.txt
=从file1.txt获取搜索字符串


您几乎可以使用本机FINDSTR命令执行相同的操作,但有两个问题:

1) 搜索字符串中的任何反斜杠字符
\
必须作为
\\
转义,即使在指定文字搜索时也是如此

2) 如果使用了多个区分大小写的文本搜索字符串,则会有一个讨厌的FINDSTR错误导致某些匹配丢失

有关未记录的FINDSTR问题的“完整”列表,请参阅

只要可以进行不区分大小写的搜索,并且file2不包含任何
\
字符,则以下操作将起作用:

findstr /x /v /i /l /g:file2.txt file1.txt >file3.txt
可以通过创建一个逃避反斜杠的临时文件来消除反斜杠限制。这是一段代码,但最终结果仍然运行得相当快。搜索仍必须不区分大小写

@echo off
setlocal disableDelayedExpansion

::Define the files
set "file1=test1.txt"
set "file2=test2.txt"
set "file3=test3.txt"

::Create an LF variable containing a line feed character
set LF=^


::The above 2 blank lines are critical - do not remove

::Create a modified version of file2 that escapes any backslash
::EOL is set to a linefeed so that all non blank lines are preserved
::Delayed expansion is toggled on and off to protect ! characters
>"%file2%.mod" (
  for /f usebackq^ eol^=^%LF%%LF%^ delims^= %%A in ("%file2%") do (
    set "ln=%%A"
    setlocal enableDelayedExpansion
    echo(!ln:\=\\!
    endlocal
  )
)

::Find lines in file1 that are missing from file2.mod
findstr /vixlg:"%file2%.mod" "%file1%" >"%file3%"

::Delete the temporary file2.mod
del "%file2%.mod"
使用2 FOR循环编写健壮的本机批处理解决方案相对简单,但如果文件太大,性能会很快下降

@echo off
setlocal disableDelayedExpansion

::Define the files
set "file1=test2.txt"
set "file2=test.txt"
set "file3=test3.txt"

::Create an LF variable containing a line feed character
set LF=^


::The above 2 blank lines are critical - do not remove

::Find lines in file1 that are missing from file2.mod
::EOL is set to a linefeed character so that all non blank lines are preserved
>"%file3%" (
  for /f usebackq^ eol^=^%LF%%LF%^ delims^= %%A in ("%file1%") do (
    set "found="
    for /f usebackq^ eol^=^%LF%%LF%^ delims^= %%B in ("%file2%") do (
      if %%A==%%B set found=1
    )
    if not defined found echo %%A
  )
)


可能有一个简单高效的本机PowerShell解决方案,但这不是我的专长。

您已经回答了您的问题。您询问了如何比较两个文本文件,然后解释了如何进行比较。那么,现在的问题是什么?
@echo off
setlocal disableDelayedExpansion

::Define the files
set "file1=test2.txt"
set "file2=test.txt"
set "file3=test3.txt"

::Create an LF variable containing a line feed character
set LF=^


::The above 2 blank lines are critical - do not remove

::Find lines in file1 that are missing from file2.mod
::EOL is set to a linefeed character so that all non blank lines are preserved
>"%file3%" (
  for /f usebackq^ eol^=^%LF%%LF%^ delims^= %%A in ("%file1%") do (
    set "found="
    for /f usebackq^ eol^=^%LF%%LF%^ delims^= %%B in ("%file2%") do (
      if %%A==%%B set found=1
    )
    if not defined found echo %%A
  )
)