0

我想在文件 (std_serverX.out) 中搜索字符串cpu=的值,即 11 个字符或更大。该文件可以包含最多或超过 100 万行的任何位置。

为了进一步限制搜索,我希望在第一次出现字符串Java Thread Dump后开始搜索cpu=。在我的源文件中,字符串Java Thread Dump直到大约 1057465 行长的文件的 # 1013169 行才开始因此Java Thread Dump之前的 96%是不必要的。

这是我要搜索的文件的一部分:

cpu=191362359.38 [reset 191362359.38] ms elapsed=1288865.05 [reset 1288865.05] s allocated=86688238148864 B (78.84 TB) [reset 86688238148864 B (78.84 TB)] defined_classes=468 
io= file i/o: 588014/275091 B, net i/o: 36449/41265 B, files opened:19, socks opened:0 [reset file i/o: 588014/275091 B, net i/o: 36449/41265 B, files opened:19, socks opened:0 ] 
user="Guest" application="JavaEE/ResetPassword" tid=0x0000000047a8b000 nid=0x1b10 / 6928 runnable [_thread_blocked (_call_back), stack(0x0000000070de0000,0x0000000070fe0000)] [0x0000000070fdd000] java.lang.Thread.State: RUNNABLE

上面可以看到 cpu= 191362359.38有 12 个字符长(包括句号和 2 个小数位)。如何匹配它以便忽略小于 11 个字符的cpu=值并且不打印到文件中?

这是我到目前为止所拥有的:

Get-Content -Path .\std_server*.out | Select-String '(cpu=)' | out-File  -width 1024 .\output.txt

我已将我的命令简化为绝对基础,因此我不会被其他搜索要求弄糊涂。

另外,我希望这个命令尽可能基本,如果可能的话,它可以在 Powershell 的一个命令行中运行。所以没有高级脚本或定义的变量,如果我们可以避免的话...... :)

这与我打开的先前消息有关,由于我没有准确定义我的要求而变得复杂。

在此先感谢您的帮助。

安东

4

2 回答 2

0

它当然可以做到,但是管道一百万行,你知道的前 96% 没有相关性不会很快/有效。

一种更快的方法是使用 aStreamReader并跳过这些行,直到Java Thread Dump找到字符串:

$CPULines = @()

foreach($file in Get-Item .\std_server*.out)
{

    # Create stream reader from file
    $Reader = New-Object -TypeName 'System.IO.StreamReader' -ArgumentList $file.FullName
    $JTDFound = $false

    # Read file line by line
    while(($line = $Reader.ReadLine()))
    {
        # Keep looking until 'Java Thread Dump' is found 
        if(-not $JTDFound)
        {
            $JTDFound = $line.Contains('Java Thread Dump')
        }
        else
        {
            # Then, if a value matching your description is found, add that line to our results
            if($line -match '^cpu=([\d\.]{11,})\s')
            {
                $CPULines += $line
            }
        }
    }

    # dispose of the stream reader
    $Reader.Dispose()
}

# Write output to file
$CPULines |Out-File .\output.txt
于 2016-02-05T10:32:52.513 回答
0

正则表达式查找 9 位数字,后跟一个文字.,后跟 1 个或多个数字。全部一条线

Get-Content -Path .\std_server*.out | 
 Select-String -Pattern 'cpu=\d{9}\.\d+' -AllMatches | 
  Select-Object -ExpandProperty matches  | 
    Select-Object -ExpandProperty value
于 2016-02-05T10:30:09.357 回答