我想像这样操作以下输出:
输出:
S* 0.0.0.0/0 [1/0] via 1.1.1.1, MAR-LNK-VGL_DSL
[1/0] via 2.2.2.2, MAR-LNK-GVT_DSL
[1/0] via 3.3.3.3, ENT-LNK-OI_CORP
[1/0] via 4.4.4.4, ENT-EBT_CORP, [10/0]
S 10.0.0.0/16 [10/0] via 5.5.5.5, ENT-LNK
C 10.5.10.0/24 is directly connected, MAR-DEV-LAN
C 10.5.20.0/24 is directly connected, MAR-DEV-DMZ
C 10.5.30.0/24 is directly connected, MAR-DEV-BALANCE
C 10.6.40.0/28 is directly connected, BNC-HOM
C 10.6.40.3/32 is directly connected, BNC-HOM
B 10.7.0.0/21 [20/100] via 6.6.6.6, vpn-21fce533-0, 03:12:51
S 10.19.210.56/29 [10/0] via 7.7.7.7, MAR-EMB_MPLS
S 10.19.233.0/24 [10/0] is directly connected, VPN-HML-PP
S 10.19.252.0/24 [5/0] is directly connected, VPN-CON
B 10.19.254.0/24 [20/0] via 8.8.8.8, ENT_EBT_MPLS, 4d20h13m
S 10.20.0.4/32 [10/0] is directly connected, VPN-AX4B
S 10.33.13.70/32 [10/0] is directly connected, VPN-NEW
B 10.50.0.0/17 [20/0] via 9.9.9.9, SEC-EQNX, 00:02:11
B 10.50.128.0/17 [20/0] via 9.9.9.9, SEC-EQNX, 00:02:11
O 100.0.153.164/32 [110/200] via 99.1.152.61, HUB-MAR-03_79, 17:36:22
[110/200] via 99.1.101.217, HUB-MAR-04_54, 17:36:22
[110/200] via 99.1.101.153, HUB-MAR-04_38, 17:36:22
[110/200] via 99.1.151.13, HUB-MAR-03_3, 17:36:22
O 100.0.252.11/32 [110/200] via 99.1.151.109, HUB-MAR-03_15, 03:22:24
[110/200] via 99.1.102.17, HUB-MAR-04_68, 03:22:24
如果我过滤 100.0.153.164/32,它将打印以下输出:
O 100.0.153.164/32 [110/200] via 99.1.152.61, HUB-MAR-03_79, 17:36:22
[110/200] via 99.1.101.217, HUB-MAR-04_54, 17:36:22
[110/200] via 99.1.101.153, HUB-MAR-04_38, 17:36:22
[110/200] via 99.1.151.13, HUB-MAR-03_3, 17:36:22
如果我过滤 0.0.0.0/0,输出将是:
S* 0.0.0.0/0 [1/0] via 1.1.1.1, MAR-LNK-VGL_DSL
[1/0] via 2.2.2.2, MAR-LNK-GVT_DSL
[1/0] via 3.3.3.3, ENT-LNK-OI_CORP
[1/0] via 4.4.4.4, ENT-EBT_CORP, [10/0]
如果我过滤类似 10.20.0.4/32 的内容,输出将是:
S 10.20.0.4/32 [10/0] is directly connected, VPN-AX4B
但我不太擅长这个,所以我搞不懂:(
答案1
如果您想在第二个字段中匹配给定的字符串,并在该字符串匹配后打印出“连续行”,请尝试这个 awk 脚本。
awk -v fltr="_str_" 'BEGIN {havematch=0}
{if ($1 ~ /^----/) {
if (havematch == 1) {
print $0
}
next
}
if ($2 == fltr) {
print $0
havematch=1
next
}
{havematch=0}
}' _file_
我将其分解开来以便于阅读。
基本上,您首先将 havematch 变量初始化为零,因为一开始没有任何匹配项。然后,检查某一行是否以“----”开头。如果是,并且 havematch 变量已设置,则打印出此行,因为它是“续行”。如果行以“----”开头,并且 havematch 设置为零,则这是其他内容的续行。不要打印它。无论哪种方式,如果行以“----”开头,则在 havematch 检查后,移至下一行。
如果行中的第二个字段与过滤字符串匹配,则打印出该行并将 havematch 变量设置为 1。转到下一行。
如果该行不是续行,并且第二个字段与您的过滤字符串不匹配,则该行不匹配,并且您不想打印出任何续行。因此,将 havematch 变量设置为零。
希望这可以帮助。