LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash - find something in one file and if it doesn't exist in the second file print. (http://www.linuxquestions.org/questions/linux-newbie-8/bash-find-something-in-one-file-and-if-it-doesnt-exist-in-the-second-file-print-4175424818/)

Rogue45 08-30-2012 12:50 PM

Bash - find something in one file and if it doesn't exist in the second file print.
 
I'm trying to compare two files. I want to grab the first line in the first file and check second file to see if it exists. If it does not exist in second file then print. And so on for every line in file1.

Right now i don't get any output stuck into my output file. I'm sure this is easy and i've just stared at it too long.

#!/bin/bash
file1=/home/user/Documents/file1.txt
file2=/home/user/Documents/file2.txt


while read $line
do
grep $line $file2 > /dev/null
if [ $? -ne 0 ]; then
echo $line >> output.txt

fi
done < $file1

MensaWater 08-30-2012 01:06 PM

while read line

NOT: while read $line

In the first you're declaring the variable which can be used as $line later in the script. In the latter you're attempting to use the variable and likely getting a null since you haven't previously declared it.

Rogue45 08-30-2012 01:17 PM

OMG. Well thank you. I was SO freaking close. Always has to be something dumb.


All times are GMT -5. The time now is 11:20 AM.