Skip to main content
added ip a output
Source Link
jamacoe
  • 488
  • 5
  • 14

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  

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.

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  
deleted greedy ? as it doesn't work in POSIX commands
Source Link
jamacoe
  • 488
  • 5
  • 14

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.

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.

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.

Source Link
jamacoe
  • 488
  • 5
  • 14

sed -E regex not working as expected using (.*?)

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.