LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   grep script/syntax (https://www.linuxquestions.org/questions/linux-newbie-8/grep-script-syntax-478831/)

carriehoff 08-30-2006 12:38 PM

grep script/syntax
 
Hi all,

I was originally working with this script that would compare two text files. This script compares the files and lists the matching line if there is a match, or the matching line and the word 'NOT' if it doesn't match.
Code:

exec 3< /usr/local/photodir-.txt
while read line <&3
do
        if grep -q $line /tmp/mlsnos.txt
        then
                echo "$line"
        else
                echo "$line NOT"               
        fi
done

All I really want is a list of the files that don't match. The script won't work if I omit the match condition:
Code:

exec 3< /usr/local/photodir-.txt
while read line <&3
do
        if grep -q $line /tmp/mlsnos.txt
        then
        else
                echo "$line NOT"               
        fi
done

and the following script won't work, even though I think the -v flag is supposed to produce non matching lines.
Code:

exec 3< /usr/local/photodir-.txt
while read line <&3
do
        if grep -q -v $line /tmp/mlsnos.txt
        then
                echo "$line NOT"               
        fi
done

Can anybody tell me what will work? I want to compare two text files and produce a list of the files in photodir-.txt that are 'NOT' in the file mlsnos.txt.

Thanks,
Carrie

pete1234 08-30-2006 01:06 PM

diff - find differences between two files

Check man diff.

Samoth 08-30-2006 01:14 PM

Although diff does a lot more than just what you want to do, you should be able to adapt to it.

carriehoff 08-31-2006 08:35 AM

Good, but missing something
 
Well, I have found diff to work much faster, but as far as I've read, the closest I can come to what I want is by displaying all of the results, not just the ones that don't match:

Code:

diff -u /usr/local/photodir-.txt /tmp/mlsnos.txt
That will list out every single line (all 26,000+) of photodir-.txt with either a plus or minus sign preceding the content to tell me if it exists in mlsnos.txt or not.
Is there a way to use diff to display just the lines that do not match?

Carrie

Bikerpete 08-31-2006 09:52 AM

How about
diff -u /usr/local/photodir-.txt /tmp/mlsnos.txt
cat /tmp/mlsnos.txt |grep - > /tmp/notinmlsnos.txt

- for the preceding minus sign

Hope it helps,

Pete

haertig 08-31-2006 12:45 PM

Check out the "comm" command. By default it compares two files and it's output is in three columns: (1) lines only in file 1, (2) lines only in file 2, and (3) lines in both files.

You can give comm some commandline parameters to suppress one or more columns of the default output. e.g., "comm -1 file1 file2" will suppress column one (lines unique to file 1) e.g., "comm -12 file1 file2" suppresses columns one and two, and lists ONLY lines that are common to both files. Run "man comm" to see invokation details.

ntubski 08-31-2006 01:13 PM

Quote:

Originally Posted by carriehoff
Is there a way to use diff to display just the lines that do not match?

diff --side-by-side --suppress-common-lines <file1> <file2>
or
diff -u0 <file1> <file2>
or
diff -c0 <file1> <file2>


All times are GMT -5. The time now is 12:16 PM.