Author Posts

July 21, 2016 at 9:50 am

Hello,

I have 2 txt files in a have a mailing list and other files have several records.
The goal goes through search the txt file that has records the email that is in the txt file that has the list of emails and return some records.

Right now I'm doing the search for mail ('@') in the document of record. And I'm not able to do the search based on an existing list in another txt file.

July 21, 2016 at 10:48 am

Hi Ricardo,

Here is a solution you could try.

I created a small text file called mail.txt and added the 3 lines for testing:
graham.beer@outlook.com
Dave.dave
test@test.com

I created a small regular expression to search on the symbol '@'. The metacharacter \b is an anchor. It matches at a position that is called a "word boundary". This match is zero-length.
Here is the regular expression:

 [regex]$regex = '\b@\b' 

I then used the 'select-string' cmdlet. The Select-String cmdlet searches for text and text patterns in input strings and files. You can use it like Grep in UNIX and Findstr in Windows. (from get-help select-string)

So putting it altogether and getting a clean output I created a PS Object to display the results. Here is the code:

[regex]$regex = '\b@\b'

Select-String $regex C:\temp\mail.txt |
    ForEach {
        $fileName = $_.Path
        ForEach($match in $_.Matches) {
            New-Object PSObject -Property @{
                FileName   = $fileName
                Value      = $_.line
                LineNumber = $_.LineNumber
                Matched_On = $_.Matches
            }
        }
    } 

returned:

LineNumber Matched_On FileName         Value                      
---------- ---------- --------         -----                      
         1 {@}        C:\temp\mail.txt graham.beer@outlook.com    
         3 {@}        C:\temp\mail.txt test@test.com

Where I have 'C:\temp\mail.txt', change this to your file location.

Hope this helps.

July 21, 2016 at 3:43 pm

Hi Graham Beer,

At the moment I have this code:

$files = Get-ChildItem "C:\Users\ricardo.braganca\Desktop\NEW\EDPC*.txt"
$devolvidos = "C:\Users\ricardo.braganca\Desktop\NEW\devolvidos.txt"
$folder = "C:\Users\ricardo.braganca\Desktop\NEW\dados*.txt"
$count = $null
$match =  "C:\Users\ricardo.braganca\Desktop\NEW\match.txt"

switch -Regex -File $files.fullname
{
    'MAI000' {$count++;Write-Verbose "Group $count found" -Verbose}
    '^FE|^01' { $_ |Add-Content -Path "C:\Users\ricardo.braganca\Desktop\NEW\dados$($count).txt" }
    'MAIFIM' {continue}
}

Get-ChildItem -path $folder -Recurse |


Where-Object{
ForEach-Object {
   
       Get-Content $devolvidos | ForEach-Object {

       $names1_Line = $_
       Get-Content  $folder | Where-Object { $_.contains($names1_line)}|
       Out-File -FilePath $match -Append 
}
}
}

The problem is that I'm not able to get the full contents of the files that meet the conditions. I am just getting the lines which have email. what should I do?

July 21, 2016 at 7:46 pm

Ricardo,

Not sure I fully understand. Are you not looking for the @ symbol to pull out all the e-mail address ?
Can you give me a sample of your text file so I can take a look ?

July 22, 2016 at 6:19 am

Hp Graham,

I have 1 txt file with for e.g.:

Ricardo.braganca@hotmail.com
Graham@gmail.com
Pokemon@hotmail.com

And I have anotar with for e.g. :

FE | 123456 | MAI000| 6582 |
FE | 123456 | 6582 |Pokemon@hotmail.com
FE | 123456 | MAIFIM|
FE | 456789 | MAI00 | 6582 |
FE | 123456 | 6582 |POTATOS@.com
FE | 456789 | MAFIM |

the objective is with mail existing in first txt file search in second to find a marcha mail and return this:

FE | 123456 | MAI000| 6582 |
FE | 123456 | 6582 |Pokemon@hotmail.com
FE | 123456 | MAIFIM|

At this moment i managed to get
FE | 123456 | 6582 |Pokemon@hotmail.com

I hoje tais explanation Washington Bettencourt.

July 22, 2016 at 8:44 am

Hi Graham,

Sorry for my previous post is the fault of the Portuguese smart smartphone writing,

I have 1 txt file with for e.g.:

Ricardo.braganca@hotmail.com
Graham@gmail.com
Pokemon@hotmail.com

And I have anotar with for e.g. :

FE | 123456 | MAI000| 6582 |
FE | 123456 | 6582 |Pokemon@hotmail.com
FE | 123456 | MAIFIM|
FE | 456789 | MAI00 | 6582 |
FE | 123456 | 6582 |POTATOS@.com
FE | 456789 | MAFIM |

the objective is with mail existing in first txt file search in second to find a marcha mail and return this:

FE | 123456 | MAI000| 6582 |
FE | 123456 | 6582 |Pokemon@hotmail.com
FE | 123456 | MAIFIM|

At this moment i managed to get

FE | 123456 | 6582 |Pokemon@hotmail.com

I hope this explanation was better.

July 22, 2016 at 9:43 am

So from the E-mail address you want to get the number in the second column and display all matching to that number ?

July 22, 2016 at 10:07 am

Yes, that's it. In the code I presented before I was to divide the various records in blocks starting at MAI000 and end in MAIFIM and each block was stored in a different txt file . Then makes the comparison that has txt this email. If you have email that is on the first txt file goal is to return the block.

So far I'm only able to return the the line containing the email e not the entire block.

July 22, 2016 at 1:09 pm

So my two files contain the below:

Main.txt
FE | 123456 | MAI000| 6582 |
FE | 123456 | 6582 |Pokemon@hotmail.com
FE | 123456 | MAIFIM|
FE | 456789 | MAI00 | 6582 |
FE | 123456 | 6582 |POTATOS@.com
FE | 456789 | MAFIM |

Mail.txt 
Ricardo.braganca@hotmail.com 
Graham@gmail.com
Pokemon@hotmail.com

Now use the below code:

$eMail = get-content 'C:\temp\PS Testing\mail.txt'
$main = get-content 'C:\temp\PS Testing\Main.txt'

foreach ($address in $eMail) {
    $main | Select-String -Pattern $address -Context 1,1
}

So capture each text file contents in a variable, using get-content.

Use a foreach to get each e-mail address in e-mail text file ($address which is an empty variable), $address in $email.

pipe the main text file to select-string, passing the pattern, which in our case is each e-mail address. The -context option allows you to select lines above and below the found object. I used 1,1 which is 1 line above and 1 below, displaying :

  FE | 123456 | MAI000| 6582 |
> FE | 123456 | 6582 |Pokemon@hotmail.com
  FE | 123456 | MAIFIM|

That help ?

July 22, 2016 at 2:20 pm

This helps but there is a small problem for "-context" work is necessary that the block has only three lines but the block vary the context does not work because it does not guarantee that I will return the complete block .