文本文件的Powershell比较
Powershell comparison of text file
我只是想知道是否可以比较两个文本文件并检查它们之间是否存在差异?我的意思是这样的,
例如我有 text1.txt :
123
456
789
000
我有 text2.txt :
123,test,input,bake
789,input,cake,bun
预期输出:
456 does not exist in text2.txt
000 does not exist in test2.txt
我是这种语言 (Powershell) 的新手,我只有这段代码可以获取两个文本文件的内容,但我不知道如何比较它们。
$file1 = "C:\test\folder\test1.txt";
$file2 = "C:\test\folder\test2.txt";
$getfile1 = Get-Content $file1
$getfile2 = Get-Content $file2
让您入门的东西:
# $file1 will be an array with each element containing the line contents
$file1 = get-content .\text1.txt
# $file2 will be an array with each element containing the line contents just like $file1
$file2 = get-content .\text2.txt
# This splits each line of $file2 on the comma and grabs the first element and assigns it to the $file2FirstPart array.
$file2FirstPart = $file2 | ForEach-Object { $_.Split(',')[0] }
# This uses the Compare-Object cmdlet to compare the arrays.
$comparision = Compare-Object -ReferenceObject $file1 -DifferenceObject $file2FirstPart -Passthru
# This just displays the string you wanted to display for the comparison results
$comparison | ForEach-Object { "$($_) does not exist in text file 2" }
祝你学习powershell好运!
我只是想知道是否可以比较两个文本文件并检查它们之间是否存在差异?我的意思是这样的,
例如我有 text1.txt :
123
456
789
000
我有 text2.txt :
123,test,input,bake
789,input,cake,bun
预期输出:
456 does not exist in text2.txt
000 does not exist in test2.txt
我是这种语言 (Powershell) 的新手,我只有这段代码可以获取两个文本文件的内容,但我不知道如何比较它们。
$file1 = "C:\test\folder\test1.txt";
$file2 = "C:\test\folder\test2.txt";
$getfile1 = Get-Content $file1
$getfile2 = Get-Content $file2
让您入门的东西:
# $file1 will be an array with each element containing the line contents
$file1 = get-content .\text1.txt
# $file2 will be an array with each element containing the line contents just like $file1
$file2 = get-content .\text2.txt
# This splits each line of $file2 on the comma and grabs the first element and assigns it to the $file2FirstPart array.
$file2FirstPart = $file2 | ForEach-Object { $_.Split(',')[0] }
# This uses the Compare-Object cmdlet to compare the arrays.
$comparision = Compare-Object -ReferenceObject $file1 -DifferenceObject $file2FirstPart -Passthru
# This just displays the string you wanted to display for the comparison results
$comparison | ForEach-Object { "$($_) does not exist in text file 2" }
祝你学习powershell好运!