所以我有兩個文件,一個帶有數字和描述列表,另一個帶有數字列表。Powershell將一個文件中的行匹配到另一行中的一行。
如:
文件1包含:
10001 Screw
23456 Bolt
文件2包含:
10001
23456
23456
我想要做的就是爲每一個數字出現在文件2時它添加描述它或輸出數字和描述到一個新的文件。
那麼結果會是什麼樣子:
10001 Screw
23456 Bolt
23456 Bolt
這些都只是小樣本,但實際的文件有很多千行。
這是我想出來的,但它不是很正確。
$file1 = "C:\pstest\file1.txt"
$file2 = "C:\pstest\file2.txt"
Get-Content $file2 | Foreach-Object -process {Get-Content $file1 | Where-Object $_ -contains $file2 | write-host $_ }
OR
$file1 = "C:\pstest\file1.txt"
$file2 = "C:\pstest\file2.txt"
$file3 = "C:\pstest\file3.txt"
Get-Content $file2
foreach ($line in $file2){
Get-Content $file1 | Where-Object $_ -contains $line | out-file $file3
}
應該提到兩者都是製表符分隔的。 – 2015-02-12 23:43:54