我有两个文件:
文件1:
Server A sent Mail with testuser1@testdom.com
Server A sent Mail with testuser2@testdom.com
Server B sent Mail with testuser3@testdom.com
file2的:
testuser1@testdom.com
testuser2@testdom.com
testuser3@testdom.com
例如,如果file2中的电子邮件地址“testuser1@testdom.com”也在file1中,则应将此行从file1追加到新文件file3。 是否有两个文件可以在一个步骤中将它们与file2进行比较?
这是我尝试过的,但它并不完全符合我的要求:
compare (cat $file1) (cat $file2) | Out-File $file3
和这:(仅打印完全相同的行,但部分需要它)
Get-Content $file1 | ForEach-Object {
$file1_Line = $_
Get-Content $file2 | Where-Object {$_.Contains($file1_Line)} |
Out-File -FilePath $file3 -Append
}
答案 0 :(得分:3)
如果我理解你的问题,你需要这样的东西:
$cInFile1 = "infile1.txt"
$cInFile2 = "infile2.txt"
$cOutFile = "outfile.txt"
# Reading files as collections on lines.
$cLines1 = Get-Content -Path $cInFile1
$cLines2 = Get-Content -Path $cInFile2
foreach ($sLine in $cLines1) {
$sAddress = ($sLine -split ' ')[-1]
if ($sAddress -in $cLines2) {
$sLine | Out-File -FilePath $cOutFile -Append
}
}