Powershell – Compare-Object

Loading

測試環境為 Windows10 Pro (Version :1809 , Build :17763)

Powershell 指令 Compare-Object – 用來比較兩個 objects ,直接來看範例.

參考文章 – https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/compare-object?view=powershell-6

比較兩個文字檔

PS C:\Users\ben> type C:\Users\ben\Temp1.txt
Staff & Project
LOAD DATA INFILE '/tmp/Agu1.csv'
INTO TABLE astl.dms
FIELDS TERMINATED BY ','
ENCLOSED BY '"' LINES
TERMINATED BY '\n' (site , dept , fun , name , project ) ;
PS C:\Users\ben> type C:\Users\ben\Temp2.txt
Staff & Project
LOAD DATA INFILE '/tmp/Agu2.csv'
INTO TABLE astl.dms
FIELDS TERMINATED BY ','
ENCLOSED BY '"' LINES
TERMINATED BY '\n' (site , dept , fun , name , project ) ;

比較時需要兩個指定檔案的參數 ReferenceObject 與 DifferenceObject.

PS C:\Users\ben> Compare-Object -ReferenceObject $(Get-Content -Path C:\Users\ben\Temp1.txt) -DifferenceObject $(Get-Content -Path C:\Users\ben\Temp2.txt)

InputObject                       SideIndicator
-----------                       -------------
LOAD DATA INFILE '/tmp/Agu2.csv'  =>
LOAD DATA INFILE '/tmp/Agu1.csv'  <=

比較結果會使用
<= 指出該行出現在 Reference Object (與 Difference Object 不同).
=> 指出該行出現在 Difference Object (與 Reference Object 不同).
== 指出該行出現在 Reference Object 與 Difference Object (代表相同,預設不顯示,需使用 IncludeEqual parameter 參數,如下範例).

PS C:\Users\ben> Compare-Object -ReferenceObject $(Get-Content -Path C:\Users\ben\Temp1.txt) -DifferenceObject $(Get-Content -Path C:\Users\ben\Temp2.txt) -IncludeEqual

InputObject                                                SideIndicator
-----------                                                -------------
Staff & Project                                            ==
INTO TABLE astl.dms                                        ==
FIELDS TERMINATED BY ','                                   ==
ENCLOSED BY '"' LINES                                      ==
TERMINATED BY '\n' (site , dept , fun , name , project ) ; ==
LOAD DATA INFILE '/tmp/Agu2.csv'                           =>
LOAD DATA INFILE '/tmp/Agu1.csv'                           <=
沒有解決問題,試試搜尋本站其他內容

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料