Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I have the below script to find missing items from fileA compared to fileB and write to fileC

script.sh fileA fileB fileC

script.sh:

rm $3
while IFS="" read -r inputline; do

fgrep -q \""$inputline"\" $1  
if [ 1 -eq $? ]; then
    echo \""$inputline"\" >>$3
fi
done <$2

I see everything in fileB getting dumped to fileC, i'm missing somethig basic. (aix 6, bash)

ps: files have trailing spaces and it matters in comparison

share|improve this question
    
Not an answer to your specific question, but perhaps comm does what you need. –  dhag Mar 27 at 21:25
    
Better to see your fileB and fileA to sea inputline but are you sure that all lines in fileA have quotes? –  Costas Mar 27 at 21:28
    
@Costas im pretty sure it's not beacuse of file contents. and no quotes in the files, one set of quotes immediately surrounding the variable is to prevent trialing spaces getting stripped off. next set (escaped quotes) are for having the string enclosed in the fgrep call. also i verified in the tasks i can see fgrep "xyz " fileA –  dbza Mar 27 at 21:56
    
Try to deal without extra quotes: if ! grep -Fq "$inputline" $1 ; then –  Costas Mar 27 at 22:02
    
I checked running processes while script is running and it shows {fgrep "xyz " fileA }-with spaces - which is exactly what i want. without 'extra' quotes it evaluates to {fgrep xyz fileA}, which is as good as {fgrep "xyz" fileA} –  dbza Mar 27 at 22:06

1 Answer 1

up vote 1 down vote accepted

Try

#!/usr/bin/bash
rm "$3"
while IFS="" read -r inputline
do
    grep -Fq "$inputline" "$1" && echo "$inputline" >> "$3"
done < "$2"
share|improve this answer
    
below one worked! thanks! but any idea why the if+test method is acting different? #!/usr/bin/bash echo "" > "$3" while IFS="" read -r inputline; do fgrep -q \""$inputline"\" "$1" && echo \""$inputline"\" >>"$3" done <$2 –  dbza Mar 27 at 22:14
1  
If+test should act the same. Problem can be in file's names quoting. Additionally Direct invocation as either egrep or fgrep is deprecated –  Costas Mar 27 at 22:19

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.