Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.