You could have also used gawk (or awk) like this:
gawk -F. '{print $1$2$3$4}'
The -F. sets the field separator to a period. When you list the output fields one after another they are concatenated. Many, many ways to skin the cat in Linux.
Another way to do what you want is to use extended regular expressions (regexp) with grep as follows:
/sbin/ifconfig eth1 | grep -o -E [[:digit:]]{3}\.[[:digit:]]{3}\.[[:digit:]]{3}\.[[:digit:]]{3}
This will effectively strip out all xxx.xxx.xxx.xxx number sequences from your output. The [:digit:] regexp is any digit (go figure). The bracketed number, {3}, following the [:digit:] regular expressions says to find exactly 3 occurrences of this regexp. The \. is an escaped period, since a period has a special meaning. Therefore, you need to escape it to tell grep I really want a period, not it's special meaning. And of course, this is repeated 3 more times to form a 4 number sequence of 3 digits separated by periods, i.e. xxx.xxx.xxx.xxx. IP's may only have one digit for one of the numbers, e.g. 192.168.1.101. To filter out this IP, change the {3} to {1} for the 3rd regexp:
/sbin/ifconfig eth1 | grep -o -E [[:digit:]]{3}\.[[:digit:]]{3}\.[[:digit:]]{1}\.[[:digit:]]{3}
To mix things up a little more, you can remove all the {3} and replace it with a +. The + means find atleast one occurence or more of the regexp preceeding it. So,
/sbin/ifconfig eth1 | grep -o -E [[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+
would find things like xx.xx.xxx.x or xxx.xxx.x.x and so on. You should get the point by now. Regexp are pretty powerful. Hope this helps someone who may want to do similar things. All the best.
Last edited by card-suse; 04-16-2006 at 12:06 PM.
|