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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
file=testfile.abc
if grep -x  "filename - $file" /temp/checkfile.dat;
then
    do something
else
  do someting
fi
0
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bjeAuthor Commented:
Thank you for both solutions.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.