Link to home
Start Free TrialLog in
Avatar of dhite99
dhite99

asked on

KORN Shell String Manipulation

The goal is to be able to strip out the first 3 octets of the ip addresses in /etc/hosts.

I can get the whole IP addresses with:

grep “\.” /etc/hosts | egrep –v “localhost|^#” | cut –d” “ –f1


...that yields something like:

10.209.2.45
10.209.6.24
:
:

but what I need to end up with is
10.209.2
10.209.6
:
:

Any help is most appreciated!
Avatar of user_n
user_n
Flag of United States of America image

ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dhite99
dhite99

ASKER

user_n - that is a great reference, and sometime when I have several hours I'm going to look at it in detail. Regrettably today is not a day to do that, but thanks for the reference.

woolmilkporc - I like the idea of using awk, but I cannot seem to make it work:

oracle@emghlc001:/u01/app/oracle> awk -F\. '/^[0-9]{1,3}\./&&!/localhost/ {print $1 FS $2 FS $3}' /etc/hosts
oracle@emghlc001:>

It comes back with nothing.

/etc/hosts has something like this in it:

# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1               localhost.localdomain localhost
::1             localhost6.localdomain6 localhost6
10.200.13.65  emrhlc001-MGMT
10.208.1.31  emrhlc001 emrhlc001.xxxx.com
10.208.1.34  emrhlc002 emrhlc002.xxxx.com
:
:

What am i doing wrong with the awk you provided?
Avatar of dhite99

ASKER

I do not know what I would do without you guru's - thanks again!!!
Here's some other methods

sed -n "/^[0-9]/ s/\.[0-9]* .*//p" /etc/hosts

grep "^[0-9]" /etc/hosts | cut -f1-3 -d.