简体   繁体   中英

Grep if else statement

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 :)

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM