简体   繁体   English

是否有任何 powershell 命令来计算日志文件中一行中的 id 数?

[英]is there any powershell command to count the number of ids in a line on log file?

is there any powershell command to count the number of ids in a line on log file?是否有任何 powershell 命令来计算日志文件中一行中的 id 数? for example imagine this a line in log file -例如想象这是日志文件中的一行 -

2021-04-27 15:12:04.5797 INFO Generated Call Log IDs: 4219860,4219861,4219862,4219863,4219864,4219865,4219866,4219867,4219868,4219869,4219870,4219871,4219872,4219873,4219874,4219875,4219876,4219877,4219878,4219879,4219880,4219881,4219882,4219883. 2021-04-27 15:12:04.5797 INFO Generated Call Log IDs: 4219860,4219861,4219862,4219863,4219864,4219865,4219866,4219867,4219868,4219869,4219870,4219871,4219872,4219873,4219874,4219875,4219876, 4219877,4219878,4219879,4219880,4219881,4219882,4219883。

I have to check the number of event log ids generated in the log for the particular log completion time in this case it is 15:12:04 cst.我必须检查特定日志完成时间在日志中生成的事件日志 ID 的数量,在这种情况下是 15:12:04 cst。

$line ="2021-04-27 15:12:04.5797 INFO Generated Call Log IDs: 4219860,4219861,4219862,4219863,4219864,4219865,4219866,4219867,4219868,4219869,4219870,4219871,4219872,4219873,4219874,4219875,4219876,4219877,4219878,4219879,4219880,4219881,4219882,4219883."

(($line -split "IDs: ")[1] -split ","  | Measure-Object).Count
# data simulating log
$log = @'
2021-04-27 15:12:04.5797 INFO Generated Call Log IDs: 4219860,4219861,4219862,4219863,4219864,4219865,4219866,4219867,4219868,4219869,4219870,4219871,4219872,4219873,4219874,4219875,4219876,4219877,4219878,4219879,4219880,4219881,4219882,4219883.
2021-04-27 15:15:04.5797 INFO Generated Call Log IDs: 4219860,4219861,4219862,4219863,4219864,4219865,4219866,4219867,4219868,4219869,4219870,4219871,4219872.
'@ -split '\r?\n'

$log | ForEach-Object {
    # Regex with named capturing groups to get date and ids
    if ($_ -match '(?<date>^(?:\d+-?)+\s(?:\d+:?)+).*?IDs:\s?(?<ids>(?:\d+,?)+)\.?') {
        $ids = $Matches.ids -split ','

        # Create output objects with date, ids, and id count
        [pscustomobject]@{date = [datetime]::parse($Matches.date); count = $ids.Count; ids = $ids }        }
}

Output Output

date                count ids
----                ----- ---
2021-04-27 15:12:04    24 {4219860, 4219861, 4219862, 4219863…}
2021-04-27 15:15:04    13 {4219860, 4219861, 4219862, 4219863…}

Update更新

Based off your repost of this question it seems like you want something a bit more focused.根据您对这个问题的转发,您似乎想要一些更专注的东西。 Its sounds like you want to get count of IDs for a specific date/time string found in the log.听起来您想获取日志中找到的特定日期/时间字符串的 ID 计数。

function Get-IdCountFromLog {
    [cmdletbinding()]
    param(
        [Parameter()]
        [ValidateScript( { Test-Path $_ })]
        [string]
        $Path,

        [Parameter()]
        [string]
        $DateStringPattern = '.*',

        # Use to return only count of IDs
        [Parameter()]
        [switch]
        $Count
    )

    Get-Content -Path $Path |
        Select-String -Pattern $DateStringPattern  |
        Select-Object -ExpandProperty Line |
        ForEach-Object {
            # Regex with named capturing groups to get date and ids
            if ($_ -match '(?<date>^(?:\d+-?)+\s(?:\d+:?)+\.\d+).*?IDs:\s?(?<ids>(?:\d+,?)+)\.?') {
                $ids = $Matches.ids -split ','

                if ($Count.IsPresent) { $ids.Count }
                else {
                    # Create output objects with date, ids, and id count
                    [pscustomobject]@{date = $Matches.date; count = $ids.Count; ids = $ids }
                }
            }
        }
}

Get-IdCountFromLog -Path C:\temp\log.txt -DateStringPattern '2021-06-01 04:51:54.3094' -Count

8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在命令行/ PowerShell Windows 7中对任何站点执行ping操作 - unable to ping any sites in command line/powershell windows 7 Copy-Item命令在Powershell命令行中有效,但在bat文件中无效 - Copy-Item command working in Powershell Command line but not in bat file 替换PowerShell文件中的字符串,然后通过批处理+命令行参数调用它 - Replacing strings in a PowerShell file and then calling it via batch + command line arguments 从Windows命令行或Powershell横向打印文件 - print a file in landscape from Windows command line or powershell 是否有powershell命令来选择具有特定字符串(时间)的行并且必须打印计数值生成的总调用数:156? - Is there powershell command to select the line with particular string(time) and have to print count values Total calls Generated:156? powershell传递命令行参数 - powershell pass command line arguments powershell 2.0命令行重定向 - powershell 2.0 command line redirection 如何使用windows命令行查找文件中字符串的出现次数? - How to find the number of occurrences of a string in file using windows command line? 在命令行上执行一个简单的powershell命令 - Executing a simple powershell command on the command line 从SVN命令行中提取文件的修订号 - Extracting the Revision number of a file from the SVN Command line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM