Home > Software engineering >  Grep if else statement
Grep if else statement

Time:07-23

I'm trying to build a script that will look for changes between two files and action a task if changes have been found.

grep -vf file2 file1

The grep command above displays the differences between the two files but I'm not sure how to combine that with an if statement.

The file contains 11 digit telephone number.

Thanks :)

CodePudding user response:

grep is not the right tool to compare files. It will be inefficient, slow and may be buggy.

It would be much more efficient to use cmp for this comparison

if cmp -s file1 file2; then
   echo "same"
else
   echo "different"
fi

CodePudding user response:

If you intend to do something based on the differences, I mocked up a couple files of phone numbers to generate output from, following your example. This came to mind:

OutPut=`grep -vf file2 file1`
if [ -z "$OutPut" ]
then
   echo No Output
else
   for i in $OutPut
   do
      echo Do something with phone number $i
   done
fi

Hope that makes sense and is useful.

  • Related