?
Solved

KORN Shell String Manipulation

Posted on 2012-09-06
5
Medium Priority
?
930 Views
Last Modified: 2012-09-07
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!
0
Comment
Question by:dhite99
5 Comments
 
LVL 9

Expert Comment

by:user_n
ID: 38373491
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 total points
ID: 38373525
Why not awk?

awk -F\. '/^[0-9]{1,3}\./&&!/localhost/ {print $1 FS $2 FS $3}' /etc/hosts

But your version needs just a slight modification:

grep "\." /etc/hosts | egrep -v "localhost|^#" | cut -d"." -f1-3
0
 

Author Comment

by:dhite99
ID: 38373560
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?
0
 

Author Closing Comment

by:dhite99
ID: 38373629
I do not know what I would do without you guru's - thanks again!!!
0
 
LVL 48

Expert Comment

by:Tintin
ID: 38378411
Here's some other methods

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

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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Often times it's very very easy to extend a volume on a Linux instance in AWS, but impossible to shrink it. I wanted to contribute to the experts-exchange community a way of providing a procedure that works on an AWS instance. It can also be used on…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question