在比较批处理脚本两个文件 [英] Comparing two files in batch script

查看:205
本文介绍了在比较批处理脚本两个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图两个文件的方式相比,文件1的每一行将以文件2的每一行进行比较,如果没有找到匹配,写行一个单独的文件。

i am trying to compare two files in the manner, each line of file 1 will be compared with every line of file 2 and if no match is found, write that line to a seperate file.

下面是code我写的,但预期它不工作,

Below is the code i wrote but it is not working as expected,

@echo on
cd path
for /f %%a in (file1.txt) do (
for /f %%b in (file2.txt) do (
if %%a==%%b
(
echo lines are same
) else (
echo %%a >> file3.txt
)
)
)

我收到一个错误说,该命令的语法不正确。
请帮我这一点。

I am getting an error saying, the syntax of the command is incorrect. Please help me with this.

推荐答案

这foxidrive显示FINDSTR方法肯定是最快的纯批次的方式来解决这个问题,特别是如果文件2大。不过,也有一些可能会导致它失败的情况:正则表达式元本地字符文件1,文件1引号和/或反斜线等见的什么是无证的功能和Windows FINDSTR命令的限制?所有的潜在问题。更一些工作可以使解决方案更加可靠。

The FINDSTR method that foxidrive shows is definitely the fastest pure batch way to approach the problem, especially if file2 is large. However, there are a number of scenarios that can cause it to fail: regex meta-charactes in file 1, quotes and/or backslashes in file 1, etc. See What are the undocumented features and limitations of the Windows FINDSTR command? for all of the potential issues. A bit more work can make the solution more reliable.


  • 搜索应当明确提出直译

  • 搜索应该是精确匹配(全线)

  • 在搜索行的任何反斜线应被转义为 \\\\

  • 每个搜索应存放在一个临时文件和 \\ G:文件选项一起使用

  • The search should be explicitly made literal
  • The search should be exact match (entire line)
  • Any backslash in search line should be escaped as \\
  • Each search should be stored in a temp file and the \G:file option used

另外,你没有描述每行的格式。你FOR / F语句只会读取,因为&LT的默认 delims 选项的每一行的第一个单词;标签> <空> 。我怀疑你想要设置 delims 不了了之。你也想​​禁用 EOL 选项,这样线,开始; 不会被跳过。这需要一些怪异的语法。我说万一有usebackq 选项你曾经处理必须被引用的文件名。

Also, you don't describe the format of each line. Your FOR /F statements will read only the first word of each line because of the the default delims option of <tab> and <space>. I suspect you want to set delims to nothing. You also want to disable the eol option so that lines beginning with ; are not skipped. This requires some weird looking syntax. I added the usebackq option in case you ever deal with file names that must be quoted.

@echo off
setlocal disableDelayedExpansion
set "file1=file1.txt"
set "file2=file2.txt"
set "file3=file3.txt"
set "search=%temp%\search.txt"

>"%file3%" (
  for /f usebackq^ delims^=^ eol^= %%A in ("%file1%") do if "%%A" neq "" (
    set "ln=%%A"
    setlocal enableDelayedExpansion
    (echo(!ln:\=\\!) >"%search%"
    findstr /lxg:"%search%" "%file2%" >nul || (echo(!ln!)
    endlocal
  )
)
del "%search%" 2>nul

有是一个非常快速的一条线解决方案,如果你的文件2不包含 \\键,你能负担得起做一个区分大小写的搜索:简单地恢复FINDSTR搜索一下在文件1没有在文件2存在的任何线为什么没有与此FINDSTR例如搜索必须如此,因为的不敏感多个文字搜索字符串找到一个匹配?

There is an extremely fast one line solution if your file2 does not contain \" and you can afford to do a case insensitive search: simply reverse the FINDSTR search to look for any lines in file1 that don't exist in file 2. The search must be case insensitive because of Why doesn't this FINDSTR example with multiple literal search strings find a match?.

findstr /livxg:"file2.txt" "file1.txt" >"file3.txt"

如果文件2包含这将无法正常工作 \\因逃逸的问题。你可以preprocess文件2和逃避所有的 \\ ,但你还不如用第一个解决方案,如果你限制自己一个纯粹的批量解决方案。

This will not work if file2 contains \" because of escape issues. You could preprocess file2 and escape all \, but then you might as well use the first solution if you are restricting yourself to a pure batch solution.

如果你愿意使用混合的JScript /批工具叫REPL.BAT ,那么我有一个非常简单的高效的解决方案。 REPL.BAT执行正则表达式搜索和标准输入的每一行替换操作,并将结果写入到stdout。

If you are willing to use a hybrid JScript/batch utility called REPL.BAT, then I have an extremely simple and efficient solution. REPL.BAT performs a regex search and replace operation on each line of stdin, and writes the result to stdout.

假设REPL.BAT是在当前目录中,或更好,但在你的路径的话:

Assuming REPL.BAT is in your current directory, or better yet, somewhere within your path:

@echo off
setlocal
set "file1=file1.txt"
set "file2=file2.txt"
set "file3=file3.txt"
set "search=%temp%\search.txt"

type "%file2%"|repl \\ \\ >"%search%"
findstr /livxg:"%search%" "%file1%" >"%file3%"
del "%search%" 2>nul

请注意,此解决方案还必须执行不区分大小写的比较。

Note that this solution still must perform a case insensitive comparison.

这篇关于在比较批处理脚本两个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆