开发者

Count all occurences of the string in files and group the output

I have a script that counts all occurences of ":/" string in one file.

$Sourcefile = 'K:\SRCFILES\Source.txt'
$Destfile = 'K:\DSTFILES\Destination.txt'

Get-Content $Sourcefile -Readcount 0 |
 ForEach-Object { $_ -match ":/" | out-file $Destfile
}
(Get-Content $Destfile -Readcount 0).Length

However, in the source directory ($SourceDir = 'K:\SRCFI开发者_运维技巧LES' ) I have some more text files and I would like to count how many times that string appears in each file and group everything by filename like this:

textfile1.txt - 20
textfile2.txt - 2

No idea where to start. I've tried to adapt this script:

Get-ChildItem $SourceDir -Include '*.txt' | 
Select-String -Pattern ':/' |
ForEach-Object { '{0}' -f $_.Filename , $_.Line } |
Out-File $Destfile 

But I am stuck.

Also, a quick question, why this script works well with ":" string, but does not work with ":/"?

-split (Get-Content $Sourcefile | Out-String) | 
Where-Object { $_ -eq ":" } | Measure-Object |
Select-Object -exp count
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜