The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk - comparing files dbrundrett Shell Programming and Scripting 4 08-22-2008 07:23 AM
Comparing two files superstar003 Forum Support Area for Unregistered Users & Account Problems 1 05-08-2008 12:34 AM
Comparing two files.. padarthy Shell Programming and Scripting 1 08-29-2007 05:01 AM
Comparing two files... paqman Shell Programming and Scripting 12 08-08-2007 12:45 AM
comparing shadow files with real files terrym UNIX for Advanced & Expert Users 4 02-08-2007 11:38 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 10-13-2005
Registered User
 

Join Date: Jun 2005
Posts: 19
Comparing two files

Hi,

I have a requirement to compare two files and delete the duplicates from the master file.

For eg. two files file1 and file2. Compare each row of file1 with each row file2 and delete the row from file1 if it the same as the row in file2. Hope the requirement is clear.

Your early help will be appreciated.

TA
Narayana Gupta
Reply With Quote
Forum Sponsor
  #2  
Old 10-14-2005
Neo's Avatar
Neo Neo is offline
Administrator
 

Join Date: Sep 2000
Location: Asia Pacific
Posts: 4,493
Try using a PERL script. Read both files, compare lines, delete or not, write to file, etc.
Reply With Quote
  #3  
Old 10-15-2005
reborg's Avatar
Administrator
 

Join Date: Mar 2005
Location: Ireland
Posts: 3,644
Also 'man comm'
Reply With Quote
  #4  
Old 08-04-2008
Registered User
 

Join Date: Aug 2008
Posts: 1
Lightbulb Comparing two files

grep -Fvf file1.txt file2.txt

-Renjith
Reply With Quote
  #5  
Old 08-04-2008
ynilesh's Avatar
Registered User
 

Join Date: Oct 2007
Location: Bangalore, India.
Posts: 174
check next post reply.
Reply With Quote
  #6  
Old 08-04-2008
ynilesh's Avatar
Registered User
 

Join Date: Oct 2007
Location: Bangalore, India.
Posts: 174
I have one script which compares two files and write unique data into third file. You just have to replace third file with first one.

Code:
#!/bin/bash

clear

FILE1="/shell/file1"
FILE2="/shell/file2"
FILE3="/shell/file3"

cp /dev/null $FILE3

NOTEXIT=66

if [ ! -e $FILE1 ] && [ ! -e $FILE2 ]
then 
	echo "Oops! any of the file does not exist"
	exit $NOTEXIT
fi

echo "Files which differs " > $FILE3

for a in  `cat $FILE1 | awk '{print $9}' | sed '/^$/d'` # `cat $FILE1`  `strings $FILE1 | awk '{print $9}' | sed '/^$/d'`
do
	for b in `cat $FILE2  | awk '{print $9}' | sed '/^$/d'`   #`strings $FILE2 | awk '{print $9}' | sed '/^$/d'`
	do
		if [ "$b" == "$a" ]
		then
		 	flag=0	
			break
		else
			flag=1
			c="$a"
		fi
	done
			if [ "$flag" -eq 1 ]
			then
				echo "$c" >> $FILE3
			fi
done

for b in  `cat $FILE2 | awk '{print $9}' | sed '/^$/d'` # `cat $FILE2`
do
	for a in  `cat $FILE1 | awk '{print $9}' | sed '/^$/d'` # `cat $FILE1`
	do
		if [ "$a" == "$b" ]
		then
			flag=0
			break
		else
			flag=1
			c="$b"
		fi
	done
	if [ "$flag" -eq 1 ]
			then
				echo "$c" >> $FILE3
			fi
done

echo "Result "
echo "-----------"
cat $FILE3
echo "-----------"

- nilesh
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 05:37 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0