LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Reply
 
Search this Thread
Old 08-30-2006, 12:38 PM   #1
carriehoff
LQ Newbie
 
Registered: Jul 2006
Posts: 9

Rep: Reputation: 0
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
 
Old 08-30-2006, 01:06 PM   #2
pete1234
Member
 
Registered: May 2005
Distribution: Slack, FreeBSD,NetBSD, OpenBSD, Open Solaris, Minix
Posts: 172

Rep: Reputation: 30
diff - find differences between two files

Check man diff.
 
Old 08-30-2006, 01:14 PM   #3
Samoth
Member
 
Registered: Apr 2005
Distribution: Exherbo
Posts: 474
Blog Entries: 1

Rep: Reputation: 32
Although diff does a lot more than just what you want to do, you should be able to adapt to it.
 
Old 08-31-2006, 08:35 AM   #4
carriehoff
LQ Newbie
 
Registered: Jul 2006
Posts: 9

Original Poster
Rep: Reputation: 0
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
 
Old 08-31-2006, 09:52 AM   #5
Bikerpete
Member
 
Registered: Jun 2003
Location: Germany
Distribution: OpenSuSE, SLES, Debian, Ubuntu, Eisfair
Posts: 79

Rep: Reputation: 15
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
 
Old 08-31-2006, 12:45 PM   #6
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 2,004

Rep: Reputation: 304Reputation: 304Reputation: 304Reputation: 304
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.
 
Old 08-31-2006, 01:13 PM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,455

Rep: Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843Reputation: 843
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>
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script with grep MicahCarrick Programming 4 08-15-2006 01:08 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
Help with sh script syntax needed ! MikeAtVillage Programming 4 03-31-2006 02:39 AM
Grep syntax questions Phaethar Linux - General 4 04-11-2005 09:34 AM
Shell Script Syntax dragon49 Programming 1 03-12-2003 09:25 AM


All times are GMT -5. The time now is 01:19 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration