1753500 Members
4647 Online
108794 Solutions
New Discussion юеВ

grep command

 
SOLVED
Go to solution
Tracey
Trusted Contributor

Re: grep command

The hostnames in my hosts file usually have a tab after the name. The following works for me:

grep "hp1[ ]" hosts

The first character in the brackets is a space, the second character is where I pressed the tab key (on my system it actually show many more spaces) That way it will get those that end in spaces or those that end in a tab character.
Laurent Paumier
Trusted Contributor

Re: grep command

How about rewriting the whole script ?
Here's what you could du using awk :

awk '
BEGIN {
while (getline <"/etc/hosts") {
for (i=1;i<=NF;i++) {
host[$i]=$0
}
}
while (getline <"/tmp/hplist") {
printf("%s\n",host[$0])
}
}' >/tmp/list
Tarek
Super Advisor

Re: grep command

That's it Robin!!! The second code was the right one.
I tried also yours Laurent, it works, but there are some entries that aren't found.
However i'd like to learn something about using awk, it seems very powerful, is there some tutorial on the net?
Thanks both, and of course thanks to all for your kindly help.
Regards
Tarek
Robin Wakefield
Honored Contributor

Re: grep command