bashshellgrepzgrep

zgrep on multiple input variables issue


I have an issue using zgrep. I want to look for a XML file based on an ID and a certain Date given by the user. Each XML file contains an ID and a date in it. The program should acknowledge that these files may have the same ID but a different Date. I tried to use zgrep but I don't know how to look for two different variables and my code doesn't work. Here's my code:

echo "Introduce ID: "
read -r InputCode
echo "Set a specific Date [ DD-MM-YYYY ]: "
read -r Date

#Search patterns in every zip folder
find . -print0 | xargs -0 zgrep -l $InputCode && $Date

I would appreciate your help!


Solution

  • You cannot grep both ID and Date with a single grep command if they are not on the same line. Try this:

    find . -print0 | xargs -0 zgrep -lZ -e "$InputCode" | xargs -0 zgrep -l -e "$Date"