zgrep -f /onip/appt/app/yemi/usage_biglist.txt \
    /onip/cdr/output/nor/bac/filec/$count/nordat.gz \
    >> /onip/appt/app/yemi/have_usage.txt


  1. pick the first string in file usage_biglist.txt
  2. grep it from file nordat.gz and save the result to have_usage.txt
  3. pick next string in usage_biglist.txt and do the same till nth string

grep (or zgrep in your case) will always output all matching lines from a file.

You could do each pattern match individually, and pass the result through head -n 1 though:

while read -r pattern; do
    zgrep "$pattern" file.gz | head -n 1
done <pattern_list.txt >output_file.txt

In your case:

while read -r pattern; do
    zgrep "$pattern" /onip/cdr/output/nor/bac/filec/$count/nordat.gz | head -n 1
done </onip/appt/app/yemi/usage_biglist.txt >/onip/appt/app/yemi/have_usage.txt


zgrep -m 1 -f

in the place of

zgrep -f
  • 1
    As far as I am aware this is a feature of GNU grep only. It is worth to mention it. – Kalavan Jan 31 '17 at 13:11
  • 1
    @Kalavan - even if OP was using gnu grep this doesn't do what they want i.e. print first match for each string in the list – don_crissti Jan 31 '17 at 13:15
  • @don_crissti, You are right! I focused on a sub-problem and stopped there :/ – Kalavan Jan 31 '17 at 13:17

I'd use awk:

gunzip < "/onip/cdr/output/nor/bac/filec/$count/nordat.gz" |
  awk '
    !list_processed {strings[$0]; next}
      do_print = 0
      for (s in strings)
        if (index($0, s)) {
          delete strings[s]
          do_print = 1
  ' /onip/appt/app/yemi/usage_biglist.txt list_processed=1 - \
    >> /onip/appt/app/yemi/have_usage.txt

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.