[comp.unix.questions] batch file

claus@iesd.auc.dk (Claus S. Jensen) (12/03/90)

  I am trying to make a batch file, that looks through
a number of textfiles for a string. I've made up something
like this, but it doesn't work. Can anybody tell me why
this is, and perhaps if there is another way to do it.


#!/bin/tcsh
foreach file ($1)
echo $file
cat $file | grep $2
end


      Claus S.Jensen
--
########################################################################
#  Claus S.Jensen , claus@iesd.auc.dk                                  #
#  University of Aalborg, Denmark                                      #
########################################################################

weimer@ssd.kodak.com (Gary Weimer) (12/03/90)

In article <1990Dec2.211810.2302@iesd.auc.dk> claus@iesd.auc.dk (Claus S. Jensen) writes:
>  I am trying to make a batch file, that looks through
>a number of textfiles for a string. I've made up something
>like this, but it doesn't work. Can anybody tell me why
>this is, and perhaps if there is another way to do it.
>
>#!/bin/tcsh
>foreach file ($1)
>echo $file
>cat $file | grep $2
>end

You don't say why it doesn't work, but I would guess that it will work for
one file, but not more than one. With more than one file, it will use the
first file name for the file to search and the second file name for the
string to search for. It might work if you called the program using quotes:

    <program> "file1 file2 file3" string

However, if this is all your shell is doing, it would be easier just to say:

    grep string file1 file2 file3

chench@rrdstrad.nist.gov (Frank Chen) (12/06/90)

In article <1990Dec2.211810.2302@iesd.auc.dk> claus@iesd.auc.dk (Claus S. 
Jensen) writes:
>   I am trying to make a batch file, that looks through
> a number of textfiles for a string. I've made up something
> like this, but it doesn't work. Can anybody tell me why
> this is, and perhaps if there is another way to do it.
> 
> 
> #!/bin/tcsh
> foreach file ($1)
> echo $file
> cat $file | grep $2
> end

When you use wild card for filename, "quote" the file name.
Example:
% ls .
doc1.txt      doc2.txt      doc3.txt
% script "*"  text                 /* This will give you correct result */

Using "script * text" will be translated by shell into "script doc1.txt 
doc2.txt doc3.txt text" Then you are actually search for the
 string "doc2.txt" in the file "doc1.txt"


----------------------------------------
Frank Chen              
NIST                        
Bldg. 235/E-151     
chench@rrdstrad.nist.gov
chen@enh.nist.gov
chen@nbsenh.bitnet