解析信息并仅返回匹配的信息

时间:2019-06-21 00:04:00

标签: powershell

我只需要从结果中返回5或更大的正数即可得到帮助

F([1,1,1]) = I([1,1,1]) + 1 = 0 + 4*0 + (4^2)*0 + 1 = 1
F([2,1,1]) = I([2,1,1]) + 1 = 1 + 4*0 + (4^2)*0 + 1 = 2
F([3,1,1]) = I([2,1,1]) + 1 = 2 + 4*0 + (4^2)*0 + 1 = 3
F([4,1,1]) = I([2,1,1]) + 1 = 3 + 4*0 + (4^2)*0 + 1 = 4
...
F([2,1,3]) = I([2,2,3]) + 1 = 1 + 4*1 + (4^2)*2 + 1 = 38
...
F([1,1,4]) = I([1,1,4]) + 1 = 0 + 4*0 + (4^2)*3 + 1 = 49
...
F([4,4,4]) = I([4,4,4]) + 1 = 3 + 4*3 + (4^2)*3 + 1 = 64

文件testttt.txt:其中的一些内容

$string = "positives"
$fileread = "C:\Users\test\desktop\testttt.txt"
Select-String $string $fileread -Context 0, 2 | % { $_.line + $_.Context.PostContext } | format-table

这是我运行命令时的结果:

  "date": "2019-05-13 17:14:10",
      "positives": 4,
      "total": 72,
      "sha256":"7b4aceaadd78ec496c6fd9a284d7411c5085f4efd156e7fe2bac0df872a01034"

2 个答案:

答案 0 :(得分:1)

假设您的文件如下所示:

"date": "2019-05-13 17:14:10",
"positives": 3,
"total": 73,
"sha256":"3b4aceaadd78ec496c6fd9a284d7411c5085f4efd156e7fe2bac0df872a01034"
"date": "2019-05-13 17:14:10",
"positives": 4,
"total": 74,
"sha256":"4b4aceaadd78ec496c6fd9a284d7411c5085f4efd156e7fe2bac0df872a01034"
"date": "2019-05-13 17:14:10",
"positives": 5,
"total": 75,
"sha256":"5b4aceaadd78ec496c6fd9a284d7411c5085f4efd156e7fe2bac0df872a01034"
"date": "2019-05-13 17:14:10",
"positives": 6,
"total": 76,
"sha256":"6b4aceaadd78ec496c6fd9a284d7411c5085f4efd156e7fe2bac0df872a01034"

然后您只能获得“正数:X”且X大于或等于5的项目,像这样:

$pattern = "`"positives`": (\d+)"
$targetFile = ".\testttt.txt"

Select-String -Path $targetFile -Pattern $pattern -Context 0,2 |
    Where-Object { $_.Matches[0].Groups[1].Value -ge 5 } |
        ForEach-Object {
            $total = $($_.Context.PostContext[0] -match ": (\d+)" | Out-Null; $Matches)
            $hash = $($_.Context.PostContext[1] -match "`"(\w+)`":`"([a-z0-9]+)`"" | Out-Null; $Matches)

            [PsCustomObject]@{
                Positives = $_.Matches[0].Groups[1].Value
                Total     = $total[1]
                Algorithm = $hash[1]
                HashValue = $hash[2]
            }
        }

有点笨拙,但这确实为您提供了很好的对象集合,以便于操作或显示:

Positives Total Algorithm HashValue                                                       
--------- ----- --------- ---------                                                       
5         75    sha256    5b4aceaadd78ec496c6fd9a284d7411c5085f4efd156e7fe2bac0df872a01034
6         76    sha256    6b4aceaadd78ec496c6fd9a284d7411c5085f4efd156e7fe2bac0df872a01034

答案 1 :(得分:0)

这将满足需要:

$string = "positives`": *([5-9]|\d\d+)"
$fileread = "Y:\testttt.txt"
Select-String -Path $fileread -Pattern $string -Context 0,2 | % { $_.line + $_.Context.PostContext }

示例结果:

  "positives": 5,      "total": 71,       "sha256": "6d371555e08ba2b2397fd44a0db31605e7def831585c4c11dbb21c70d89e3b35"
  "positives": 14,      "total": 70,       "sha256": "c5bd171025b10ba777008c349b26863a5259a7f45ad46ab71d9f227a2959ab83"
  "positives": 1964,      "total": 70,       "sha256": "c5bd171025b10ba777008c349b26863a5259a7f45ad46ab71d9f227a2959ab83"

使用的测试文件:

  "date": "2019-05-13 17:14:10",
      "positives": 4,
      "total": 72,
      "sha256":"7b4aceaadd78ec496c6fd9a284d7411c5085f4efd156e7fe2bac0df872a01034"
  "date": "2019-05-13 17:14:10",
      "positives": 5,
      "total": 71,
      "sha256": "6d371555e08ba2b2397fd44a0db31605e7def831585c4c11dbb21c70d89e3b35"
  "date": "2019-05-13 17:14:10",
      "positives": 4,
      "total": 72,
      "sha256":"7b4aceaadd78ec496c6fd9a284d7411c5085f4efd156e7fe2bac0df872a01034"
  "date": "2019-05-13 17:14:10",
      "positives": 14,
      "total": 70,
      "sha256": "c5bd171025b10ba777008c349b26863a5259a7f45ad46ab71d9f227a2959ab83"
  "date": "2019-05-13 17:14:10",
      "positives": 1964,
      "total": 70,
      "sha256": "c5bd171025b10ba777008c349b26863a5259a7f45ad46ab71d9f227a2959ab83"

由于较新的答案排在第一位,因此看起来他们更有可能被看成第一名,并且被认为是第一名,这有点奇怪。

好吧,无论如何,昨晚我还有另外两个版本想要发布,但是实际上不得不赶上火车。

这两种方法都通过消除对For Loop的使用来改进脚本,从而以精确的原始格式以及作为表格输出更快地生成结果。=

这将比使用For循环更快地生成最初概述的结果:

$string = "positives`": *([5-9]|\d\d+)"
$fileread = "Y:\testttt.txt"
(Select-String $string $fileread -Context 0, 2 | select  @{n="Results";e={$_.line + $_.Context.PostContext} } ).Results

示例结果:

PS Y:\> (Select-String $string $fileread -Context 0, 2 | select  @{n="Results";e={$_.line + $_.Context.PostContext} } ).Results
      "positives": 5,      "total": 71,       "sha256": "6d371555e08ba2b2397fd44a0db31605e7def831585c4c11dbb21c70d89e3b35"
      "positives": 14,      "total": 70,       "sha256": "c5bd171025b10ba777008c349b26863a5259a7f45ad46ab71d9f227a2959ab83
      "positives": 1964,      "total": 70,       "sha256": "c5bd171025b10ba777008c349b26863a5259a7f45ad46ab71d9f227a2959ab83"

此方法将信息解析到具有相关信息列的表中,而没有For循环,这将使其更快。

$string = "positives`": *([5-9]|\d\d+)"
$fileread = "Y:\testttt.txt"
Select-String $string $fileread -Context 0, 2 |  select (
    @{n="Positives";e={$($("$($_.line)" -split ':')[1]).trim() -replace '[\t ,]*','' } },
    @{n="Total";e={$($($($_.Context.PostContext) -split ':')[1]).trim() -replace ',.*','' } },
    @{n="Cipher";e={$($($($($_.Context.PostContext) -split ':') -split ',')[3]).trim() -replace '[\t "]+','' } },
    @{n="Hash";e={$($($($_.Context.PostContext) -split ':')[3]).trim() -replace '[\t "]+','' } }
) | FT

结果如下:

Positives Total Cipher Hash                                                            
--------- ----- ------ ----                                                            
5         71    sha256 6d371555e08ba2b2397fd44a0db31605e7def831585c4c11dbb21c70d89e3b35
14        70    sha256 c5bd171025b10ba777008c349b26863a5259a7f45ad46ab71d9f227a2959ab83
1964      70    sha256 c5bd171025b10ba777008c349b26863a5259a7f45ad46ab71d9f227a2959ab83