Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

File1 contains:

421 RP-001
421 RP-002
421 RP-003
550 SC-001
550 SC-002
550 SC-003
550 SC-004
550 DY-001
550 DY-002
550 DY-001
550 OU-001
550 OU-002

Script:

#!/bin/bash

Elogs=/home/elogs.txt
Errors=/home/errorcodes
    for i in `cat $Errors`; do
    #Get Error Logs
    grep "$i" /home/eximlog >> $Elogs
done

Debug:

+ cat /home/errorcodes + for i in 'cat $Errors' + grep 421 /home/eximlog + for i in 'cat $Errors' + grep RP-001 /home/eximlog + for i in 'cat $Errors' + grep 421 /home/eximlog + for i in 'cat $Errors' + grep RP-002 /home/eximlog + for i in 'cat $Errors' + grep 421 /home/eximlog + for i in 'cat $Errors' + grep RP-003 /home/eximlog + for i in 'cat $Errors' + grep 550 /home/eximlog + for i in 'cat $Errors' + grep SC-001 /home/eximlog + for i in 'cat $Errors' + grep 550 /home/eximlog + for i in 'cat $Errors' + grep SC-002 /home/eximlog + for i in 'cat $Errors' + grep 550 /home/eximlog + for i in 'cat $Errors' + grep SC-003 /home/eximlog + for i in 'cat $Errors' + grep 550 /home/eximlog + for i in 'cat $Errors' + grep SC-004 /home/eximlog + for i in 'cat $Errors' + grep 550 /home/eximlog + for i in 'cat $Errors' + grep DY-001 /home/eximlog + for i in 'cat $Errors' + grep 550 /home/eximlog + for i in 'cat $Errors' + grep DY-002 /home/eximlog + for i in 'cat $Errors' + grep 550 /home/eximlog + for i in 'cat $Errors' + grep DY-001 /home/eximlog + for i in 'cat $Errors' + grep 550 /home/eximlog + for i in 'cat $Errors' + grep OU-001 /home/eximlog + for i in 'cat $Errors' + grep 550 /home/eximlog + for i in 'cat $Errors' + grep OU-002 /home/eximlog

Why loop read all codes like "421" and "RP-001" separately. And bring duplicate result. Grep should consider codes a single string like: "421 RP-001" And debuging should like this:

+ for i in 'cat $Errors' + grep 421 RP-001 /home/eximlog

share|improve this question
up vote 2 down vote accepted

The shell will perform word splitting on $(cat $Errors). That is why you get one word at a time instead of one line at a time.

You want a while read... loop:

while read -r line; do
    #Get Error Logs
    grep "$line" /home/eximlog >> $Elogs
done <"$Errors"

read is line oriented: it reads one line at a time.

Or, this may work for you and it doesn't need any looping at all:

 grep -f "$Errors" /home/eximlog

The -f options tells grep to read patterns from a file, one pattern per line.

Also, it looks to me like your error code patterns are fixed strings not regular expressions. In that is the case, to avoid unpleasant surprises, add the -F option to grep:

 grep -Ff "$Errors" /home/eximlog
share|improve this answer
1  
Yes grep -f "$Errors" /home/eximlog works for me. thanks @John1024 – rlinux57 Jun 8 at 6:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.