![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Looping Through A Text File To Find Patterns From Another Text Fil Looping Through A Text File To Find Patterns From Another Text File All, I'm trying to loop through a text file searching for specific terms (a la *nix grep) that are taken from another text file. Here's some text from the log file to be searched: CodeA 12:45:27 AM LOCATION label: 1234, at lane: 21 SORTED Err30 12:45:28 AM SYSTEM: Unexpected Packages at Update 007 CodeB 12:45:29 AM LOCATION label: 5678, at lane: 6 SORTED Here are some of the search terms from a comma-delimited text file: SORTED,Unexpected,Unread,: CodeMissing Here's the original code: $SearchStrings = @((Get-Content "searchstrings.txt").split(",")) ForEach($Line in $CurrentLog) { ForEach($string in $SearchStrings) { $TOTALSORTED = Select-String "SORTED" $CurrentLog | Measure-Object -line | % {$_.lines} if ((Select-String ": CodeMissing" $CurrentLog | Measure-Object -line | % {$_.lines}) -eq $null) { $CMISSING = 0 } else { $CMISSING = $_.lines } ... } } It works alright, but I'd like to 1) shorten it and 2) not have to use the if statement to check if anyone of them is $null; I'd rather use a switch of something like this: $CurrentLog | % {$SearchStrings | % {$global:h=@{}} {$h[$_] += 1}} When I run this, I get 1 for each of my search strings instead of their count from the text file. It seems I need to add an if there, but am not sure where. Also, the file it runs against is ~ 20 MB and it takes a looooong time to process. Any thoughts? |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Looping Through A Text File To Find Patterns From Another Text Fil Hi GWold, See if this helps: $SearchStrings = (Get-Content searchstrings.txt).split(",") Select-String -Path *.txt -Pattern $SearchStrings | group pattern --- Shay Levy Windows PowerShell MVP http://blogs.microsoft.co.il/blogs/ScriptFanatic PowerShell Toolbar: http://tinyurl.com/PSToolbar G> Looping Through A Text File To Find Patterns From Another Text File G> G> All, G> G> I'm trying to loop through a text file searching for specific terms G> (a la *nix grep) that are taken from another text file. Here's some G> text from the log file to be searched: G> G> CodeA 12:45:27 AM LOCATION label: 1234, at lane: 21 SORTED Err30 G> 12:45:28 AM SYSTEM: Unexpected Packages at Update 007 CodeB 12:45:29 G> AM LOCATION label: 5678, at lane: 6 SORTED G> G> Here are some of the search terms from a comma-delimited text file: G> G> SORTED,Unexpected,Unread,: CodeMissing G> G> Here's the original code: G> $SearchStrings = @((Get-Content "searchstrings.txt").split(",")) G> ForEach($Line in $CurrentLog) G> { G> ForEach($string in $SearchStrings) G> { G> $TOTALSORTED = Select-String "SORTED" $CurrentLog | G> Measure-Object G> -line | % {$_.lines} G> if ((Select-String ": CodeMissing" $CurrentLog | G> Measure-Object G> -line | % {$_.lines}) -eq $null) G> { $CMISSING = 0 } G> else G> { $CMISSING = $_.lines } G> ... G> } G> } G> It works alright, but I'd like to 1) shorten it and 2) not have to G> use the if statement to check if anyone of them is $null; I'd rather G> use a switch of something like this: G> G> $CurrentLog | % {$SearchStrings | % {$global:h=@{}} {$h[$_] += 1}} G> G> When I run this, I get 1 for each of my search strings instead of G> their count from the text file. It seems I need to add an if there, G> but am not sure where. Also, the file it runs against is ~ 20 MB and G> it takes a looooong time to process. Any thoughts? G> |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Looping Through A Text File To Find Patterns From Another Text Shay, Sort of. What I need is something like grep on *nix because I just want the numbers/counts (possibly the name- I've figured out how to remove the Group column). So just getting a count is problem 1 and problem 2 is that I'm going through a large text file and the count for at least one of the search terms is going to be huge. Here's an example after I ran your code: Count Name ----- ---- ....64 SORTED It crops the first part of the number which something like 102664. "Shay Levy [MVP]" wrote: Quote: > Hi GWold, > > See if this helps: > > $SearchStrings = (Get-Content searchstrings.txt).split(",") > Select-String -Path *.txt -Pattern $SearchStrings | group pattern > > > > > --- > Shay Levy > Windows PowerShell MVP > http://blogs.microsoft.co.il/blogs/ScriptFanatic > PowerShell Toolbar: http://tinyurl.com/PSToolbar > > > > G> Looping Through A Text File To Find Patterns From Another Text File > G> > G> All, > G> > G> I'm trying to loop through a text file searching for specific terms > G> (a la *nix grep) that are taken from another text file. Here's some > G> text from the log file to be searched: > G> > G> CodeA 12:45:27 AM LOCATION label: 1234, at lane: 21 SORTED Err30 > G> 12:45:28 AM SYSTEM: Unexpected Packages at Update 007 CodeB 12:45:29 > G> AM LOCATION label: 5678, at lane: 6 SORTED > G> > G> Here are some of the search terms from a comma-delimited text file: > G> > G> SORTED,Unexpected,Unread,: CodeMissing > G> > G> Here's the original code: > G> $SearchStrings = @((Get-Content "searchstrings.txt").split(",")) > G> ForEach($Line in $CurrentLog) > G> { > G> ForEach($string in $SearchStrings) > G> { > G> $TOTALSORTED = Select-String "SORTED" $CurrentLog | > G> Measure-Object > G> -line | % {$_.lines} > G> if ((Select-String ": CodeMissing" $CurrentLog | > G> Measure-Object > G> -line | % {$_.lines}) -eq $null) > G> { $CMISSING = 0 } > G> else > G> { $CMISSING = $_.lines } > G> ... > G> } > G> } > G> It works alright, but I'd like to 1) shorten it and 2) not have to > G> use the if statement to check if anyone of them is $null; I'd rather > G> use a switch of something like this: > G> > G> $CurrentLog | % {$SearchStrings | % {$global:h=@{}} {$h[$_] += 1}} > G> > G> When I run this, I get 1 for each of my search strings instead of > G> their count from the text file. It seems I need to add an if there, > G> but am not sure where. Also, the file it runs against is ~ 20 MB and > G> it takes a looooong time to process. Any thoughts? > G> > > > |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Looping Through A Text File To Find Patterns From Another Text Did I also mention in my reply that I DON"T want any labels? If not, there's the request. "Shay Levy [MVP]" wrote: Quote: > Hi GWold, > > See if this helps: > > $SearchStrings = (Get-Content searchstrings.txt).split(",") > Select-String -Path *.txt -Pattern $SearchStrings | group pattern > > > > > --- > Shay Levy > Windows PowerShell MVP > http://blogs.microsoft.co.il/blogs/ScriptFanatic > PowerShell Toolbar: http://tinyurl.com/PSToolbar > > > > G> Looping Through A Text File To Find Patterns From Another Text File > G> > G> All, > G> > G> I'm trying to loop through a text file searching for specific terms > G> (a la *nix grep) that are taken from another text file. Here's some > G> text from the log file to be searched: > G> > G> CodeA 12:45:27 AM LOCATION label: 1234, at lane: 21 SORTED Err30 > G> 12:45:28 AM SYSTEM: Unexpected Packages at Update 007 CodeB 12:45:29 > G> AM LOCATION label: 5678, at lane: 6 SORTED > G> > G> Here are some of the search terms from a comma-delimited text file: > G> > G> SORTED,Unexpected,Unread,: CodeMissing > G> > G> Here's the original code: > G> $SearchStrings = @((Get-Content "searchstrings.txt").split(",")) > G> ForEach($Line in $CurrentLog) > G> { > G> ForEach($string in $SearchStrings) > G> { > G> $TOTALSORTED = Select-String "SORTED" $CurrentLog | > G> Measure-Object > G> -line | % {$_.lines} > G> if ((Select-String ": CodeMissing" $CurrentLog | > G> Measure-Object > G> -line | % {$_.lines}) -eq $null) > G> { $CMISSING = 0 } > G> else > G> { $CMISSING = $_.lines } > G> ... > G> } > G> } > G> It works alright, but I'd like to 1) shorten it and 2) not have to > G> use the if statement to check if anyone of them is $null; I'd rather > G> use a switch of something like this: > G> > G> $CurrentLog | % {$SearchStrings | % {$global:h=@{}} {$h[$_] += 1}} > G> > G> When I run this, I get 1 for each of my search strings instead of > G> their count from the text file. It seems I need to add an if there, > G> but am not sure where. Also, the file it runs against is ~ 20 MB and > G> it takes a looooong time to process. Any thoughts? > G> > > > |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Looping Through A Text File To Find Patterns From Another Text Probably a formatting issue, try: Select-String -Path *.txt -Pattern $SearchStrings | group pattern | ft -auto --- Shay Levy Windows PowerShell MVP http://blogs.microsoft.co.il/blogs/ScriptFanatic PowerShell Toolbar: http://tinyurl.com/PSToolbar G> Shay, G> G> Sort of. What I need is something like grep on *nix because I just G> want the numbers/counts (possibly the name- I've figured out how to G> remove the Group column). G> G> So just getting a count is problem 1 and problem 2 is that I'm going G> through a large text file and the count for at least one of the G> search terms is going to be huge. Here's an example after I ran your G> code: G> G> Count Name G> ----- ---- G> ...64 SORTED G> It crops the first part of the number which something like 102664. G> G> "Shay Levy [MVP]" wrote: G> Quote: Quote: >> Hi GWold, >> >> See if this helps: >> >> $SearchStrings = (Get-Content searchstrings.txt).split(",") >> Select-String -Path *.txt -Pattern $SearchStrings | group pattern >> >> --- >> Shay Levy >> Windows PowerShell MVP >> http://blogs.microsoft.co.il/blogs/ScriptFanatic >> PowerShell Toolbar: http://tinyurl.com/PSToolbar >> G> Looping Through A Text File To Find Patterns From Another Text >> File >> G> >> G> All, >> G> >> G> I'm trying to loop through a text file searching for specific >> terms >> G> (a la *nix grep) that are taken from another text file. Here's >> some >> G> text from the log file to be searched: >> G> >> G> CodeA 12:45:27 AM LOCATION label: 1234, at lane: 21 SORTED Err30 >> G> 12:45:28 AM SYSTEM: Unexpected Packages at Update 007 CodeB >> 12:45:29 >> G> AM LOCATION label: 5678, at lane: 6 SORTED >> G> >> G> Here are some of the search terms from a comma-delimited text >> file: >> G> >> G> SORTED,Unexpected,Unread,: CodeMissing >> G> >> G> Here's the original code: >> G> $SearchStrings = @((Get-Content "searchstrings.txt").split(",")) >> G> ForEach($Line in $CurrentLog) >> G> { >> G> ForEach($string in $SearchStrings) >> G> { >> G> $TOTALSORTED = Select-String "SORTED" $CurrentLog | >> G> Measure-Object >> G> -line | % {$_.lines} >> G> if ((Select-String ": CodeMissing" $CurrentLog | >> G> Measure-Object >> G> -line | % {$_.lines}) -eq $null) >> G> { $CMISSING = 0 } >> G> else >> G> { $CMISSING = $_.lines } >> G> ... >> G> } >> G> } >> G> It works alright, but I'd like to 1) shorten it and 2) not have to >> G> use the if statement to check if anyone of them is $null; I'd >> rather >> G> use a switch of something like this: >> G> >> G> $CurrentLog | % {$SearchStrings | % {$global:h=@{}} {$h[$_] += 1}} >> G> >> G> When I run this, I get 1 for each of my search strings instead of >> G> their count from the text file. It seems I need to add an if >> there, >> G> but am not sure where. Also, the file it runs against is ~ 20 MB >> and >> G> it takes a looooong time to process. Any thoughts? >> G> |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Looping Through A Text File To Find Patterns From Another Text Can you attach a sample file content (in my reader it shows as one line) and how would you like the output to be? --- Shay Levy Windows PowerShell MVP http://blogs.microsoft.co.il/blogs/ScriptFanatic PowerShell Toolbar: http://tinyurl.com/PSToolbar G> Did I also mention in my reply that I DON"T want any labels? If not, G> there's the request. G> G> "Shay Levy [MVP]" wrote: G> Quote: Quote: >> Hi GWold, >> >> See if this helps: >> >> $SearchStrings = (Get-Content searchstrings.txt).split(",") >> Select-String -Path *.txt -Pattern $SearchStrings | group pattern >> >> --- >> Shay Levy >> Windows PowerShell MVP >> http://blogs.microsoft.co.il/blogs/ScriptFanatic >> PowerShell Toolbar: http://tinyurl.com/PSToolbar >> G> Looping Through A Text File To Find Patterns From Another Text >> File >> G> >> G> All, >> G> >> G> I'm trying to loop through a text file searching for specific >> terms >> G> (a la *nix grep) that are taken from another text file. Here's >> some >> G> text from the log file to be searched: >> G> >> G> CodeA 12:45:27 AM LOCATION label: 1234, at lane: 21 SORTED Err30 >> G> 12:45:28 AM SYSTEM: Unexpected Packages at Update 007 CodeB >> 12:45:29 >> G> AM LOCATION label: 5678, at lane: 6 SORTED >> G> >> G> Here are some of the search terms from a comma-delimited text >> file: >> G> >> G> SORTED,Unexpected,Unread,: CodeMissing >> G> >> G> Here's the original code: >> G> $SearchStrings = @((Get-Content "searchstrings.txt").split(",")) >> G> ForEach($Line in $CurrentLog) >> G> { >> G> ForEach($string in $SearchStrings) >> G> { >> G> $TOTALSORTED = Select-String "SORTED" $CurrentLog | >> G> Measure-Object >> G> -line | % {$_.lines} >> G> if ((Select-String ": CodeMissing" $CurrentLog | >> G> Measure-Object >> G> -line | % {$_.lines}) -eq $null) >> G> { $CMISSING = 0 } >> G> else >> G> { $CMISSING = $_.lines } >> G> ... >> G> } >> G> } >> G> It works alright, but I'd like to 1) shorten it and 2) not have to >> G> use the if statement to check if anyone of them is $null; I'd >> rather >> G> use a switch of something like this: >> G> >> G> $CurrentLog | % {$SearchStrings | % {$global:h=@{}} {$h[$_] += 1}} >> G> >> G> When I run this, I get 1 for each of my search strings instead of >> G> their count from the text file. It seems I need to add an if >> there, >> G> but am not sure where. Also, the file it runs against is ~ 20 MB >> and >> G> it takes a looooong time to process. Any thoughts? >> G> |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| find text in file how | Vista General | |||
| Howto: Add lines of text from a specific point in a text file.. | VB Script | |||
| Find text within file | Vista General | |||
| Find text within file? | Vista General | |||
| How do I read a text file and sort text by fixed positions? | PowerShell | |||