PowerShell中进行文件读取,信息排序,分类计数。
2015-05-30 15:26
513 查看
这是国外某大学QA的一道作业题,读取mainlog文件中的每一行信息,并获取有效的信息,也就是每条信息中第四个@后面的内容,然后进行分类与计数,要求是用Perl写,但我是用PowerShell完成的,文件截图如下:
代码如下:
测试结果如下:
代码如下:
<# $logPath = "C:\Users\tylan\Desktop\mainlog.txt" #> function CheckInfo { param($logPath) $logContent = Get-Content $logPath $reg = "^.+@.+@.+@.+@.+$" $categories = "","" foreach($log in $logContent) { if($log -match $reg) { $partialLog = $log.split("@") $category = $partialLog[4] $categories += $category } } $sortResult = $categories|Sort-Object|Get-Unique foreach($cate in $sortResult) { $cate|Add-Member -MemberType NoteProperty -Name "cateCount" -Value 0 } foreach($category in $categories) { foreach($cate in $sortResult) { if($category -eq $cate) { $cate.cateCount ++ } } } foreach($cate in $sortResult) { if($cate -ne "") { $cate + " : " + $cate.cateCount } } } $logPath = "C:\Users\tylan\Desktop\mainlog.txt" CheckInfo $logPath
测试结果如下:
相关文章推荐
- linux学习笔记—shell概述
- KalilMetasploit生成cmdshell(未免杀)
- Linux&nbsp;Shell常用技巧(目录)
- shell脚本学习
- tlcl-从shell眼中看世界echo
- Bash中的特殊字符
- Linux Bash编程
- [LeetCode][Shell]Tenth Line
- Linux Shell脚本攻略(1.7)
- BASH
- win7(windows) 下 git bash 调用 notepad++ 或者其他编辑器的方法
- shell
- (转)xshell无法在vim中复制黏贴
- Windows Shell 程序
- PowerShell Azure 常用命令
- Shell 基本命令
- shell数组的使用
- shell数组的使用
- bash:haoop:command not found
- shell 第一站 创建shell脚本