gpt4 book ai didi

awk 作为 ifconfig 过滤器

转载 作者:行者123 更新时间:2023-12-02 02:18:45 25 4
gpt4 key购买 nike

我对整个 awk 工具都很陌生,它对我来说似乎真的很复杂。

ifconfig 输出示例:

enp2s0f1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
ether xx:xx:xx:xx:xx:xx txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 338 bytes 27536 (26.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 338 bytes 27536 (26.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

wlp3s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet xxx.xxx.xxx.xxx netmask xxx.xxx.xxx.x broadcast xxx.xxx.xxx.xxx
inet6 xxxx::xxxx:xxxx:xxxx:xxxx prefixlen 64 scopeid 0x20<link>
ether xx:xx:xx:xx:xx:xx txqueuelen 1000 (Ethernet)
RX packets 143296 bytes 172270775 (164.2 MiB)
RX errors 0 dropped 5 overruns 0 frame 0
TX packets 32786 bytes 6774762 (6.4 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

到目前为止,我得到了一个 ifconfig 过滤器命令,它会丢弃环回以及其他不需要的东西。

ifconfig | awk 'BEGIN{ORS=RS="\n\n"} !/^lo/{print}' | awk '{print $1, $2}' | awk '!/^RX/{print}' | awk '!/^TX/{print}'

这是一个输出:

enp2s0f1: flags=4099<UP,BROADCAST,MULTICAST>
ether xx:xx:xx:xx:xx:xx

wlp3s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>
inet xxx.xxx.xxx.xxx
inet6 xxxx::xxxx:xxxx:xxxx:xxxx
ether xx:xx:xx:xx:xx:xx

我研究了类似的问题,但我想知道如何使用awk从行的中间剪切

我已经尝试过这样的行:awk '!/flags=/{print}'但它删除了包含标志的整行

期望的输出:

enp2s0f1:
ether xx:xx:xx:xx:xx:xx

wlp3s0:
inet xxx.xxx.xxx.xxx
ether xx:xx:xx:xx:xx:xx

最佳答案

您可以尝试这个awk:

ifconfig |
awk '
/^[[:alnum:]]+: / { # if we encounter an interface name
n = $1 # save name in variable n
p = 1 # set flag p=1
}
# interface does not start with "lo" and protocols are inet or ether
n !~ /^lo/ && /^[[:blank:]]+(inet|ether) / {
if (p) { # if flag is set
print n # print interface name
p = 0 # reset flag p=0
}
print $1, $2 # print protocol and address
}'

关于awk 作为 ifconfig 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66768521/

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