LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   grep -oE not portable (https://www.linuxquestions.org/questions/programming-9/grep-oe-not-portable-852784/)

frater 12-28-2010 06:21 AM

grep -oE not portable
 
I'm using Zabbix on which I can use give bash command to the agent.
This 1-liner will give me all the interfaces with their IPv4 addresses.
I have a 2nd expression which returns a checksum so I can detect a difference whenever someone deletes/adds/changes an ipv4 interface.

This is the output on my Ubuntu-server:
Code:

~# ifconfig | grep -B1 ' inet ' | grep -oE '(^[a-z0-9:]*|addr:[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)'
eth0
addr:192.168.10.100
lo
addr:127.0.0.1

This is the output on CentOS 5.5
Code:

# ifconfig | grep -B1 ' inet ' | grep -oE '(^[a-z0-9:]*|addr:[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)'
eth0
eth0:0
eth0:1
eth0:3
eth0:4
eth0:5
eth0:6
eth0:7
eth0:8
eth0:9
eth0:10
eth0:11
eth0:12
lo

I'm interested in 2 things.
I would really like a regular expression (as elegant as possible) which will give me the output similar to the first. It needs an output that can be checksummed so I can create a trigger on it.

I would also like to know which behaviour is the correct one.

This is my Ubuntu Lucid Lynx
Code:

# set | grep ^BASH
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:histappend:hostcomplete:interactive_comments:login_shell:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="1" [2]="5" [3]="1" [4]="release" [5]="i486-pc-linux-gnu")
BASH_VERSION='4.1.5(1)-release'

This is CentOS5.5
Code:

# set | grep ^BASH
BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="3" [1]="2" [2]="25" [3]="1" [4]="release" [5]="i686-redhat-linux-gnu")
BASH_VERSION='3.2.25(1)-release'


Sergei Steshenko 12-28-2010 11:21 AM

Maybe better switch to Perl ? It's pretty stable WRT features, i.e. new features are added, by very few things get broken - each Perl release undergoes a lot of regression testing.

ntubski 12-28-2010 02:25 PM

I doubt the difference is in the grep implentation, but rather in the output of ifconfig. Can you post the ifconfig from both machines?

oops, turns out it is grep: see post 8

frater 12-28-2010 05:54 PM

Code:

# ifconfig
eth0      Link encap:Ethernet  HWaddr EA:9A:C0:23:A7:F9
          inet addr:89.250.184.254  Bcast:89.250.184.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:112718716 errors:0 dropped:0 overruns:0 frame:0
          TX packets:77665575 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:3252769608 (3.0 GiB)  TX bytes:2203873401 (2.0 GiB)

eth0:0    Link encap:Ethernet  HWaddr EA:9A:C0:23:A7:F9
          inet addr:89.250.184.253  Bcast:89.250.184.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

eth0:1    Link encap:Ethernet  HWaddr EA:9A:C0:23:A7:F9
          inet addr:89.250.184.252  Bcast:89.250.184.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

eth0:3    Link encap:Ethernet  HWaddr EA:9A:C0:23:A7:F9
          inet addr:89.250.184.203  Bcast:89.250.184.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

eth0:4    Link encap:Ethernet  HWaddr EA:9A:C0:23:A7:F9
          inet addr:89.250.184.204  Bcast:89.250.184.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

eth0:5    Link encap:Ethernet  HWaddr EA:9A:C0:23:A7:F9
          inet addr:89.250.184.205  Bcast:89.250.184.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

eth0:6    Link encap:Ethernet  HWaddr EA:9A:C0:23:A7:F9
          inet addr:89.250.184.206  Bcast:89.250.184.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

eth0:7    Link encap:Ethernet  HWaddr EA:9A:C0:23:A7:F9
          inet addr:89.250.184.207  Bcast:89.250.184.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

eth0:8    Link encap:Ethernet  HWaddr EA:9A:C0:23:A7:F9
          inet addr:89.250.184.208  Bcast:89.250.184.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

eth0:9    Link encap:Ethernet  HWaddr EA:9A:C0:23:A7:F9
          inet addr:89.250.184.209  Bcast:89.250.184.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

eth0:10  Link encap:Ethernet  HWaddr EA:9A:C0:23:A7:F9
          inet addr:89.250.184.210  Bcast:89.250.184.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

eth0:11  Link encap:Ethernet  HWaddr EA:9A:C0:23:A7:F9
          inet addr:89.250.184.211  Bcast:89.250.184.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

eth0:12  Link encap:Ethernet  HWaddr EA:9A:C0:23:A7:F9
          inet addr:89.250.184.212  Bcast:89.250.184.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

eth0:13  Link encap:Ethernet  HWaddr EA:9A:C0:23:A7:F9
          inet addr:89.250.184.80  Bcast:89.250.184.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

eth0:14  Link encap:Ethernet  HWaddr EA:9A:C0:23:A7:F9
          inet addr:89.250.184.81  Bcast:89.250.184.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

eth0:15  Link encap:Ethernet  HWaddr EA:9A:C0:23:A7:F9
          inet addr:89.250.184.82  Bcast:89.250.184.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

eth0:16  Link encap:Ethernet  HWaddr EA:9A:C0:23:A7:F9
          inet addr:89.250.184.83  Bcast:89.250.184.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

eth0:17  Link encap:Ethernet  HWaddr EA:9A:C0:23:A7:F9
          inet addr:89.250.184.84  Bcast:89.250.184.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

eth0:18  Link encap:Ethernet  HWaddr EA:9A:C0:23:A7:F9
          inet addr:89.250.184.85  Bcast:89.250.184.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

eth0:19  Link encap:Ethernet  HWaddr EA:9A:C0:23:A7:F9
          inet addr:89.250.184.87  Bcast:89.250.184.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:22621367 errors:0 dropped:0 overruns:0 frame:0
          TX packets:22621367 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:3770694885 (3.5 GiB)  TX bytes:3770694885 (3.5 GiB)

Code:

# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0c:29:6d:20:20
          inet addr:192.168.10.100  Bcast:192.168.10.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:9028024 errors:0 dropped:0 overruns:0 frame:0
          TX packets:9536504 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:674515000 (674.5 MB)  TX bytes:700485704 (700.4 MB)
          Interrupt:18 Base address:0x2000

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:6489106 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6489106 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:396977928 (396.9 MB)  TX bytes:396977928 (396.9 MB)


grail 12-28-2010 06:28 PM

Is it maybe something to do with the versions of grep??

Tinkster 12-28-2010 07:51 PM

I'm wondering whether one ifconfig outputs a \t and the other " " before inet ...
I can't imagine grep being that badly broken in either of the OSes.

Tinkster 12-28-2010 07:51 PM

I'm wondering whether one ifconfig outputs a \t and the other " " before inet ...
I can't imagine grep being that badly broken in either of the OSes.

Kenhelm 12-28-2010 08:02 PM

There are problems with some versions of grep -o
http://www.linuxquestions.org/questi...system-758692/
http://lists.gnu.org/archive/html/bu.../msg00087.html
http://lists.gnu.org/archive/html/bu.../msg00012.html

Changing the * into + gets the code to work on GNU grep 2.5.1
Code:

grep -oE '(^[a-z0-9:]+|addr:[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)'

frater 12-28-2010 11:18 PM

Thanks all.
It seems I need to be careful with 'grep -o'
A year ago I was getting strange results with it on busybox for which I posted a ticket.
This ticket was immediately followed by a fix.

Also thanks for the workaround which works.

CentOS 5.5 is running 'grep (GNU grep) 2.5.1' where Lucid Lynx is running 'GNU grep 2.5.4'
Can I do something to make CentOS distribute a newer version or is it just something to live with?

After my initial tests, I expected it to work on all Linux versions


All times are GMT -5. The time now is 02:52 PM.