How to check a file against a table

hello,
I would like to check a file against a table (file name.dat)

 filename - testfile.abc
 table (file name called ) - checkfile.dat  ( the checkfile.dat would have several file names in it )

If file called testfile.abc is in checkfile.dat then do this or if it does not then do this

would this work,

if [ ` grep -c -f /temp/checkfile.dat $file ` -ne 0 ]
 then
    do something
else
  do someting
fi
bjeAsked:
Who is Participating?
 
Gerwin Jansen, EE MVETopic Advisor Commented:
The -f option is used when you specify a file that is containing patterns that you want to 'grep' for in other file(s). So in this case you don't use -f because you just want to know whether the filename testfile.abc is present in checkfile.dat - you would just have to:
grep testfile.abc checkfile.dat

Open in new window


and because you're not interest in grep's output, you should specify the -q (quiet) option. Also, you don't need to run that grep in it's own shell (backquotes), you can just evaluate the result (return code), so:
if $(grep -q "$1" checkfile.dat)
then
  echo "Do something when found"
else
  echo "Do something else when not found"
fi

Open in new window

(I've put the above in a script where the first parameter is the filename to specify)
0
 
ozoCommented:
file=testfile.abc
if grep -x  "filename - $file" /temp/checkfile.dat;
then
    do something
else
  do someting
fi
0
 
bjeAuthor Commented:
Thank you for both solutions.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.