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
compare files prashanth.spl Shell Programming and Scripting 0 06-18-2008 04:22 PM
compare files danabo Shell Programming and Scripting 3 05-19-2008 12:09 PM
compare two files charandevu Shell Programming and Scripting 7 03-30-2008 03:20 PM
Compare two files penfold Shell Programming and Scripting 3 04-28-2005 11:04 PM
compare files and beyond MizzGail UNIX for Dummies Questions & Answers 2 04-25-2003 01:34 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-08-2008
amir07 amir07 is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 37
Compare two files

I need to compare two files:
Basically I have an input file fileA.
which need to be compare with fileB located in /etc/lc/mbd directroy

Both file format is like:

abc01def:10.80.11.123

The input file format is:

abc01mns:10.80.11.1
dbc02mns:10.80.11.2
fbc01mns:10.80.11.3
rbc01mns:10.80.11.4
tbc01mps:10.80.11.5
abt05mns:10.80.11.6
zbc11mys:10.80.11.7
ttc01mns:10.80.11.8
hbc05mns:10.80.11.9
qbc01mns:10.80.11.10

So after comparison the script will tell me:
what has been dupicate and what not duplicate.

Thanks
  #2 (permalink)  
Old 10-08-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink Can do a diff or comm

My preference is the comm command. From the manpages --
Code:
OPTIONS
     The following options are supported:

     -1       Suppresses the output column  of  lines  unique  to
              file1.

     -2       Suppresses the output column  of  lines  unique  to
              file2.

     -3       Suppresses the output column of lines duplicated in
              file1 and file2.
If you provide samples of both files, an example command could be created.
  #3 (permalink)  
Old 10-08-2008
amir07 amir07 is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 37
Thanks.,

This approach works, what do you think:

#!/bin/ksh
while read myline
do
cnt=0
while read line
do
if [[ "$myline" = "$line" ]]
then
((cnt+=1))
break
fi
done < file1
if [[ $cnt -eq 0 ]]
then
echo "$myline" >> output.file
fi
done < file2
  #4 (permalink)  
Old 10-08-2008
amir07 amir07 is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 37
But the problem here is suppose if i have one additional line in any file, it does not compare. It compares only with exact line numbers in each file.
  #5 (permalink)  
Old 10-08-2008
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,933
you can try something like,

[ not tested ]

Code:
awk 'BEGIN{ while ( getline < "file_1" ) { arr[$0]++ } }{ if ( $0 in arr ) { printf "%s is duplicate\n", $0 } }' file_2
  #6 (permalink)  
Old 10-08-2008
treesloth treesloth is offline
Registered User
  
 

Join Date: Oct 2008
Location: Orem, Utah
Posts: 72
The diff command seems ideal for this. In particular:

Code:
diff -y file1 file2
This will give a side-by-side comparison. The man page lists more options than I've ever dreamed of using, but -y seems to answer your needs.
  #7 (permalink)  
Old 10-09-2008
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,215
Try this, the result is stored in the files dup_file and no_dup_file:

Code:
awk 'NR==FNR{a[$0]=$0;next}
$0 in a {print $0 > "dup_file";next}
{print $0 > "no_dup_file"}
' fileA fileB
Use nawk or /usr/xpg4/bin/awk on Solaris.

Regards
Sponsored Links
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 12:26 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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