Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

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.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Reading text files using powershell

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 02-29-2008   #1 (permalink)
marzy
Guest


 

Reading text files using powershell

Hi all.

I have a particular problem.

I have some log files that I want to search. I would like to use powershell
to read the files and find a particular word.
e.g. search for the word error in the log and tell me how many times it
finds the word and report back.

any help much appreciated.


My System SpecsSystem Spec
Old 02-29-2008   #2 (permalink)
Keith Hill [MVP]
Guest


 

Re: Reading text files using powershell

"marzy" <marzy@xxxxxx> wrote in message
news:A203B368-A62A-4DEB-A08D-F8F1A2130321@xxxxxx
Quote:

> Hi all.
>
> I have a particular problem.
>
> I have some log files that I want to search. I would like to use
> powershell
> to read the files and find a particular word.
> e.g. search for the word error in the log and tell me how many times it
> finds the word and report back.
>
> any help much appreciated.
>
$regex = [regex]'(?i)\berror\b'
Get-ChildItem *.log -r | Get-Content | Foreach { $regex.Matches($_) } |
measure-object

--
Keith

My System SpecsSystem Spec
Old 02-29-2008   #3 (permalink)
Kiron
Guest


 

Re: Reading text files using powershell

If you want a count per log file report here are a couple of ways of doing it in v1.0 and v2.0 CTP

$word = 'Error'
# v1.0
Get-ChildItem *.log |
Where-Object {Select-String "\b$word\b" $_.fullname -quiet} |
Select-Object FullName, @{name = 'Count';
expression = {@((Get-Content $_.fullname |
Out-String).split() -match "\b$word\b").count}} |
Format-Table -auto

# faster
Get-ChildItem *.log |
Where-Object {Select-String "\b$word\b" $_.fullname -quiet} |
Select-Object FullName, @{name = 'Count';
expression = {@([regex]::matches((Get-Content $_.fullname),
"\b$word\b", 1)).count}} |
Format-Table -auto

# one-liner versions
ls *.log|?{Select-String "\b$word\b" $_.fullname -q}|select FullName,@{n='Count';e={@((gc $_.fullname|out-string).split()-match"\b$word\b").count}}|ft -a
# faster
ls *.log|?{Select-String "\b$word\b" $_.fullname -q}|select FullName,@{n='Count';e={@([regex]::matches((gc $_.fullname),"\b$word\b",1)).count}}|ft -a

### v2.0 CTP ###
Get-ChildItem *.log |
Where-Object {Select-String "\b$word\b" $_.fullname -quiet} |
Select-Object FullName, @{name = 'Count';
expression = {@((gc $_.fullname |
Out-String) -split ' ' -match "\b$word\b").count}} |
Format-Table -auto
# fastest
Get-ChildItem *.log |
Where-Object {Select-String "\b$word\b" $_.fullname -quiet} |
Select-Object FullName, @{name = 'Count';
expression = {Select-String "\b$word\b" $_.fullname -all |
ForEach-Object {$total = 0} {$total += $_.matches.count} {$total}}} |
Format-Table -auto

# one-liner versions
ls *.log|?{Select-String "\b$word\b" $_.fullname -q}|select FullName,@{n='Count';e={@((gc $_.fullname|Out-String)-split' '-match"\b$word\b").count}}|ft -a
# fastest
ls *.log|?{Select-String "\b$word\b" $_.fullname -q}|select FullName,@{n='Count';e={Select-String "\b$word\b" $_.fullname -all|%{$total=0}{$total+=$_.matches.count}{$total}}}|ft -a

--
Kiron
My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading text file and charting them via powergadget IT Staff PowerShell 2 03-19-2008 08:18 AM
Searching for content in text files with powershell snofire PowerShell 5 12-31-2007 08:55 PM
Text reading software.. ellisfaith Vista General 6 11-13-2007 09:24 AM
Reading event log files - PowerShell and LogParser 2.2 Rob Campbell PowerShell 0 04-11-2007 03:14 PM
Reading the BOM of text files Maximilian Hänel PowerShell 5 10-16-2006 02:50 PM


Vistax64.com 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 2005-2008

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 47 48 49 50 51