gpt4 book ai didi

Powershell 比较对象格式输出

转载 作者:行者123 更新时间:2023-12-04 14:52:09 26 4
gpt4 key购买 nike

这是我写的脚本:

function Compare {

$file1 = Read-Host "Please enter the path of the first file you would like to compare"
$file2 = Read-Host "Please enter the path of the second file you would like to compare"

$outFile = Read-Host "Please enter the path to where you would like your output file."

Try{
$compareOne = Get-Content $file1
$comparetwo = Get-Content $file2
}
Catch{
Write-Host "The path you entered is either invalid or the file does not exist. "
}

Write-Host "Beginning comparison"
Compare-Object $compareOne $compareTwo | Out-File $outFile
Write-Host "Complete!"
}

Compare

这是我的输出:
InputObject | SideIndicator
------------|--------------
Value1 | <=
Value2 | <=
Value3 | =>
Value4 | =>

我是否可以以一种可以更改每列标题的方式来格式化我的输出?

而不是 =><=我可以给出实际在哪个文件中发现差异?

这是我正在寻找的输出类型:
   Value    |     File 
------------|--------------
Value1 | $file1
Value2 | $file2
Value3 | $file2
Value4 | $file1

我对 PowerShell 还是很陌生,所以如果你能解释你的答案会很棒,这样我就可以理解实际发生的事情。

我也在尝试制作这个“虚拟证明”,以便任何人都可以只比较两个文本文件,而无需进一步输入。

任何帮助将不胜感激!

最佳答案

像这样的东西,也许?

function compareCSV {

$file1 = Read-Host "Please enter the path of the first file you would like to compare"
$file2 = Read-Host "Please enter the path of the second file you would like to compare"

$outFile1 = Read-Host "Please enter the path to where you would like your output file."

Try{
$compareOne = Get-Content $file1
$comparetwo = Get-Content $file2
}
Catch{
Write-Host "The path you entered is either invalid or the file does not exist. "
}

Write-Host "Beginning comparison"
$Compare =
Compare-Object $compareOne $compareTwo

$compare | foreach {
if ($_.sideindicator -eq '<=')
{$_.sideindicator = $file1}

if ($_.sideindicator -eq '=>')
{$_.sideindicator = $file2}
}

$Compare |
select @{l='Value';e={$_.InputObject}},@{l='File';e={$_.SideIndicator}} |
Out-File $outFile1

Write-Host "Complete!"
}

compareCSV

关于Powershell 比较对象格式输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22230022/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com