Display match found or not using awk

Could you please try following. In case your awk supports word boundaries.

awk '
/\<apple\>/{
  app_found=1
}
/\<mango\>/{
  mango_found=1
}
/\<grapes\>/{
  grapes_found=1
}
END{
  if(app_found && mango_found && grapes_found){
    print "All 3 words found."
  }
  else{
    print "All 3 words are NOT present in whole Input_file."
  }
}
' Input_file

Edited answer: the following command has been tested with the input sample provided above and works as desired:

awk '
  BEGIN { RS = "§" }
  {print (/apple/ && /mango/&&/grapes/) ? "match found" : "match not found"}
' demo.txt

I used the char § as record separator because there is no such a char in the input and because RS = "\0" is not portable. If you feel it could happen that such a § could occur in the input file, you can use the portable solution below:

awk '
  { i = i $0 } 
  END { print (i ~ /apple/ && i ~ /mango/ && i ~ /grapes/) ? "match found" : "match not found"}
' demo.txt

Tags:

Linux

Shell

Awk