我希望从文件中提取带有主机名一词的行。我正在搜索的文件是来自不同网络交换机的日志。我想从每个日志中提取主机名,然后我想返回每个端口名,以及端口的 crc 错误。
这是代码,日志示例在代码下方。我将要从日志文件中拉出的行加粗。
# This pulls the port numbers out of each file
[string[]]$port_counter = Select-String -Path C:\Users\tlyle\OneDrive\Documents\Log\*.txt -Pattern 'is up', 'is down'
# this pulls the crc errors out of each file
[string[]]$crc_logs = Select-String -Path C:\Users\tlyle\OneDrive\Documents\Log\*.txt -Pattern 'crc'
# Below I combine the arrays and print them out
$MaxLen = [Math]::Max($port_counter.Length, $crc_logs.Length)
$Result = @()
for ($i = 0; $i -lt $MaxLen; $i++) {
$Result += $port_counter[$i]
$Result += $crc_logs[$i]
}
$Result
这目前为我拉这个。
:GigabitEthernet1/1/1 已启动,线路协议已启动
:0 输入错误,0 CRC,0 帧,0 被忽略
:GigabitEthernet1/1/2 已启动,线路协议已启动
0 个输入错误,0 个 CRC,0 个帧,0 个被忽略
然后对文件夹中的每个文件重复。
这是输入文件的样子 - 每个文件有 48 个端口 - GigabitEthernet1/1/1 到 1/1/48 - 我为文件中的每个端口拉出2 行- 下面以粗体显示 - 并对中的每个文件执行此操作目录..我想拉出文件的第一行并在端口和crc信息之前返回它-对于每个文件--这将首先显示主机名,然后是端口和crc错误,然后转到下一个文件和显示一次主机名,然后是端口和 crc 错误。.
GigabitEthernet1/1/1 已启动,线路协议已启动
端口已启动 490 天 3 小时 48 分钟 56 秒
硬件为千兆以太网,地址为 609c.9f77.2d44 (bia 609c. 9f77.2d44)
配置速度自动,实际 1Gbit,配置双工 fdx,实际 fdx
配置 mdi 模式 AUTO,实际 MDI
EEE 功能禁用
L2 VLAN ID 281 的成员,端口未标记,端口状态为 FORWARDING
BPDU 保护已启用,ROOT 保护为Disabled, Designated protection is Disabled
Link Error Dampening is Enabled
STP 配置为 ON,优先级为 level0,mac-learning 已启用
Loop Detection is ENABLED
Flow Control is config enabled, oper enabled,协商 disabled
Mirror disabled, Monitor disabled
Mac 通知被禁用
不是任何活动中继的
成员 不是任何已配置中继的成员
端口名称是 dev-wap
IPG MII 0 位时间,IPG GMII 0 位时间
MTU 10200 字节
300 秒输入速率:21600 位/秒,13数据包/秒,0.00% 利用率
300 秒输出速率:126592 比特/秒,17 数据包/秒,0.01% 利用率
1078349 数据包输入,213724458 字节,0 无缓冲区
收到 0 广播,3719 多播,107cast4630
CRC 输入错误, 0 帧, 0 忽略
0
短帧, 0 巨人1884404 数据包输出, 2300283173 字节, 0 欠载
传输 85 次广播, 138932 多播, 1745387 单播
0 输出错误, 0 冲突
中继代理信息选项:禁用
UC Egress queues:
Queue counters Queued packets Dropped Packets
0 1733399 0
1 1381 0
2 14 0
3 6245 0
4 1194 0
5 7 0
6 105 0
7 58847 0
MC Egress queues:
Queue counters Queued packets Dropped Packets
0 82570 0
1 25 0
2 595 0
3 23 0
GigabitEthernet1/1/2 已启动,线路协议已启动
端口已启动 473 天 9 小时 17 分钟 50 秒
硬件为千兆以太网,地址为 609c.9f77.2d45 (bia 609c. 9f77.2d45)
配置速度自动,实际 1Gbit,配置双工 fdx,实际 fdx
配置 mdi 模式 AUTO,实际 MDIX
EEE 功能禁用
L2 VLAN ID 281 成员,端口未标记,端口状态为 FORWARDING
BPDU 保护已启用,ROOT 保护为Disabled, Designated protection is Disabled
Link Error Dampening is Enabled
STP 配置为 ON,优先级为 level0,mac-learning 已启用
Loop Detection is ENABLED
Flow Control is config enabled, oper enabled,协商 disabled
Mirror disabled, Monitor disabled
Mac 通知被禁用
不是任何活动中继的
成员 不是任何已配置中继的成员
端口名称是 dev-wap
IPG MII 0 位时间,IPG GMII 0 位时间
MTU 10200 字节
300 秒输入速率:4704 位/秒,2数据包/秒,0.00% 利用率
300 秒输出速率:5320 比特/秒,3 数据包/秒,0.00% 利用率
170203 个数据包输入,37873738 字节,0 无缓冲区
收到 0 个广播,3720 个多播,166483 个单播
,0 个 CRC 输入错误, 0 帧, 0 忽略
0
矮小, 0 巨人365548 数据包输出, 160575794 字节, 0 欠载
传输 85 次广播, 138931 多播, 226532 单播
0 输出错误, 0 冲突
中继代理信息选项:禁用
UC Egress queues:
Queue counters Queued packets Dropped Packets
0 220453 0
1 83 0
2 1 0
3 641 0
4 973 0
5 3717 0
6 46 0
7 56422 0
MC Egress queues:
Queue counters Queued packets Dropped Packets
0 82569 0
1 25 0
2 595 0
3 23 0
这是我想要的最终结果:
主机“10.16.156.76”
:GigabitEthernet1/1/1 is up, line protocol is up
:0 输入错误,0 CRC,0 帧,0 被忽略
:GigabitEthernet1/1/2 已启动,线路协议已启动
0 输入错误、0 CRC、0 帧、0 被忽略
然后对文件夹中的每个文件重复。
你太复杂了。
您应该能够将 RegEx 匹配或 Select-String 与 RegEx 模式一起使用,以仅抓取带有您要查找的文本的两行。
我刚刚复制了您的示例日志,将其保存为单个 txt 文件,然后执行以下操作。
例子:
# Use RegEx matches to match / select the line that contains the
Get-Content -Path 'D:\Temp\Backup\RouterLog.txt' |
%{[regex]::Matches($PSItem,'^.*\b(protocol|CRC)\b.*$').Value}
# Use Select-String RegEx to match / select the line that contains the pattern
Get-Content -Path 'D:\Temp\Backup\RouterLog.txt' |
Select-String '^.*\b(protocol|CRC)\b.*$'
# Results of both is
GigabitEthernet1/1/1 is up, line protocol is up
0 input errors, 0 CRC, 0 frame, 0 ignored
GigabitEthernet1/1/2 is up, line protocol is up
0 input errors, 0 CRC, 0 frame, 0 ignored
当然,您可以像说您已经在做的那样,只使用 ForLoop 单个文件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句