![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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 Specs![]() |
| | #2 (permalink) | ||||||||||||
| 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
| ||||||||||||
My System Specs![]() | |||||||||||||
| | #3 (permalink) | ||||||||||||||||||||||||
| 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
| ||||||||||||||||||||||||
My System Specs![]() | |||||||||||||||||||||||||
| | #4 (permalink) |
| 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 Specs![]() |
| | #5 (permalink) |
| 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 Specs![]() |
| | #6 (permalink) |
| Guest | Re: Searching for content in text files with powershell Thank you all for your advice. I'll give it a whirl. |
My System Specs![]() |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| searching content within files | Amy | Vista General | 3 | 02-28-2008 05:20 PM |
| Help searching text within XLS files | Ryochan | Vista file management | 6 | 11-10-2007 08:07 PM |
| Searching files bases on content. | mundo | Vista file management | 0 | 09-25-2007 07:50 AM |
| Searching for files, content on replaceable media (CD) | Ludwig | Vista General | 0 | 06-20-2007 09:26 PM |
| Searching large text files | Chris Harris | PowerShell | 12 | 06-17-2007 08:30 PM |