I am trying to zgrep multiple strings with the code below, but if I omit one of the parameters it spools a lot of un-matching files. If I enter all the 5 strings it works correctly. How can I zgrep any number of strings even if its just 3 of 5.

echo "Enter string 1: "
read isdn1
echo "Enter string 2: "
read isdn2
echo "Enter string 3: "
read isdn3
echo "Enter string 4: "
read isdn4
echo "Enter string 5: "
read isdn5

for host in $(cat host.txt); do 
    ssh "$host" "cd /onip/cdr/output/snapshot/normal/backup && 
      zgrep '$isdn1\|$isdn2\|$isdn3\|$isdn4\|$isdn5' xyz_shot*"
  • With single quotes you have no replace the variable with value. So you can try with double quotes Sep 29 '16 at 11:47
  • @RomeoNinov, the string is in double-quotes, it just contains some single-quotes too. But quotes don't nest, so it doesn't matter.
    – ilkkachu
    Sep 29 '16 at 19:36

if I omit one of the parameter it spools a lot of un-matching files.

Once you omit, you get an expression like || which is empty so everything matches. You have to check the input and build your expression correctly.

If the strings might also contain special characters, maybe you would prefer the --fixed-strings option of grep.



echo "Enter string: "
while read string
    [ ${#string} -eq 0 ] && break # blank line cancels

echo "You entered: "
echo ----
echo -n "$isdn"
echo ----

# your ssh user@host "zgrep -F '$isdn' ..." here
#    or maybe this would allow ' in filenames too:
# echo -n "$isdn" | ssh user@host zgrep -F -f - ...

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.