The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 6 01-18-2009 10:51 PM
Comparing two files superstar003 Forum Support Area for Unregistered Users & Account Problems 1 05-08-2008 04:34 AM
Comparing two files.. padarthy Shell Programming and Scripting 1 08-29-2007 09:01 AM
Comparing two files... paqman Shell Programming and Scripting 12 08-08-2007 04:45 AM
comparing shadow files with real files terrym UNIX for Advanced & Expert Users 4 02-09-2007 02:38 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-13-2005
guptan guptan is offline
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
  #2 (permalink)  
Old 10-15-2005
Neo's Avatar
Neo Neo is offline Forum Staff  
Administrator
  
 

Join Date: Sep 2000
Location: Asia Pacific
Posts: 6,730
Try using a PERL script. Read both files, compare lines, delete or not, write to file, etc.
  #3 (permalink)  
Old 10-15-2005
reborg's Avatar
reborg reborg is online now Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,229
Also 'man comm'
  #4 (permalink)  
Old 08-04-2008
myboonze myboonze is offline
Registered User
  
 

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

grep -Fvf file1.txt file2.txt

-Renjith
  #5 (permalink)  
Old 08-04-2008
ynilesh's Avatar
ynilesh ynilesh is offline
Registered User
  
 

Join Date: Oct 2007
Location: Bangalore, India.
Posts: 222
check next post reply.
  #6 (permalink)  
Old 08-04-2008
ynilesh's Avatar
ynilesh ynilesh is offline
Registered User
  
 

Join Date: Oct 2007
Location: Bangalore, India.
Posts: 222
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:17 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0