使用命令提示符进行文件比较 [英] File comparison using command prompt

查看:257
本文介绍了使用命令提示符进行文件比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用命令提示符比较两个文本文件,我使用名为abc和xyz的两个文本文件。我需要在其他文本文件中的唯一记录。但是我得到的一些字符串的输出到第二行,这将我的urls分成两个单独的行是有任何方式来比较他们,并获得在现有的文本文件中相同的格式的输出。

  fc abc.txt xyz.txt> uniquetxt 

abc文件包含如下数据

  newsroom.associatedbank.com/News-Releases/Associated-Bank-opens-new-Minocqua-branch-5e1.aspx 
newsroom.associatedbank.com/content/default .aspx?NewsAreaId = 2& SubjectId = 75
newsroom.associatedbank.com/content/default.aspx?NewsAreaId=2&SubjectId=76
newsroom.associatedbank.com/content/default.aspx?NewsAreaId = 2& SubjectId = 202
newsroom.associatedbank.com/News-Releases/Associated-Bank-finances-12M-for-retail-and-residential-projects-5dc.aspx
newsroom.associatedbank.com /News-Releases/Associated-Banc-Corp-completes-purchase-of-risk-and-benefits-consulting-firm-Ahmann-Martin-Co-5db.aspx
newsroom.associatedbank.com/News-Releases/ Associated-opens-new-Rochester-branch-5da.aspx

xyz文件包含如下数据



  newsroom.associatedbank.com/News-Releases/Associated-Bank-opens-new-Minocqua-branch-5e1.aspx 
newsroom.associatedbank.com/content/default.aspx?NewsAreaId=2&SubjectId=75
newsroom.associatedbank.com/content/default.aspx?NewsAreaId=2&SubjectId=76
newsroom.associatedbank .com / content / default.aspx?NewsAreaId = 2& SubjectId = 202
newsroom.associatedbank.com/News-Releases/Associated-opens-new-Rochester-branch-5da.aspx


解决方案

您不必下载 Windows PowerShell 2.0

cmd.exe 命令行:

  powershell Compare-Object -ReferenceObject(Get-Content abc.txt )-DifferenceObject(Get-Content xyz.txt)-IncludeEqual ^ | Out-File -FilePath unique.txt -Width 4096 

注意:




  • 删除 -IncludeEqual (仅限于添加);

  • | 管道是 ^ | 转义以传递给PowerShell,而不是在 cmd ;

  • -Width 4096 更改为所需的输出行长度(整数范围)。任何其他字符都将被截断,不包括。如果省略此参数,则宽度由主机的特性决定。 Windows PowerShell控制台的默认值为80(字符);

  • 这是一个巨大的(用 PowerShell 过滤文件操作的链接);

  • 此处是 Compare-Object Cmdlet 参考。



要查看 SideIndicator 输出格式,请省略 ^ | Out-File ... ,如下所示。

  powershell Compare-Object -ReferenceObject(Get-Content abc.txt)-DifferenceObject Get-Content xyz.txt)-IncludeEqual 

使用 Cmdlet的别名,省略PowerShell 语句的可选部分,下一个命令应该给出相同的结果:

  powershell diff(type abc.txt)(gc xyz.txt)-includeequal 
pre>

I wanted to compare two text files using command prompt and I am using two text files with name abc and xyz. I need unique records in other text file. But the output which I get for some string are going on to second line which breaks my urls into two separate lines is there any way to compare them and get output in the same format which in the existing text file.

fc abc.txt xyz.txt > unique.txt

abc File contains data as below

newsroom.associatedbank.com/News-Releases/Associated-Bank-opens-new-Minocqua-branch-5e1.aspx
newsroom.associatedbank.com/content/default.aspx?NewsAreaId=2&SubjectId=75
newsroom.associatedbank.com/content/default.aspx?NewsAreaId=2&SubjectId=76
newsroom.associatedbank.com/content/default.aspx?NewsAreaId=2&SubjectId=202
newsroom.associatedbank.com/News-Releases/Associated-Bank-finances-12M-for-retail-and-residential-projects-5dc.aspx
newsroom.associatedbank.com/News-Releases/Associated-Banc-Corp-completes-purchase-of-risk-and-benefits-consulting-firm-Ahmann-Martin-Co-5db.aspx
newsroom.associatedbank.com/News-Releases/Associated-opens-new-Rochester-branch-5da.aspx

xyz File contains data as below

newsroom.associatedbank.com/News-Releases/Associated-Bank-opens-new-Minocqua-branch-5e1.aspx
newsroom.associatedbank.com/content/default.aspx?NewsAreaId=2&SubjectId=75
newsroom.associatedbank.com/content/default.aspx?NewsAreaId=2&SubjectId=76
newsroom.associatedbank.com/content/default.aspx?NewsAreaId=2&SubjectId=202
newsroom.associatedbank.com/News-Releases/Associated-opens-new-Rochester-branch-5da.aspx

解决方案

You do not have to download Windows PowerShell 2.0 if you have Windows 7 because it is already installed.

From cmd.exe command line:

powershell Compare-Object -ReferenceObject (Get-Content abc.txt) -DifferenceObject (Get-Content xyz.txt)  –IncludeEqual ^| Out-File -FilePath unique.txt -Width 4096

Notes:

  • remove –IncludeEqual (added for piquancy only);
  • note that | pipe is ^| escaped to pass it to PowerShell, not to treat it in cmd;
  • change -Width 4096 to desired output line length (integer range). Any additional characters are truncated, not wrapped. If you omit this parameter, the width is determined by the characteristics of the host. The default for the Windows PowerShell console is 80 (characters);
  • here is a huge script repository there (the link provided with filter to file manipulation with PowerShell);
  • here is a Compare-Object Cmdlet reference.

To see SideIndicator output format, omit ^| Out-File ... as follows. You should get truncated output on your screen.

powershell Compare-Object -ReferenceObject (Get-Content abc.txt) -DifferenceObject (Get-Content xyz.txt)  –IncludeEqual

Using alias names for Cmdlets and omitting optional parts of PowerShell statements, next command should give the same result:

powershell diff  (type abc.txt)  (gc xyz.txt) -includeequal 

这篇关于使用命令提示符进行文件比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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