Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista Tutorial - Searching for content in text files with powershell

Reply
 
Old 12-30-2007   #1 (permalink)
snofire
Guest


 
 

Searching for content in text files with powershell

Hey all,

I need a powershell script that will search through all of the text files in
a directory, and copy those files that contain a given term into another
file. This is the line I am having trouble with:

Get-Content *.* | Select-String -pattern "$term" -context 20,20
-caseSensitive | Out-File E:\test.txt

With this, the script locates the terms all right, but because I had to use
context 20,20 in order to get the whole file where the term lives, it grabs a
bunch of useless data as well. Is there any way that I can just paste a file
where the term occurs?

My System SpecsSystem Spec
Old 12-30-2007   #2 (permalink)
Shay Levi
Guest


 
 

Re: Searching for content in text files with powershell


Hi snofire, try this:

Select-String -pattern "$term" -context 20,20 -caseSensitive -Path <path>\*.*
-list | foreach { cat $_.path | Add-Content -Path c:\matches.txt -force}


Since you copy each matched file full content to another file, I don't think
the -context 20,20 is necessary.
If you need to perform recursive search, use:

get-childitem <path> -filter <wildcard> -recurse | Select-String -pattern
"$term" ... | foreach { cat $_.path | Add-Content -Path c:\matches.txt -force}

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> Hey all,
>
> I need a powershell script that will search through all of the text
> files in a directory, and copy those files that contain a given term
> into another file. This is the line I am having trouble with:
>
> Get-Content *.* | Select-String -pattern "$term" -context 20,20
> -caseSensitive | Out-File E:\test.txt
>
> With this, the script locates the terms all right, but because I had
> to use context 20,20 in order to get the whole file where the term
> lives, it grabs a bunch of useless data as well. Is there any way that
> I can just paste a file where the term occurs?
>

My System SpecsSystem Spec
Old 12-30-2007   #3 (permalink)
Shay Levi
Guest


 
 

Re: Searching for content in text files with powershell


The second command should look like this: (without ... )

get-childitem <path> -filter <wildcard> -recurse | Select-String -pattern
"$term" | foreach { cat $_.path | Add-Content -Path c:\matches.txt -force}


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> Hi snofire, try this:
>
> Select-String -pattern "$term" -context 20,20 -caseSensitive -Path
> <path>\*.* -list | foreach { cat $_.path | Add-Content -Path
> c:\matches.txt -force}
>
> Since you copy each matched file full content to another file, I don't
> think
> the -context 20,20 is necessary.
> If you need to perform recursive search, use:
> get-childitem <path> -filter <wildcard> -recurse | Select-String
> -pattern "$term" ... | foreach { cat $_.path | Add-Content -Path
> c:\matches.txt -force}
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
Quote:

>> Hey all,
>>
>> I need a powershell script that will search through all of the text
>> files in a directory, and copy those files that contain a given term
>> into another file. This is the line I am having trouble with:
>>
>> Get-Content *.* | Select-String -pattern "$term" -context 20,20
>> -caseSensitive | Out-File E:\test.txt
>>
>> With this, the script locates the terms all right, but because I had
>> to use context 20,20 in order to get the whole file where the term
>> lives, it grabs a bunch of useless data as well. Is there any way
>> that I can just paste a file where the term occurs?
>>

My System SpecsSystem Spec
Old 12-30-2007   #4 (permalink)
Kiron
Guest


 
 

Re: Searching for content in text files with powershell

Use Select-String's -Quiet switch --it returns a boolean value-- and filter the files that match with Where-Object to set their content to the new file:

set-content E:\test.txt (
get-childItem *.txt |
where {select-string $term $_ -quiet} |
get-content
)

--
Kiron
My System SpecsSystem Spec
Old 12-30-2007   #5 (permalink)
Kiron
Guest


 
 

Re: Searching for content in text files with powershell

If $term is present more than once there'll be repeated content, to avoid this use Select-String's -List switch.

--
Kiron
My System SpecsSystem Spec
Old 12-31-2007   #6 (permalink)
snofire
Guest


 
 

Re: Searching for content in text files with powershell

Thank you all for your advice. I'll give it a whirl.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
searching content within files Vista General
Help searching text within XLS files Vista file management
Searching files bases on content. Vista file management
Searching for files, content on replaceable media (CD) Vista General
Searching large text files PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46