gpt4 book ai didi

regex - 从 grep 输出中按索引获取行

转载 作者:行者123 更新时间:2023-12-01 23:25:19 25 4
gpt4 key购买 nike

我正在尝试获取机器的 ipv4 地址。我已经开始工作了,但是有没有一种方法可以指定您想要返回数据中的索引 2?

ip a | grep -E -o "([0-9]{1,3}[\.]){3}[0-9]{1,3}"

输出

127.0.0.1
192.168.13.131
192.168.13.255

最佳答案

使用 awk,请尝试按照您的尝试编写。

ip a | 
awk '
match($0,/([0-9]{1,3}\.){3}[0-9]{1,3}/) && ++count==2{
print substr($0,RSTART,RLENGTH)
}'

说明:为上述解决方案添加详细说明。

ip a |
##Running ip a command and sending its output to awk as input
awk '
##Starting awk program from here.
match($0,/([0-9]{1,3}\.){3}[0-9]{1,3}/) && ++count==2{
##Using match function to match IP address and checking if its 2nd time coming.
print substr($0,RSTART,RLENGTH)
##Printing matching sub string here.
}'

关于regex - 从 grep 输出中按索引获取行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67334675/

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