grep in for loop shell script

Sun / Sunfire v240
April 27, 2010 at 14:13:51
Specs: Solaris 9, 4GB
I am trying to use a for loop in shell script to look for lines that contain 'hostname <hosname>' inside thousands of files in a particular directory. <hostname> reside in another file called namelist2 (which has hundreds of names, each in a separate line).

I have this so far, but it is failing to expand the hostname, hence is NOT returning anything.

Thanks

==

for names in `cat /tmp/namelist2`
do

grep -i '^hostname $names' *

done

The script should return something like this:

1712665e003.cfg:hostname V76RR001
1712665e034.cfg:hostname V45RR007
etc.
etc


See More: grep in for loop shell script

Report •


#1
April 27, 2010 at 14:26:54
By default, GREP expects you give it a file, like

grep -i hostname $filename

If you scan a variable, you need something like

echo $var1 | grep -i hostname

I dont understand what you are doing with the single quotes in :
grep -i '^hostname $names' *


Report •

#2
April 27, 2010 at 14:41:41
. . . Especially since single quotes prevent variable expansion.

Report •

#3
April 28, 2010 at 05:24:10

I tried both single and double quotes hoping that the variable will expand. Did not know that behavior of grep.

Inside each .cfg file there are tens or hundreds of lines. I am interested in getting the line that start with hostname and followed by the device name. So, the line would like like this:

be-05-1591871e005.cfg:hostname "BE1RR0967"

Now, I need to look for the hostnames that are in the other file located in /tmp/namelist

I tried your suggestion, but echoing the variable on my for do loop does not append to: grep -i '^hostname'

Again, I am trying to find out in each *.cfg file the line that start with:
hostname <for each hostname in file /tmp/namelist>

there is only one of these line in each *.cfg file. Like I said, I have thousand of files to look for, hence I tried the for do loop.

Thank you for your comments and help.


Report •

Related Solutions

#4
April 28, 2010 at 09:00:56
> I tried both single and double quotes hoping that
> the variable will expand. Did not know that behavior
> of grep.

Dont need any quotes for variable expansion


Report •

#5
April 28, 2010 at 09:23:24
Let's break it up:
___
Inside each .cfg file there are tens or hundreds of lines. I am interested in getting the line that start with hostname and followed by the device name. So, the line would like like this:

be-05-1591871e005.cfg:hostname "BE1RR0967"
___


grep :hostname filename.cfg

This basic command will take the lines containing hostname. Either you make the first (outer) loop to do this, or you create a new file for each incoming file, ex.

grep :hostname filename.cfg > filename.cfg.hst

I would propose to create a file, that way, you avoid nested loops, which are a pain (my opinion).



Report •

#6
April 28, 2010 at 09:26:13
And step 2:
____
Now, I need to look for the hostnames that are in the other file located in /tmp/namelist
____


Run a FOR /F loop against filename.cfg.hst, use the double quote as a separator, take out occurance number 2, which is the device name, and grep that against /tmp/namelist

You can use exitcodes to see if any occurance matches


Report •

#7
April 28, 2010 at 17:46:00
So what does a line in a .cfg file look like? Does it or does it not have " (quotation marks) around the device name? If it does have ",

for name in `cat /tmp/namelist2`; do

  grep -i "^hostname \"$names\"" *

done > result.list

should do the job. Note that the search pattern would be in " so that $names can be replaced. This will search all files in the directory (use *.cfg if you only want to look in those files). If your namelist2 file has the correct case for names already, you can drop the -i (search case insensitive). This will speed up the search.


Report •


Ask Question