2015-09-25 52 views
2

我有兩個文件:比較線條,使新的生產線進行比賽

文件1:

Server A sent Mail with [email protected] 
Server A sent Mail with [email protected] 
Server B sent Mail with [email protected] 

文件2:

[email protected] 
[email protected] 
[email protected] 

例如,如果電子郵件地址「testuser1 @ TESTDOM。 com「from file2也在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

「它應該創建與鄂麥新文件l地址或文件1中的行「 - 您能澄清一下嗎? –

+0

例如,如果file2中的電子郵件地址「[email protected]」也在file1中,則應將此行從file1附加到file3。 – Clam

回答

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 
    } 
} 
+0

這正是我一直在尋找的,謝謝! – Clam