# Search in file

**URL:** <https://forums.powershell.org/t/search-in-file/6861>\
**Category:** PowerShell Help\
**Created:** [July 21, 2016, 4:20am UTC](https://forums.powershell.org/t/search-in-file/6861 "2016-07-21T04:20:42Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ricardo-braganca](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@ricardo-braganca](https://forums.powershell.org/u/ricardo-braganca)\
**Post date:** [July 21, 2016, 4:20am UTC](https://forums.powershell.org/t/search-in-file/6861/1 "2016-07-21T04:20:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![graham-beer](https://avatars.discourse-cdn.com/v4/letter/g/49beb7/32.png) [@graham-beer](https://forums.powershell.org/u/graham-beer)\
**Post date:** [July 21, 2016, 5:18am UTC](https://forums.powershell.org/t/search-in-file/6861/2 "2016-07-21T05:18:44Z")

</div>

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](mailto:graham.beer@outlook.com)  
Dave.dave  
[test@test.com](mailto: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.

---

<div class="post-metadata">

**Author:** ![ricardo-braganca](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@ricardo-braganca](https://forums.powershell.org/u/ricardo-braganca)\
**Post date:** [July 21, 2016, 10:13am UTC](https://forums.powershell.org/t/search-in-file/6861/3 "2016-07-21T10:13:54Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![graham-beer](https://avatars.discourse-cdn.com/v4/letter/g/49beb7/32.png) [@graham-beer](https://forums.powershell.org/u/graham-beer)\
**Post date:** [July 21, 2016, 2:16pm UTC](https://forums.powershell.org/t/search-in-file/6861/4 "2016-07-21T14:16:55Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![ricardo-braganca](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@ricardo-braganca](https://forums.powershell.org/u/ricardo-braganca)\
**Post date:** [July 22, 2016, 12:49am UTC](https://forums.powershell.org/t/search-in-file/6861/5 "2016-07-22T00:49:44Z")

</div>

Hp Graham,

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

[Ricardo.braganca@hotmail.com](mailto:Ricardo.braganca@hotmail.com)  
[Graham@gmail.com](mailto:Graham@gmail.com)  
[Pokemon@hotmail.com](mailto: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.

---

<div class="post-metadata">

**Author:** ![ricardo-braganca](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@ricardo-braganca](https://forums.powershell.org/u/ricardo-braganca)\
**Post date:** [July 22, 2016, 3:14am UTC](https://forums.powershell.org/t/search-in-file/6861/6 "2016-07-22T03:14:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![graham-beer](https://avatars.discourse-cdn.com/v4/letter/g/49beb7/32.png) [@graham-beer](https://forums.powershell.org/u/graham-beer)\
**Post date:** [July 22, 2016, 4:13am UTC](https://forums.powershell.org/t/search-in-file/6861/7 "2016-07-22T04:13:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ricardo-braganca](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@ricardo-braganca](https://forums.powershell.org/u/ricardo-braganca)\
**Post date:** [July 22, 2016, 4:37am UTC](https://forums.powershell.org/t/search-in-file/6861/8 "2016-07-22T04:37:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![graham-beer](https://avatars.discourse-cdn.com/v4/letter/g/49beb7/32.png) [@graham-beer](https://forums.powershell.org/u/graham-beer)\
**Post date:** [July 22, 2016, 7:39am UTC](https://forums.powershell.org/t/search-in-file/6861/9 "2016-07-22T07:39:23Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![ricardo-braganca](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@ricardo-braganca](https://forums.powershell.org/u/ricardo-braganca)\
**Post date:** [July 22, 2016, 8:50am UTC](https://forums.powershell.org/t/search-in-file/6861/10 "2016-07-22T08:50:00Z")

</div>

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 .

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:40pm UTC](https://forums.powershell.org/t/search-in-file/6861/11 "2024-05-16T20:40:51Z")

</div>


