我的文件由扫描结果组成。每个结果可以有4-20行
我只想筛选成功的mac地址(通过扫描)
我的文件:
FAIL user1 OS-Anti-Virus-Check Mac OS X 10.10.5
PASSED Operating-System :: OS X 10.10 Yosemite
PASSED Operating-System :: OS X 10.10 Yosemite Update
FAILED Anti-Virus :: Sophos
E0:AC:CB:82:C3:F2 - en0
FAIL user2 OS-Anti-Virus-Check Windows Vista (TM) Home Premium 6.0 Service Pack 2
PASSED Operating-System :: Windows Vista
PASSED Operating-System :: Vista Service Pack
PASSED Operating-System :: Windows Vista Edition
PASSED Operating-System :: Vista Critical and Security Updates
PASSED Operating-System :: Windows Vista AutoUpdates Label
FAILED Anti-Spyware :: Microsoft Windows Defender
FAILED Anti-Virus :: Microsoft Windows Defender
00:23:4D:E2:8E:03 - Atheros AR928x Wireless Network Adapter
00:1D:BA:AF:D4:35 - Marvell Yukon 88E8055 PCI-E Gigabit Ethernet Controller
PASS user3 OS-Anti-Virus-Check Windows 8 China 6.2
PASSED Anti-Spyware :: Avast! Premier
PASSED Anti-Virus :: Avast! Premier
PASSED Anti-Virus :: Avast! Premier Definitions
PASSED Operating-System :: Windows 8 x64
PASSED Operating-System :: Windows 8 x64 Service Pack
PASSED Operating-System :: Windows 8 x64 Edition
PASSED Operating-System :: Windows 8 x64 Critical and Security Updates
PASSED Operating-System :: Windows 8 x64 AutoUpdates Label
28:D2:44:D2:7A:2E - Intel(R) Ethernet Connection I218-V
7C:7A:91:73:88:09 - Intel(R) Wireless-N 7260
7C:7A:91:73:88:0A - Microsoft Wi-Fi Direct ����������
7C:7A:91:73:88:0D - Bluetooth �?(����������
PASS user4 OS-Anti-Virus-Check Mac OS X 10.10.5
PASSED Anti-Virus :: Sophos
PASSED Anti-Virus :: Sophos Definitions
PASSED Operating-System :: OS X 10.10 Yosemite
PASSED Operating-System :: OS X 10.10 Yosemite Update
E0:AC:CB:82:C3:F2 - en0
我想提取通过扫描的mac地址列表。
所以举个例子
如果行包含“passed”,下一行或2包含mac地址…打印MAC地址。
如果有人能指点我正确的方向,我将不胜感激…
最佳答案
您可以使用grep两次:
第一次识别“通过”行,然后是mac地址
第二次从结果中提取mac地址
示例:
grep -Pzo 'PASSED.*?\s+([0-9A-F]{2}(\:[0-9A-F]{2}){5})' d.txt | grep -Po '[0-9A-F]{2}(\:[0-9A-F]{2}){5}'
您可以使用以下命令检查接下来的两行(我仍然找不到方法使其对这两种情况都有效):
grep -Pzo 'PASSED.*?(\s+([0-9A-F]{2}(\:[0-9A-F]{2}){5}).*?){2}' 3.txt | grep -Po '[0-9A-F]{2}(\:[0-9A-F]{2}){5}'
关于regex - grep mac地址-连续2行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34922105/