1

I have a file sites.txt that is generated by a script. It's a list of host names.

I have another list of host names sites_old.txt.

I need to check whether any of the host names present in the sites.txt are present in sites_old.txt.

I tried following but failed:

#!/bin/bash

if grep -Fxq "$sites.txt" sites_old.txt

then
        found something// 
else
        not found//
fi

I also tried for but failed. I know I'm not using the statements correctly.

  • 1
    You're missing a switch that tells grep to get the patterns/strings from a file... – don_crissti Mar 4 '16 at 13:02
  • You'll want to read the man pages for bash, grep, and the built in test. This is not a working script. – user156990 Mar 4 '16 at 13:13
  • @jdv - why would you need the builtin test ? this is a functional script and should work OK when grep is used with the right option. – don_crissti Mar 4 '16 at 13:21
  • 1
    @jdv, the only problem is with the grep command (should be grep -Fxqf sites.txt sites_old.txt). There's no need for a test command here (though reading the man pages wouldn't harm of course). – Stéphane Chazelas Mar 4 '16 at 13:22
  • Because it makes it clearer what we are doing here. It is idiomatic shell scripting and good practice. – user156990 Mar 4 '16 at 13:23
1

You need fgrep -f or grep -F -f:

if grep -Fxq -f "$sites.txt" sites_old.txt
then
      : found something
else
      : nothing found
fi
  • Both fgrep -f and grep -F -f worked perfect. Thanks! – Koshur Mar 6 '16 at 3:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.