I want to print all IP address and hostname from /etc/hosts using Shell script

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I want to print IP address and hostname from /etc/hosts using a shell script



/etc/hosts file :



## #For example:
## #102.54.94.97 rhino.acme.com # source server
## #38.25.63.10 x.acme.com # x client host
## #localhost name resolution is handled within DNS itself.
## #127.0.0.1 localhost
## #::1 localhost
111.11.1.1 aaaa.aa.aa
111.11.1.1 aaaa.aa.aa
111.11.1.1 aaaa.aa.aa
111.11.1.1 aaaa.aa.aa

# #122.55.80.97 bbb.bb.bb
# #122.55.80.98 ccc.cc.cc


I need output valid IP address and host name not all lines



111.11.1.1 aaaa.aa.aa
111.11.1.1 aaaa.aa.aa
111.11.1.1 aaaa.aa.aa
111.11.1.1 aaaa.aa.aa


not all the lines







share|improve this question





















  • Which IP address and which hostname? Can you be more specific on what you are trying to achieve? What have you tried? What results did you expect and what results did you get instead? What kind of errors?
    – Filipe Brandenburger
    May 4 at 5:40










  • Hi Filipe, i have list of ipaddress and their hostnames are maintains in /etc/host. Im trying to print (only) ipaddress and hostname from the hosts file.
    – Kannan M
    May 4 at 5:47







  • 2




    @KannanM The /etc/hosts file is usually in 127.0.0.1 localhost format, IP on left and hostname on right. To print that you just need cat /etc/hosts. This is what we understand, so if this is not what you want - please edit your question and include example of how you want it to look like
    – Sergiy Kolodyazhnyy
    May 4 at 5:50






  • 3




    If you want to filter for IPv4 addresses only and get rid of comments and IPv6 stuff, try grep -E "^([[:digit:]]+.)3[[:digit:]]+" /etc/hosts
    – Philippos
    May 4 at 6:01






  • 2




    Personally, I'd say it's sufficient to just do grep -v '#', since the format for the /etc/hosts file, at least according to the manual, is only IP_address canonical_hostname optional_aliasies. The only other thing that seems to be alright there is comments, which start with #. But that's just my view.
    – Sergiy Kolodyazhnyy
    May 4 at 6:19














up vote
0
down vote

favorite












I want to print IP address and hostname from /etc/hosts using a shell script



/etc/hosts file :



## #For example:
## #102.54.94.97 rhino.acme.com # source server
## #38.25.63.10 x.acme.com # x client host
## #localhost name resolution is handled within DNS itself.
## #127.0.0.1 localhost
## #::1 localhost
111.11.1.1 aaaa.aa.aa
111.11.1.1 aaaa.aa.aa
111.11.1.1 aaaa.aa.aa
111.11.1.1 aaaa.aa.aa

# #122.55.80.97 bbb.bb.bb
# #122.55.80.98 ccc.cc.cc


I need output valid IP address and host name not all lines



111.11.1.1 aaaa.aa.aa
111.11.1.1 aaaa.aa.aa
111.11.1.1 aaaa.aa.aa
111.11.1.1 aaaa.aa.aa


not all the lines







share|improve this question





















  • Which IP address and which hostname? Can you be more specific on what you are trying to achieve? What have you tried? What results did you expect and what results did you get instead? What kind of errors?
    – Filipe Brandenburger
    May 4 at 5:40










  • Hi Filipe, i have list of ipaddress and their hostnames are maintains in /etc/host. Im trying to print (only) ipaddress and hostname from the hosts file.
    – Kannan M
    May 4 at 5:47







  • 2




    @KannanM The /etc/hosts file is usually in 127.0.0.1 localhost format, IP on left and hostname on right. To print that you just need cat /etc/hosts. This is what we understand, so if this is not what you want - please edit your question and include example of how you want it to look like
    – Sergiy Kolodyazhnyy
    May 4 at 5:50






  • 3




    If you want to filter for IPv4 addresses only and get rid of comments and IPv6 stuff, try grep -E "^([[:digit:]]+.)3[[:digit:]]+" /etc/hosts
    – Philippos
    May 4 at 6:01






  • 2




    Personally, I'd say it's sufficient to just do grep -v '#', since the format for the /etc/hosts file, at least according to the manual, is only IP_address canonical_hostname optional_aliasies. The only other thing that seems to be alright there is comments, which start with #. But that's just my view.
    – Sergiy Kolodyazhnyy
    May 4 at 6:19












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to print IP address and hostname from /etc/hosts using a shell script



/etc/hosts file :



## #For example:
## #102.54.94.97 rhino.acme.com # source server
## #38.25.63.10 x.acme.com # x client host
## #localhost name resolution is handled within DNS itself.
## #127.0.0.1 localhost
## #::1 localhost
111.11.1.1 aaaa.aa.aa
111.11.1.1 aaaa.aa.aa
111.11.1.1 aaaa.aa.aa
111.11.1.1 aaaa.aa.aa

# #122.55.80.97 bbb.bb.bb
# #122.55.80.98 ccc.cc.cc


I need output valid IP address and host name not all lines



111.11.1.1 aaaa.aa.aa
111.11.1.1 aaaa.aa.aa
111.11.1.1 aaaa.aa.aa
111.11.1.1 aaaa.aa.aa


not all the lines







share|improve this question













I want to print IP address and hostname from /etc/hosts using a shell script



/etc/hosts file :



## #For example:
## #102.54.94.97 rhino.acme.com # source server
## #38.25.63.10 x.acme.com # x client host
## #localhost name resolution is handled within DNS itself.
## #127.0.0.1 localhost
## #::1 localhost
111.11.1.1 aaaa.aa.aa
111.11.1.1 aaaa.aa.aa
111.11.1.1 aaaa.aa.aa
111.11.1.1 aaaa.aa.aa

# #122.55.80.97 bbb.bb.bb
# #122.55.80.98 ccc.cc.cc


I need output valid IP address and host name not all lines



111.11.1.1 aaaa.aa.aa
111.11.1.1 aaaa.aa.aa
111.11.1.1 aaaa.aa.aa
111.11.1.1 aaaa.aa.aa


not all the lines









share|improve this question












share|improve this question




share|improve this question








edited May 4 at 6:58









Kusalananda

102k13199316




102k13199316









asked May 4 at 5:38









Kannan M

102




102











  • Which IP address and which hostname? Can you be more specific on what you are trying to achieve? What have you tried? What results did you expect and what results did you get instead? What kind of errors?
    – Filipe Brandenburger
    May 4 at 5:40










  • Hi Filipe, i have list of ipaddress and their hostnames are maintains in /etc/host. Im trying to print (only) ipaddress and hostname from the hosts file.
    – Kannan M
    May 4 at 5:47







  • 2




    @KannanM The /etc/hosts file is usually in 127.0.0.1 localhost format, IP on left and hostname on right. To print that you just need cat /etc/hosts. This is what we understand, so if this is not what you want - please edit your question and include example of how you want it to look like
    – Sergiy Kolodyazhnyy
    May 4 at 5:50






  • 3




    If you want to filter for IPv4 addresses only and get rid of comments and IPv6 stuff, try grep -E "^([[:digit:]]+.)3[[:digit:]]+" /etc/hosts
    – Philippos
    May 4 at 6:01






  • 2




    Personally, I'd say it's sufficient to just do grep -v '#', since the format for the /etc/hosts file, at least according to the manual, is only IP_address canonical_hostname optional_aliasies. The only other thing that seems to be alright there is comments, which start with #. But that's just my view.
    – Sergiy Kolodyazhnyy
    May 4 at 6:19
















  • Which IP address and which hostname? Can you be more specific on what you are trying to achieve? What have you tried? What results did you expect and what results did you get instead? What kind of errors?
    – Filipe Brandenburger
    May 4 at 5:40










  • Hi Filipe, i have list of ipaddress and their hostnames are maintains in /etc/host. Im trying to print (only) ipaddress and hostname from the hosts file.
    – Kannan M
    May 4 at 5:47







  • 2




    @KannanM The /etc/hosts file is usually in 127.0.0.1 localhost format, IP on left and hostname on right. To print that you just need cat /etc/hosts. This is what we understand, so if this is not what you want - please edit your question and include example of how you want it to look like
    – Sergiy Kolodyazhnyy
    May 4 at 5:50






  • 3




    If you want to filter for IPv4 addresses only and get rid of comments and IPv6 stuff, try grep -E "^([[:digit:]]+.)3[[:digit:]]+" /etc/hosts
    – Philippos
    May 4 at 6:01






  • 2




    Personally, I'd say it's sufficient to just do grep -v '#', since the format for the /etc/hosts file, at least according to the manual, is only IP_address canonical_hostname optional_aliasies. The only other thing that seems to be alright there is comments, which start with #. But that's just my view.
    – Sergiy Kolodyazhnyy
    May 4 at 6:19















Which IP address and which hostname? Can you be more specific on what you are trying to achieve? What have you tried? What results did you expect and what results did you get instead? What kind of errors?
– Filipe Brandenburger
May 4 at 5:40




Which IP address and which hostname? Can you be more specific on what you are trying to achieve? What have you tried? What results did you expect and what results did you get instead? What kind of errors?
– Filipe Brandenburger
May 4 at 5:40












Hi Filipe, i have list of ipaddress and their hostnames are maintains in /etc/host. Im trying to print (only) ipaddress and hostname from the hosts file.
– Kannan M
May 4 at 5:47





Hi Filipe, i have list of ipaddress and their hostnames are maintains in /etc/host. Im trying to print (only) ipaddress and hostname from the hosts file.
– Kannan M
May 4 at 5:47





2




2




@KannanM The /etc/hosts file is usually in 127.0.0.1 localhost format, IP on left and hostname on right. To print that you just need cat /etc/hosts. This is what we understand, so if this is not what you want - please edit your question and include example of how you want it to look like
– Sergiy Kolodyazhnyy
May 4 at 5:50




@KannanM The /etc/hosts file is usually in 127.0.0.1 localhost format, IP on left and hostname on right. To print that you just need cat /etc/hosts. This is what we understand, so if this is not what you want - please edit your question and include example of how you want it to look like
– Sergiy Kolodyazhnyy
May 4 at 5:50




3




3




If you want to filter for IPv4 addresses only and get rid of comments and IPv6 stuff, try grep -E "^([[:digit:]]+.)3[[:digit:]]+" /etc/hosts
– Philippos
May 4 at 6:01




If you want to filter for IPv4 addresses only and get rid of comments and IPv6 stuff, try grep -E "^([[:digit:]]+.)3[[:digit:]]+" /etc/hosts
– Philippos
May 4 at 6:01




2




2




Personally, I'd say it's sufficient to just do grep -v '#', since the format for the /etc/hosts file, at least according to the manual, is only IP_address canonical_hostname optional_aliasies. The only other thing that seems to be alright there is comments, which start with #. But that's just my view.
– Sergiy Kolodyazhnyy
May 4 at 6:19




Personally, I'd say it's sufficient to just do grep -v '#', since the format for the /etc/hosts file, at least according to the manual, is only IP_address canonical_hostname optional_aliasies. The only other thing that seems to be alright there is comments, which start with #. But that's just my view.
– Sergiy Kolodyazhnyy
May 4 at 6:19










2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










Try this



sed -e '/^#/d' -e '/^s*$/d' /etc/hosts



will delete all lines starts with # and blank lines.






share|improve this answer





















  • It will, however, not deal with comments that does not start at the start of the line, which is a supported type of comment in the /etc/hosts file.
    – Kusalananda
    May 9 at 12:22


















up vote
2
down vote













Removing commented out and empty lines form /etc/hosts:



sed -e 's/#.*//' -e 's/[[:blank:]]*$//' -e '/^$/d' /etc/hosts


This uses three sed expressions in the following order:



  1. Remove everything after a # (including the #).

  2. Remove blanks (spaces or tabs) at the end of lines.

  3. Delete empty lines.

Given your example file, this produces



111.11.1.1 aaaa.aa.aa
111.11.1.1 aaaa.aa.aa
111.11.1.1 aaaa.aa.aa
111.11.1.1 aaaa.aa.aa


This obviously does not try to validate the addresses and names that are left.






share|improve this answer





















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f441705%2fi-want-to-print-all-ip-address-and-hostname-from-etc-hosts-using-shell-script%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    Try this



    sed -e '/^#/d' -e '/^s*$/d' /etc/hosts



    will delete all lines starts with # and blank lines.






    share|improve this answer





















    • It will, however, not deal with comments that does not start at the start of the line, which is a supported type of comment in the /etc/hosts file.
      – Kusalananda
      May 9 at 12:22















    up vote
    0
    down vote



    accepted










    Try this



    sed -e '/^#/d' -e '/^s*$/d' /etc/hosts



    will delete all lines starts with # and blank lines.






    share|improve this answer





















    • It will, however, not deal with comments that does not start at the start of the line, which is a supported type of comment in the /etc/hosts file.
      – Kusalananda
      May 9 at 12:22













    up vote
    0
    down vote



    accepted







    up vote
    0
    down vote



    accepted






    Try this



    sed -e '/^#/d' -e '/^s*$/d' /etc/hosts



    will delete all lines starts with # and blank lines.






    share|improve this answer













    Try this



    sed -e '/^#/d' -e '/^s*$/d' /etc/hosts



    will delete all lines starts with # and blank lines.







    share|improve this answer













    share|improve this answer



    share|improve this answer











    answered May 4 at 8:47









    SivaPrasath

    4,83212545




    4,83212545











    • It will, however, not deal with comments that does not start at the start of the line, which is a supported type of comment in the /etc/hosts file.
      – Kusalananda
      May 9 at 12:22

















    • It will, however, not deal with comments that does not start at the start of the line, which is a supported type of comment in the /etc/hosts file.
      – Kusalananda
      May 9 at 12:22
















    It will, however, not deal with comments that does not start at the start of the line, which is a supported type of comment in the /etc/hosts file.
    – Kusalananda
    May 9 at 12:22





    It will, however, not deal with comments that does not start at the start of the line, which is a supported type of comment in the /etc/hosts file.
    – Kusalananda
    May 9 at 12:22













    up vote
    2
    down vote













    Removing commented out and empty lines form /etc/hosts:



    sed -e 's/#.*//' -e 's/[[:blank:]]*$//' -e '/^$/d' /etc/hosts


    This uses three sed expressions in the following order:



    1. Remove everything after a # (including the #).

    2. Remove blanks (spaces or tabs) at the end of lines.

    3. Delete empty lines.

    Given your example file, this produces



    111.11.1.1 aaaa.aa.aa
    111.11.1.1 aaaa.aa.aa
    111.11.1.1 aaaa.aa.aa
    111.11.1.1 aaaa.aa.aa


    This obviously does not try to validate the addresses and names that are left.






    share|improve this answer

























      up vote
      2
      down vote













      Removing commented out and empty lines form /etc/hosts:



      sed -e 's/#.*//' -e 's/[[:blank:]]*$//' -e '/^$/d' /etc/hosts


      This uses three sed expressions in the following order:



      1. Remove everything after a # (including the #).

      2. Remove blanks (spaces or tabs) at the end of lines.

      3. Delete empty lines.

      Given your example file, this produces



      111.11.1.1 aaaa.aa.aa
      111.11.1.1 aaaa.aa.aa
      111.11.1.1 aaaa.aa.aa
      111.11.1.1 aaaa.aa.aa


      This obviously does not try to validate the addresses and names that are left.






      share|improve this answer























        up vote
        2
        down vote










        up vote
        2
        down vote









        Removing commented out and empty lines form /etc/hosts:



        sed -e 's/#.*//' -e 's/[[:blank:]]*$//' -e '/^$/d' /etc/hosts


        This uses three sed expressions in the following order:



        1. Remove everything after a # (including the #).

        2. Remove blanks (spaces or tabs) at the end of lines.

        3. Delete empty lines.

        Given your example file, this produces



        111.11.1.1 aaaa.aa.aa
        111.11.1.1 aaaa.aa.aa
        111.11.1.1 aaaa.aa.aa
        111.11.1.1 aaaa.aa.aa


        This obviously does not try to validate the addresses and names that are left.






        share|improve this answer













        Removing commented out and empty lines form /etc/hosts:



        sed -e 's/#.*//' -e 's/[[:blank:]]*$//' -e '/^$/d' /etc/hosts


        This uses three sed expressions in the following order:



        1. Remove everything after a # (including the #).

        2. Remove blanks (spaces or tabs) at the end of lines.

        3. Delete empty lines.

        Given your example file, this produces



        111.11.1.1 aaaa.aa.aa
        111.11.1.1 aaaa.aa.aa
        111.11.1.1 aaaa.aa.aa
        111.11.1.1 aaaa.aa.aa


        This obviously does not try to validate the addresses and names that are left.







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered May 4 at 6:56









        Kusalananda

        102k13199316




        102k13199316






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f441705%2fi-want-to-print-all-ip-address-and-hostname-from-etc-hosts-using-shell-script%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)