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

Clash 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
linux shell-script shell
 |Â
show 2 more comments
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
linux shell-script shell
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/hostsfile is usually in127.0.0.1 localhostformat, IP on left and hostname on right. To print that you just needcat /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, trygrep -E "^([[:digit:]]+.)3[[:digit:]]+" /etc/hosts
â Philippos
May 4 at 6:01
2
Personally, I'd say it's sufficient to just dogrep -v '#', since the format for the/etc/hostsfile, at least according to the manual, is onlyIP_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
 |Â
show 2 more comments
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
linux shell-script shell
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
linux shell-script shell
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/hostsfile is usually in127.0.0.1 localhostformat, IP on left and hostname on right. To print that you just needcat /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, trygrep -E "^([[:digit:]]+.)3[[:digit:]]+" /etc/hosts
â Philippos
May 4 at 6:01
2
Personally, I'd say it's sufficient to just dogrep -v '#', since the format for the/etc/hostsfile, at least according to the manual, is onlyIP_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
 |Â
show 2 more comments
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/hostsfile is usually in127.0.0.1 localhostformat, IP on left and hostname on right. To print that you just needcat /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, trygrep -E "^([[:digit:]]+.)3[[:digit:]]+" /etc/hosts
â Philippos
May 4 at 6:01
2
Personally, I'd say it's sufficient to just dogrep -v '#', since the format for the/etc/hostsfile, at least according to the manual, is onlyIP_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
 |Â
show 2 more comments
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.
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/hostsfile.
â Kusalananda
May 9 at 12:22
add a comment |Â
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:
- Remove everything after a
#(including the#). - Remove blanks (spaces or tabs) at the end of lines.
- 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.
add a comment |Â
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.
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/hostsfile.
â Kusalananda
May 9 at 12:22
add a comment |Â
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.
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/hostsfile.
â Kusalananda
May 9 at 12:22
add a comment |Â
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.
Try this
sed -e '/^#/d' -e '/^s*$/d' /etc/hosts
will delete all lines starts with # and blank lines.
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/hostsfile.
â Kusalananda
May 9 at 12:22
add a comment |Â
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/hostsfile.
â 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
add a comment |Â
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:
- Remove everything after a
#(including the#). - Remove blanks (spaces or tabs) at the end of lines.
- 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.
add a comment |Â
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:
- Remove everything after a
#(including the#). - Remove blanks (spaces or tabs) at the end of lines.
- 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.
add a comment |Â
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:
- Remove everything after a
#(including the#). - Remove blanks (spaces or tabs) at the end of lines.
- 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.
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:
- Remove everything after a
#(including the#). - Remove blanks (spaces or tabs) at the end of lines.
- 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.
answered May 4 at 6:56
Kusalananda
102k13199316
102k13199316
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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/hostsfile is usually in127.0.0.1 localhostformat, IP on left and hostname on right. To print that you just needcat /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/hostsfile, at least according to the manual, is onlyIP_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