1

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*" done 
2
  • With single quotes you have no replace the variable with value. So you can try with double quotesCommentedSep 29, 2016 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
    CommentedSep 29, 2016 at 19:36

1 Answer 1

1

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.

Untested:

isdn="" echo "Enter string: " while read string do [ ${#string} -eq 0 ] && break # blank line cancels isdn="$isdn$string"$'\n' done 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 - ... 

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.