V
V
Vincent12021-04-20 19:34:00
linux
Vincent1, 2021-04-20 19:34:00

Is there a lazy mode in AWK regular expressions?

The task in general is to get the name of the interface by specifying ip.
There is this conclusion:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 52:54:00:51:e9:32 brd ff:ff:ff:ff:ff:ff
3: [email protected]: <NOARP> mtu 1480 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/sit 0.0.0.0 brd 0.0.0.0
18: [email protected]: <POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1480 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/sit 45.139.184.150 peer 209.51.161.14
19: [email protected]: <POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1480 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/sit 185.230.140.161 peer 184.105.250.46
20: [email protected]: <POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1480 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/sit 185.230.140.162 peer 209.51.161.14
21: [email protected]: <POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1480 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/sit 185.230.140.160 peer 216.66.84.46

I use the command: ip link show | awk '/v6/,/185.230.140.160/{print $0}' to get the last 2 lines, but it's different.
18: [email protected]: <POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1480 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/sit 45.139.184.150 peer 209.51.161.14
19: [email protected]: <POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1480 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/sit 185.230.140.161 peer 184.105.250.46
20: [email protected]: <POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1480 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/sit 185.230.140.162 peer 209.51.161.14
21: [email protected]: <POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1480 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/sit 185.230.140.160 peer 216.66.84.46

How to get the last 2 lines?
It should be like this:
21: [email protected]: <POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1480 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/sit 185.230.140.160 peer 216.66.84.46

Answer the question

In order to leave comments, you need to log in

4 answer(s)
X
xotkot, 2021-04-20
@Vincent1

$ ip -j l | jq -r '.[] | select(.address=="185.230.140.160") | .ifname'

you can even display all matches in a column - ip-address and interface
$ ip -j l | jq -r '.[] | "\(.address) \(.ifname)"'

V
vreitech, 2021-04-20
@fzfx

may help.

$ ip -o a | grep 192.168.111.143 | awk '{print $2}'
enp2s0

V
Vitsliputsli, 2021-04-20
@Vitsliputsli

If necessary, be sure to use ip link show and awk:

ip link show | awk '/185.230.140.160/{print p $0; p=""; next} {p=$0 ORS}'

via ip link show and grep:
ip link show | grep -B1 185.230.140.160

S
Saboteur, 2021-04-21
@saboteur_kiev

ip with -o option
ip -o link show| awk '/185.230.140/{print $2}'
or so
ip -o link show| awk -F: '/185.230.140/{print $2}'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question