# While loop for comparison

**URL:** <https://community.unix.com/t/while-loop-for-comparison/328326>\
**Category:** Shell Programming and Scripting\
**Created:** [May 11, 2013, 10:54pm UTC](https://community.unix.com/t/while-loop-for-comparison/328326 "2013-05-11T22:54:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sbk785](https://community.unix.com/letter_avatar/sbk785/32/6_22734026db12803ebb3e059622ac3534.png) [@sbk785](https://community.unix.com/u/sbk785)\
**Post date:** [May 11, 2013, 10:54pm UTC](https://community.unix.com/t/while-loop-for-comparison/328326/1 "2013-05-11T22:54:32Z")

</div>

hi all,  
i have two files:  
test1 contains : one two three four five (on separate lines)  
test2 contains: one ten nine (on separate lines)

I want to compare two files, test1 against test2. if any word from test1 does not match test2 display it. can someone help me with a while loop on this.

thanks

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [May 12, 2013, 12:21am UTC](https://community.unix.com/t/while-loop-for-comparison/328326/2 "2013-05-12T00:21:50Z")

</div>

This is not the appropriate forum for homework assignments.

Why would you insist on a while loop to perform comparisons? A very simple awk script would be easier (and more efficient than many common while loops that would perform this task).

---

<div class="post-metadata">

**Author:** ![sbk785](https://community.unix.com/letter_avatar/sbk785/32/6_22734026db12803ebb3e059622ac3534.png) [@sbk785](https://community.unix.com/u/sbk785)\
**Post date:** [May 12, 2013, 2:05pm UTC](https://community.unix.com/t/while-loop-for-comparison/328326/3 "2013-05-12T14:05:04Z")

</div>

thanks for your reply but for some reason my while read command doesnt seem to work ... not sure where i am getting it wrong

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [May 12, 2013, 2:43pm UTC](https://community.unix.com/t/while-loop-for-comparison/328326/4 "2013-05-12T14:43:58Z")

</div>

Is this a homework assignment?

What have you tried so far?

---

<div class="post-metadata">

**Author:** ![sbk785](https://community.unix.com/letter_avatar/sbk785/32/6_22734026db12803ebb3e059622ac3534.png) [@sbk785](https://community.unix.com/u/sbk785)\
**Post date:** [May 13, 2013, 6:12am UTC](https://community.unix.com/t/while-loop-for-comparison/328326/5 "2013-05-13T06:12:01Z")

</div>

hi Don

here it is:

```nohighlight
while read -r line
do
for i in $(cat test2)
do
if [$line != $i];
then
echo " The different value us $line"
fi
done
done < test1

```

i do not get the correct output. As per the my expectation i shud be getting all line from test1 except for the word "one". can you please help me correct the code. I am trying to extract lines from test1 that do not match lines in test2.

thanks in advacne
