PowerShell two ways to compare text files _powershell

Source: Internet
Author: User
Tags diff readline

Requirements: compare two text files, output line numbers and column numbers when not equal, and start unequal characters.

Method One: Compare-object

Using the PowerShell command Compare-object is worth learning.

Copy Code code as follows:

function compare-files{
Param
$file 1,
$file 2,
[Switch] $IncludeEqual
)
$content 1 = get-content $file 1
$content 2 = get-content $file 2
$comparedLines = compare-object $content 1 $content 2-includeequal: $IncludeEqual |
Group {$_. Inputobject.readcount} | Sort Name
$comparedLines | foreach {
$curr =$_
Switch ($_. Group[0]. Sideindicator) {
"= =" {$right = $left = $curr. Group[0]. Inputobject;break}
"=>" {$right, $left = $curr. Group[0]. Inputobject, $curr. GROUP[1]. Inputobject;break}
"<=" {$right, $left = $curr. GROUP[1]. Inputobject, $curr. Group[0]. Inputobject;break}
}
[Pscustomobject] @{
line = $_. Name
left = $left
right = $right
}
}
}

Method two, using stream processing to compare

Explanation: Instead of using a built-in string equality comparison, the function diff-string is defined to return the index of the first unequal string.

Copy Code code as follows:

function Diff-txt ([Io.fileinfo] $firstFile, [Io.fileinfo] $secondFile)
{
#参数判断
if (-not $firstFile. Exists)
{
Throw "$firstFile does not exist"
}
if (-not $secondFile. Exists)
{
Throw "$secondFile does not exist"
}
$SR 1=[io. StreamReader] $firstFile. FullName
$SR 2=[io. StreamReader] $secondFile. FullName

#内部函数: Comparing Strings
#返回-1 equals, returns other indexes that represent unequal starting from $STR1
function diff-string ([String] $str 1,[string] $str 2)
{
For ($i =0 $i-lt $str 1. Length; $i + +)
{
if ($i-lt $str 2. Length)
{
if ($str 1[$i]-cne $str 2[$i]) {return $i}
}
else {return $i}
}
if ($str 2. Length-gt $i) {return $i}
Return-1
}

# Firstfile not to the end of the file
$line =1
while (-not $SR 1. Endofstream)
{
$str 1 = $sr 1. ReadLine ()

# Secondfile to the end of the file
if ($SR 2. Endofstream)
{
Write-host "=> [$line row, 1 columns]"
}

# Secondfile not to the end of the file
Else
{
$str 2 = $SR 2. ReadLine ()
$result = diff-string-str1 $str 1-str2 $str 2
if ($result-ne-1)
{
Write-host "<> [$line Row, $result column] character->"-nonewline
Write-host $str 1[$result]-foregroundcolor Red
}
}

$line + +
}
# The second file is not at the end of the file
while (-not $SR 2. Endofstream)
{
$str 2 = $SR 2. ReadLine ()
Write-host "<= [$line row, 1 columns]"
}
# Close File stream
$SR 1. Close ()
$SR 2. Close ()
}

Test Case A

Input

The first file reads:

Copy Code code as follows:

PowerShell
Chinese blog
Jb51.net

Lai Non-moss
The second file reads:
Copy Code code as follows:

PowerShell
Chinese Podcast
Jb51.net
Lai Non-moss
Mosser Lee

Output:

Test Case Two

Input

The first file reads:

Copy Code code as follows:

Yushu
Right now, Erie.
Article contrived

The second file reads:
Copy Code code as follows:

Yushulinfeng Crazy


Output:

Article Source: http://www.pstips.net/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.