KORN Shell String Manipulation

Posted on 2012-09-06
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:

but what I need to end up with is

Any help is most appreciated!
Question by:dhite99
    LVL 9

    Expert Comment

    LVL 68

    Accepted Solution

    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

    Author Comment

    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

    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.               localhost.localdomain localhost
    ::1             localhost6.localdomain6 localhost6  emrhlc001-MGMT  emrhlc001  emrhlc002

    What am i doing wrong with the awk you provided?

    Author Closing Comment

    I do not know what I would do without you guru's - thanks again!!!
    LVL 48

    Expert Comment

    Here's some other methods

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

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
    Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
    The viewer will learn how to dynamically set the form action using jQuery.
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now