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
/etc/hosts
file is usually in127.0.0.1 localhost
format, 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 likegrep -E "^([[:digit:]]+\.){3}[[:digit:]]+" /etc/hosts
grep -v '#'
, since the format for the/etc/hosts
file, 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.