打开文本文件并计算每个IP列出的次数

时间:2018-06-07 19:53:25

标签: powershell-v4.0

我有一个包含大量IP地址的文本文件,每个不同的IP可能会多次出现在文本文件中,因此我尝试编写(或获取帮助写入)Powershell脚本将执行以下操作: 1 - 打开文本文件,计算每个IP列出的次数和 2 - 如果总数大于20,则将该IP附加到名为Banned.txt的另一个文件

这个脚本运行后,我会在几天后手动删除文件(文件是由另一个程序创建的),我会再次使用这个脚本。

下面'我最好的尝试:

$BadIP = Get-Content -Path G:\ips.txt

foreach ($ip in $BadIP) {
    $matches = Select-String -InputObject $BadIP -Pattern $ip -AllMatches
    $a = $matches.Matches.Count
    $ip + " " + $a
}

以上代码有效,但一次又一次显示相同的ip。结果如下:

197.3.11.26   1
188.128.119.172   1
64.71.18.150   1
212.92.107.105   51
95.213.162.132   1
212.92.107.105   51
212.92.107.105   51
212.92.123.202   48
64.74.185.234   1
212.92.107.105   51
212.92.107.105   51
212.92.115.227   45
212.92.123.202   48
212.92.107.105   51
212.92.115.227   45
212.92.123.202   48

如您所见,212.92.107.105多次显示。

更新:工作代码:

$BadIP = Get-Content -Path G:\ips.txt
foreach ($ip in $BadIP) {
    $matches = Select-String -InputObject $BadIP -Pattern $ip -AllMatches
    $a = $matches.Matches.Count
    if ($a -gt 10) {
        Add-Content -Path G:\NewBanned.txt -Value $ip -Force 
    }
}

$B = Get-Content -Path G:\NewBanned.txt | sort | Get-Unique
Set-Content -Path G:\C.txt -Value $B -Force
197.3.11.26
188.128.119.172
64.71.18.150
212.92.107.105
95.213.162.132
212.92.107.105
212.92.107.105
212.92.123.202
64.74.185.234
212.92.107.105
212.92.107.105
212.92.115.227
212.92.123.202
212.92.107.105
212.92.115.227
212.92.123.202
79.175.133.67
212.92.123.202
212.92.107.105
212.92.115.227
212.92.123.202
212.92.107.105
186.202.182.154
212.92.115.227
23.97.190.194
212.92.123.202
116.247.79.114
212.92.107.125
212.92.107.105
95.154.89.97
212.92.115.227
212.92.123.202
212.92.107.105
37.61.220.2
212.92.115.227
216.210.86.226
212.92.107.105
212.92.115.227
212.92.123.202
212.92.107.105
212.92.115.227
212.92.123.202
212.92.123.202
212.92.115.227
212.92.107.105
212.92.123.202
212.92.115.227
212.92.107.105
212.92.123.202
212.92.115.227
212.92.107.105
212.92.123.202
103.196.30.114
212.92.115.227
212.92.107.105
212.92.123.202
212.92.115.227
212.92.107.105
212.92.123.202
212.92.115.227
212.92.107.105
72.43.207.8
212.92.123.202
52.176.111.87
51.15.147.173
212.92.115.227
212.92.107.105
212.92.123.202
212.92.115.227
212.92.107.105
212.92.123.202
51.15.147.173
212.92.115.227
212.92.123.202
212.92.107.105
80.241.45.18
51.15.147.173
212.92.115.227
212.92.123.202
212.92.107.105
212.92.115.227
212.92.123.202
212.92.107.105
51.15.147.173
212.92.123.202
212.92.115.227
212.92.107.105
51.15.147.173
212.92.123.202
212.92.107.105
212.92.115.227
121.122.140.41
61.222.127.100
187.216.131.254
138.201.35.26
212.92.123.202
212.92.107.105
212.92.115.227
51.15.147.173
162.250.124.186
212.92.123.202
212.92.107.105
212.92.115.227
212.92.123.202
51.15.147.173
212.92.107.105
212.92.115.227
212.92.123.202
212.92.107.105
51.15.147.173
212.92.115.227
212.92.123.202
198.71.53.228
212.92.107.105
212.92.115.227
212.92.123.202
51.15.147.173
212.92.107.105
212.92.115.227
212.92.123.202
121.122.140.41
212.92.107.105
51.15.147.173
212.92.115.227
212.92.123.202
212.92.107.105
212.92.115.227
212.92.123.202
51.15.147.173
212.92.107.105
212.92.123.202
212.92.115.227
212.92.107.105
51.15.147.173
212.92.123.202
212.92.115.227
138.201.35.26
212.92.107.105
212.92.123.202
51.15.147.173
124.97.39.218
212.92.115.227
212.92.107.105
212.92.123.202
212.92.115.227
51.15.147.173
212.92.107.105
212.92.123.202
212.92.115.227
212.92.107.105
212.118.13.124
212.92.123.202
212.92.115.227
212.92.107.105
212.92.123.202
83.37.113.40
212.92.115.227
212.92.107.105
212.92.123.202
212.92.107.105
212.92.115.227
212.92.123.202
78.110.10.245
212.92.107.105
212.92.115.227
212.92.123.202
38.110.28.23
212.92.107.105
212.92.115.227
212.92.123.202
212.92.107.105
212.92.115.227
212.92.123.202
212.92.107.105
212.92.123.242
212.92.123.202
212.92.115.227
212.92.107.105
212.92.123.202
212.92.115.227
212.92.107.105
212.92.115.227
212.92.107.105
212.92.123.202
88.250.238.17
212.92.115.227
212.92.107.105
88.250.238.17
212.92.123.202
212.92.115.227
212.92.107.105
208.98.255.113
124.97.39.218
177.84.35.46
1.174.101.155

1 个答案:

答案 0 :(得分:0)

让PowerShell为您做繁重的工作。 Group-Object cmdlet将相同的输入对象分组并自动计算它们:

Get-Content 'G:\NewBanned.txt' |
    Group-Object |
    Select-Object Name, Count

输出到这样的文本文件:

Get-Content 'G:\NewBanned.txt' |
    Group-Object |
    ForEach-Object { '{0}    {1}' -f $_.Name, $_.Count } |
    Set-Content 'G:\C.txt'

输出到CSV文件,如下所示:

Get-Content 'G:\NewBanned.txt' |
    Group-Object |
    Select-Object @{n='IPAddress';e={$_.Name}}, Count
    Export-Csv 'G:\C.csv' -NoType

后者的计算属性只是为了取代" Name"使用" IPAddress"作为列标题。