LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-28-2010, 06:21 AM   #1
frater
Member
 
Registered: Jul 2008
Posts: 121

Rep: Reputation: 23
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'

Last edited by frater; 12-28-2010 at 11:20 PM.
 
Old 12-28-2010, 11:21 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
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.
 
Old 12-28-2010, 02:25 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
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

Last edited by ntubski; 12-29-2010 at 10:27 AM. Reason: point to correct solution
 
Old 12-28-2010, 05:54 PM   #4
frater
Member
 
Registered: Jul 2008
Posts: 121

Original Poster
Rep: Reputation: 23
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)
 
Old 12-28-2010, 06:28 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Is it maybe something to do with the versions of grep??
 
Old 12-28-2010, 07:51 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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.
 
Old 12-28-2010, 07:51 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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.
 
Old 12-28-2010, 08:02 PM   #8
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
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]+)'
 
Old 12-28-2010, 11:18 PM   #9
frater
Member
 
Registered: Jul 2008
Posts: 121

Original Poster
Rep: Reputation: 23
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Grep -p for Linux, Trying to grep a paragraph. ohijames Linux - Newbie 5 07-22-2010 02:09 PM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 10:31 PM
need a portable linux distro to bring on portable usb hard drive... pippo17 Linux - Distributions 3 10-20-2008 06:41 AM
how to grep multiple filters with grep LinuxLover Linux - Enterprise 1 10-18-2007 07:12 AM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration