# search a file for multiple strings and output to another file

**URL:** <https://forums.powershell.org/t/search-a-file-for-multiple-strings-and-output-to-another-file/3113>\
**Category:** PowerShell Help\
**Created:** [September 16, 2014, 2:42am UTC](https://forums.powershell.org/t/search-a-file-for-multiple-strings-and-output-to-another-file/3113 "2014-09-16T02:42:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![darth-kitty](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@darth-kitty](https://forums.powershell.org/u/darth-kitty)\
**Post date:** [September 16, 2014, 2:42am UTC](https://forums.powershell.org/t/search-a-file-for-multiple-strings-and-output-to-another-file/3113/1 "2014-09-16T02:42:03Z")

</div>

I am formatting a mail header using the following

get-childitem -Path C:\SearchMe.txt | Select-String -Pattern ‘X-[SB]’, ‘X-Envelope-From’, ‘X-MS-Exchange-Organization-[SP]CL’, ‘Message-’, ‘MIME\_H’, ‘Subject:’, ‘score=’,‘Received: from [0-9]’ -Context 0,2 | foreach{$\_.line} \> C:\stuffit.txt

very thing works great but the ‘Received: from [0-9]’ -Context 0,2

Pretty please help get the context to work or me strip the nasty path up to the last colon

Thank you Very much

John

---

<div class="post-metadata">

**Author:** ![ondrej-zilinec](https://avatars.discourse-cdn.com/v4/letter/o/e480ec/32.png) [@ondrej-zilinec](https://forums.powershell.org/u/ondrej-zilinec)\
**Post date:** [September 16, 2014, 7:24pm UTC](https://forums.powershell.org/t/search-a-file-for-multiple-strings-and-output-to-another-file/3113/2 "2014-09-16T19:24:29Z")

</div>

Why do you use “[0,9]” in “‘Received: from [0-9]’”?

---

<div class="post-metadata">

**Author:** ![darth-kitty](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@darth-kitty](https://forums.powershell.org/u/darth-kitty)\
**Post date:** [September 17, 2014, 1:37am UTC](https://forums.powershell.org/t/search-a-file-for-multiple-strings-and-output-to-another-file/3113/3 "2014-09-17T01:37:22Z")

</div>

Thank you Ondrej Z

* * *

Internet Header BEFORE Cleanup

* * *

there are several lines in the before file that start “Received: from” and a alpha charcter  
Received: from ALNEXHTCAS01.company.xyz (5.5.5.5) by…  
Received: from ALNMSW01.company.xyz (10.241.0.145) by…  
Received: from ALNMSW01.company.xyz (10.241.0.145) by…  
Received: by 089…  
I am only interested in the string  
Received: from and some number so the mask and the next two lines after so I use the pattern  
_**‘Received: from [0-9]’ -context 0,2**_  
^^^^i want the line that begins “Received: from next character any number [0-9]”

- context 0 no lines before the match 2 two lines after the match  
foreach{$\_.line} seems to strip the two after the match

---

<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:47pm UTC](https://forums.powershell.org/t/search-a-file-for-multiple-strings-and-output-to-another-file/3113/4 "2024-05-16T20:47:29Z")

</div>


