PDA

View Full Version : Script to extract system names from Host file


gotTheShakes
04-11-2007, 11:47 AM
I was wondering if you all could help me generate a script that would extract the system names from a central host file so that I may pass them to another script.

I am thinking that I would use sed to get me most of the way there, I am just having a hard time getting the script to read the host file line by line.

Any suggestions?

Thanks

hayne
04-11-2007, 02:32 PM
You'll need to give more details about:
- what the format of the "central host file" is
- what you have tried so far and difficulties you are having

I don't understand what you mean by "having a hard time getting the script to read the host file line by line" since 'sed' always works on a line by line basis.

Hal Itosis
04-11-2007, 02:55 PM
I don't understand what "system names" means (?)

gotTheShakes
04-11-2007, 03:25 PM
For example

here is part of the host file

192.168.1.119 ws19
192.168.1.118 ws18
192.168.1.117 ws17
192.168.1.116 ws16
192.168.1.115 ws15
192.168.1.114 ws14
192.168.1.113 ws13
192.168.1.112 ws12
192.168.1.110 ws10
192.168.1.109 ws09
192.168.1.108 ws08
192.168.1.107 ws07
192.168.1.106 ws06
192.168.1.105 ws05
192.168.1.104 ws04
192.168.1.103 ws03
192.168.1.102 ws02
192.168.1.101 ws01


I would like to 'sed' the ws values but removed the ip addresses.

I can do something like sed '/ws/!d' /etc/hosts but obviously I get this output.

I would like to have a script where I would have I guess o for loop, or maybe a case where it steps though the host file and for every time it finds a ws value it will execute the script.

I am just not sure of the sed syntax to read the file and then pass the variable one value at a time.

cb2005
04-12-2007, 09:22 AM
Try "awk"

If you want the ws names and not the ip addresses do

cat /etc/hosts | awk '{print $2}'

or

cat /etc/hosts | grep -v "#" | awk '{print $2}'

if you have comments in the hosts file.

-cb

Hal Itosis
04-12-2007, 01:36 PM
I would like to 'sed' the ws values but removed the ip addresses.Here are the awk and sed versions, as best as I could make them:

awk '{ if ($1 !~ /#/) print $2 }' /etc/hosts
sed -n '/^[#:]/! s/^.*[[:space:]]//p' /etc/hosts

They send the output to the screen.

I would like to have a script where I would have I guess o for loop, or maybe a case where it steps though the host file and for every time it finds a ws value it will execute the script.

I am just not sure of the sed syntax to read the file and then pass the variable one value at a time.
Not following exactly, but a redirector will write to a file... and a
pipe will pass it right to your script!!! (does your script read stdin?)


awk '{ if ($1 !~ /#/) print $2 }' /etc/hosts > /path/to/yourFile.txt
sed -n '/^[#:]/! s/^.*[[:space:]]//p' /etc/hosts | /path/to/yourScript.sh

Anyway, go with awk I think... that sed I wrote still needs some work.


--


BTW, I have two entries for localhost in my hosts:

$ grep localhost /etc/hosts
# localhost is used to configure the loopback interface
127.0.0.1 localhost
::1 localhost

Does that last one with "::1" belong there? How so?

kevvyg
03-05-2009, 10:27 PM
Attention Hal!

awk '{ if ($1 !~ /#/) print $2 }' /etc/hosts

This is my favorite command ever. Thank you. Thank you very, very much.

put17
03-09-2009, 05:50 PM
BTW, I have two entries for localhost in my hosts:

$ grep localhost /etc/hosts
# localhost is used to configure the loopback interface
127.0.0.1 localhost
::1 localhost

Does that last one with "::1" belong there? How so?

::1 is shorthand for localhost in IPv6:

$ ifconfig lo0 | grep 'prefixlen 128'
inet6 ::1 prefixlen 128

ghostdog74
03-11-2009, 09:40 AM
Attention Hal!



This is my favorite command ever. Thank you. Thank you very, very much.
in awk,

awk '/pattern/ { action }' inputfile


the "pattern" part acts as an implicit "if" therefore, there is no need to explicitly specify "if" in the "action" part

awk '$1 !~ /#/ { print $2}' /etc/hosts