gpt4 book ai didi

linux - grep 或 awk 切割 2 条不同的线

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:52:17 27 4
gpt4 key购买 nike

我在下面有这个名为 monitor.log 的报告文件

switch#sh mac address-table int g1/0/1
Mac Address Table
-------------------------------------------

Vlan Mac Address Type Ports
---- ----------- -------- -----
110 000f.ffff.ffff STATIC drop
Total Mac Addresses for this criterion: 1
switch#sh mac address-table int g1/0/4
Mac Address Table
-------------------------------------------

Vlan Mac Address Type Ports
---- ----------- -------- -----
110 000f.ffff.ffff STATIC drop
Total Mac Addresses for this criterion: 1
switch#sh mac address-table int g1/0/3
Mac Address Table
-------------------------------------------

Vlan Mac Address Type Ports
---- ----------- -------- -----
110 000f.ffff.ffff STATIC drop
Total Mac Addresses for this criterion: 1

我想要一个使用 grep 、 awk 或 sed 的命令来只显示那些字段并保存到文件中,即:

int g1/0/1 drop 
int g1/0/2 drop
int g1/0/3 drop

最佳答案

$ awk -v RS='switch#sh' '$3 {print $3,$4,$21}' monitor.log
int g1/0/1 drop
int g1/0/4 drop
int g1/0/3 drop

要将报告限制为drop 个条目:

awk -v RS='switch#sh' '$21=="drop" {print $3,$4,$21}' monitor.log

处理多行表格

假设输入文件如下所示:

$ cat monitor.log2
switch#sh mac address-table int g1/0/1
Mac Address Table
-------------------------------------------

Vlan Mac Address Type Ports
---- ----------- -------- -----
110 000f.ffff.ffff STATIC drop
200 000f.ffff.ffff STATIC drop
Total Mac Addresses for this criterion: 1
switch#sh mac address-table int g1/0/2
Mac Address Table

然后使用:

$ awk '/switch#sh/{a=$4" "$5} /----/,/Total Mac Addresses/ {if ($4=="drop")print a,$4}' monitor.log2
int g1/0/1 drop
int g1/0/1 drop

关于linux - grep 或 awk 切割 2 条不同的线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30086654/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com