0

I am using 'ip a' and a 'sed' pipe to show the ip addresses of all interfaces:

ip a |sed -En "{N ; N ; s/^[1-9]: (.*: ).+\n?.+\n? {4}inet \
(([0-9]{1,3}[./]){4}[0-9]{1,2}).*/\1\2/p}"

Why can't I substitute the group

(([0-9]{1,3}[./]){4}[0-9]{1,2})

with (.*\ ) and write instead

ip a |sed -En "{N ; N ; s/^[1-9]: (.*?: ).+\n?.+\n? {4}inet \
(.*\ ).*/\1\2/p}"

This matches but finds the blank after the ip address only at the end of the line, thus printing the whole line as \2. In 'normal' regex this works.

The output of ip a is:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000  
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00  
    inet 127.0.0.1/8 scope host lo  
       valid_lft forever preferred_lft forever  
    inet6 ::1/128 scope host   
       valid_lft forever preferred_lft forever  
2: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000  
    link/ether 10:78:d2:8e:e7:cf brd ff:ff:ff:ff:ff:ff  
    inet 192.168.1.44/24 brd 192.168.1.255 scope global dynamic eno1  
       valid_lft 41778sec preferred_lft 41778sec  
    inet6 fe80::1278:d2ff:fe8e:e7cf/64 scope link   
       valid_lft forever preferred_lft forever  
3: wlx68a3c45b2875: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000  
    link/ether 68:a3:c4:5b:28:75 brd ff:ff:ff:ff:ff:ff  
    inet 192.168.1.36/24 brd 192.168.1.255 scope global dynamic noprefixroute wlx68a3c45b2875  
       valid_lft 40457sec preferred_lft 40457sec  
    inet6 fe80::6aa3:c4ff:fe5b:2875/64 scope link   
       valid_lft forever preferred_lft forever  
  • 1
    .*? is a non-greedy expression. Such expressions are not supported by POSIX tools in general as they are Perl-compatible expressions (PCRE). See unix.stackexchange.com/questions/119905 – Kusalananda May 7 at 18:36
  • @Kusalananda definitively a worthwhile read, thank you. If I omit the ? in the first group which denotes the adapter name, it still matches until :<blank> and \1 prints correctly. But substituting group 2 for the ip address with (.*\ ) won't work either. Why not and how can I search until the next blank? – jamacoe May 7 at 19:10
  • 1
    Sine I don't use Linux and therefore do not have access to the ip tool nor know what the output looks like, I can't say for sure. To match a string of non-spaces, one usually do [^ ]*. – Kusalananda May 7 at 19:13
  • that works, thanks again – jamacoe May 7 at 19:22
  • And what do you want to get ? – ctac_ May 7 at 19:46
0

To match a string until a space, use [^ ]* The complete command would be:

ip a |sed -En "{N ; N ; s/^[1-9]: (.*: ).+\n?.+\n? {4}inet \  
([^ ]*).*/\1\2/p}"

Output:

lo: 127.0.0.1/8    
eno1: 192.168.1.44/24    
wlx68a3c45b2875: 192.168.1.36/24    
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.