我只是想知道是否可以比较两个文本文件并检查它们之间是否存在差异?我的意思是这样的,
例如我有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
答案 0 :(得分:1)
让你入门的东西:
# $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!