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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Comparing two files using awk kanu_kanu Shell Programming and Scripting 2 09-16-2008 08:30 AM
Comparing two files guptan Shell Programming and Scripting 5 08-04-2008 09:02 AM
Comparing two txt files - pls help jisha Shell Programming and Scripting 5 01-23-2008 04:14 AM
Comparing two files kingofprussia UNIX for Dummies Questions & Answers 2 08-01-2007 01:25 PM
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 12-10-2008
bab123 bab123 is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 14
Unhappy comparing two files

Hi,
some one please help me

I have two files.each having two coloumns and many rows
I want to write the difference between the two files to a new file

sample:


file1

1 2
4 5
8 6

file2

1 2
3 5
8 4
3 6
9 7


I tried with 'diff' and 'comm' commands.i got the output that include differenet rows in both the files as well as extra rows in second file.

I got the output as.

4 5
3 5
8 4
8 6
3 6
9 7

but I want the output like

4 5
3 5
8 4
8 6

I want only the changes in rows from first file to second file in the output.Not the extra rows in table 2.


Please guide me
regards
bab123
  #2 (permalink)  
Old 12-10-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,333
Use grep, check the manpage for the options -v and -f.

Regards
  #3 (permalink)  
Old 12-10-2008
methyl methyl is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 1,184
An inefficent way in shell script would be to read file1 record-by-record then look at the corresponding record in file2. This script is unsuitable for large files and would be better in awk where you could read a both files in parallel.
The script assumes that file1 does not contain more records than file2.

#!/bin/ksh
COUNTER=0
cat file1|while read LINE1
do
COUNTER=`expr $COUNTER + 1`
LINE2=`cat file2|head -${COUNTER}|tail -1`
if [ ! "${LINE1}" = "${LINE2}" ]
then
echo "${LINE1}"
echo "${LINE2}"
fi
done
  #4 (permalink)  
Old 12-10-2008
bab123 bab123 is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 14
Thanks methyl.
Can you help me using the awk command
  #5 (permalink)  
Old 12-10-2008
methyl methyl is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 1,184
Sorry, there are many better than I with awk on this forum.
Please post the number of records in the file so folks know whether efficency is important.
  #6 (permalink)  
Old 12-11-2008
bab123 bab123 is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 14
Thanks for the reply.
There are around 70,000 records in the file
  #7 (permalink)  
Old 12-11-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,333
Try this:


Code:
awk 'NR==FNR{a[$0];next}!($0 in a)' file1 file2

Regards
Closed Thread

Bookmarks

Tags
shell script, shell scripting, unix scripting, unix scripting basics

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 07:00 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