Compare two files using shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare two files using shell script
# 1  
Old 07-12-2011
Lightbulb Compare two files using shell script

Hi
i want to compare two files and i need the o/p of only difference

here the files
file1
Code:
achilles
aedxbepo
aedxbwdm01
aedxbwdm02
albedo
amarice
ambrister
anakin
anton
argon
artephius
asgard
avatar
aymara
bach
baldur
baltar
bancroft
bee
beethoven
beknods1 / yogi
beknoepo
benosds01 / yakky
bittem
bluestreak
blume
bobientje
boemel
bolder
boobie
boomer
bragi
brockman
brown
burton
caillou
casper
chalmers
checkov
chopin
cibinensis
citrinitas
cliffjumper
coen
coot
copernicus
coppola
dandy
defraitdsrv01 / s3 fra
defraitdsrv02 / s3 fea backup
defrargdb
dido
django
dodi
dqmkt
dualla
duck
egret
eira
elodie
emile
epius
esbcns3srv01
esmcvs3svr01
fattony
fedexukftp
filiberke
filiberke
fortran
fozzie
freyr
frisco
frusi
fulmar
galactica
galileo
galileo
galvatron
gawin
geber
gonzo
grutte
gurani
hector
hecuba
heimdall
helium
hermod
hildorf
hod
honir
horst
horton
hotherus
iene
inbomwdm01
inca
itmhqlnxapp02
itmhqlnxapp03
itmhqlnxapp04
itmhqlnxapw01
itmhqlnxcedi1
itmhqlnxcedi2
itmhqlnxdim01
itmhqlnxdim02
itmhqlnxhblu
itmhqlnxs303
itmxplnxapw02
itmxplnxs302
itreksjed
jackson
janssen
janssen
jazz
jommeke
jord
kent
kir00097
krypton
kubrick
kwak
lalo
larousse
lessley
louise / pngemea04
lovejoy
lucas
mallard
manush
mcclure
mccoy
mercury.emea.fedex.com
miene
milhouse
mimir
ming
moleman
molly
morton
mozart
mungalia
murdo
mutte
nabucco
natch
navi
nessa
nigredo
nimbus
nleinwww1 hp
nowa
octaaf
odilon
oran
osthanes
perceptor
petrel
phantom
pinkas
pintail
pngemea01
pngemea02
porter
priam
prowl
pryscilla
puffin
pumyra
rembrandt
roslin
rubedo
ruby
s3primary
s3secondary
sansiro
scotty
seagull
sharita
shrek
sidious
skjold
skua
snoid
solero
song
spielberg
spo00004
sunstreak
suzy
synesius
talon
tancredus
tang
tarantino
thetis
tidiamh
tolkien
tolkien
trypticon
tupi
twister
tyr
uhura
verdi
vidar
wagner
wheeljack
wigeon
windcharger
xenon
zichen
zolder
zosimos
zuse
zuse
zussen

and Files2 as follows
Code:
         achilles
             aedxbwdm01
             albedo
             amarice
             ambrister
             anakin
             anton
             asgard
             avatar
             aymara
             bach
             baldur
             baltar
             bancroft
             bee
             beethoven
             beknoepo
             bluestreak
             blume
             bobientje
             bolder
             boobie
             boomer
             bragi
             brockman
             brown
             burton
             caillou
             chalmers
             chopin
             cibinensis
             citrinitas
             cliffjumper
             coen
             coot
             copernicus
             coppola
             dandy
             defrargdb
             denebola
             dido
             django
             dodi
             dqmkt
             droes
             dualla
             duck
             egret
             eira
             emeads
             emile
             epius
             esbcns3svr01
             expo
             fattony
             fedexukftp
             fergus
             filiberke
             fortran
             fozzie
             freyr
             frisco
             frusi
             galactica
             galileo
             galvatron
             gawin
             geber
             gonzo
             gridhouse
             gridman
             grutte
             hector
             hecuba
             heimdall
             hermod
             hibbert
             hildorf
             hod
             honir
             horst
             horton
             huffer
             iene
             inca
             itmhqlnxapw01
             itmhqlnxcedi1
             itmhqlnxcedi2
             itmhqlnxhblu01
             itmhqlnxs303
             itmxplnxapw02
             itmxplnxs302
             itreksjed
             jackson
             janssen
             jazz
             jommeke
             jord
             kent
             kubrick
             lalo
             larousse
             lessley
             lorna
             louise
             lovejoy
             lucas
             manush
             mcclure
             mercury
             miene
             milou
             mimir
             ming
             moleman
             molly
             momfer
             morton
             mozart
             mungalia
             murdo
             mutte
             nabucco
             navi
             nessa
             nigredo
             nimbus
             octaaf
             oculus
             oran
             orion
             osthanes.eema.fedex.com
             paracelsus
             perceptor
             phantom
             pinkas
             porter
             priam
             propus
             prowl
             pryscilla
             pumyra
             raket
             rembrandt
             roslin
             rubedo
             sharita
             shrek
             sidious
             skjold
             solero
             spielberg
             sunstreak
             suzy
             synesius
             talon
             tancredus
             tarantino
             thelma
             thetis
             tidiamh
             trypticon
             tupi
             twister
             tyr
             vidar
             wagner
             wheeljack
             windcharger
             zeus
             zichen
             zoef
             zolder
             zosimos
             zussen

Kindlu let me know the script for find the difference of the above two files ....

Last edited by venikathir; 07-13-2011 at 04:31 AM.. Reason: [SOLVED]please use code tags
# 2  
Old 07-12-2011
Code:
diff file1 file2

# 3  
Old 07-12-2011
i know this but i want only the difference if the both file having same line i dont want the o/p
# 4  
Old 07-12-2011
Code:
awk 'NR==FNR{A[$1]=$1;next}$1=$1{if($1!=A[$1])print}' f1 f2

Shorter:
Code:
awk 'NR==FNR{A[$1]=$1;next}{$1=$1}$1!=A[$1]' f1 f2

# 5  
Old 07-12-2011
As the files are sorted try reading up on the unix "comm" command. I assume that the second file doesn't have those leading space characters which we see in the post.

Last edited by methyl; 07-14-2011 at 01:54 PM..
# 6  
Old 07-13-2011
Venikathir,
Try with below code

Code:
rm -f  newfile.txt
while read line
do
  line1=`grep -ie "${line}" 2.txt `
  if [ $? -ne 0 ] ; then
    echo "$line" >> newfile.txt
  fi
done <1.txt

Cheers
Harish
# 7  
Old 07-13-2011
tukuyomi

thnx for the help it works fine for me
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell Script to Compare Files and Email the differences

Hi, I have 2 files abc.txt and bdc.txt. I am using $diff -y abc.txt bcd.txt -- compared the files side by side I would like to write a Shell Script to cmpare the files side by side and print the results( which are not matched) in a side by side format and save the results in another... (10 Replies)
Discussion started by: vasuvv
10 Replies

2. Shell Programming and Scripting

Shell script to compare two files for duplicate..??

Hi , I had a requirement to compare two files whether the two files are same or different .... like(files contaisn of two columns each) file1.txt 121343432213 1234 64564564646 2345 343423424234 2456 file2.txt 121343432213 1234 64564564646 2345 31231313123 3455 how to... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

3. Shell Programming and Scripting

Using shell script to compare files and retrieve connections

Hello, I want to use shell script to generate network files (I tried with python but its taking too long). I have a list of nodes: node.txt LOC_Os11g37970 LOC_Os01g07760 LOC_Os03g19480 LOC_Os11g45740 LOC_Os06g08290 LOC_Os07g02800 I have an edge-list as well: edge.txt Source_node ... (2 Replies)
Discussion started by: Sanchari
2 Replies

4. Shell Programming and Scripting

How to use awk shell script to compare and match two files?

Basically, I have two files dupestest.txt 152,153 192,193,194 215,216 290,291 2279,2280 2282,2283haftest.txt 152,ABBOTS ROAD 153,ABBOTS ROAD 154,ABBOTS ROAD 155,ABBOTS ROAD 156,ABBOTS ROAD 157,ABBOTS ROADI want to find the numbers in dupestest.txt in haftest.txt... (4 Replies)
Discussion started by: amyc92
4 Replies

5. Shell Programming and Scripting

Shell script to compare ,diff and remove betwen 2 files

Hi Friends Need your expertise. Command to check the difference and compare 2 files and remove lines . example File1 is master copy and File2 is a slave copy . whenever i change, add or delete a record in File1 it should update the same in slave copy . Can you guide me how can i accomplish... (3 Replies)
Discussion started by: ajayram_arya
3 Replies

6. Shell Programming and Scripting

Shell script to compare two files

I have two files; file A and file B. I need all the entries of file A to be compared with file B line by line. If the entry exists on file B, then save those on file C; if no then save it on file D Note :- all the columns of the lines of file A need to be compared, except the last two columns... (8 Replies)
Discussion started by: ajiwww
8 Replies

7. Shell Programming and Scripting

Shell script compare all parameters in two files and display results

Hi , I am not familiar with shell programming. I have a requirement like i have two files .I need to compare the two files by comparing each parameter and i should produce 2 outputs. 1)i have around 35 parameters say i have one parameter name called db_name=dcap in one file and... (7 Replies)
Discussion started by: muraliinfy04
7 Replies

8. Shell Programming and Scripting

Shell Script to Compare Two Files

I have a directory with about 6 files that we receive regularly. these 6 files contain information for 3 different units, 2 for each unit. files related to a specific unit are named similarly with a change in number at the end of the file. the numbers should be sequential. for each grouping of... (3 Replies)
Discussion started by: scriptman237
3 Replies

9. Shell Programming and Scripting

Compare semicolon seperated data in 2 files using shell script

hello members, I have some data ( seperated by semicolon ) with close to 240 rows in a text file temp1. temp2.txt stores 204 rows of data ( seperated by semicolon ). I want to : Sort the data in both files by field1.i.e first data field in every row. compare the data in both files and print... (6 Replies)
Discussion started by: novice82
6 Replies

10. UNIX and Linux Applications

How to compare two files using shell script

hi experts please help me to compare two files which are in different directory file1<file will be master file> (/home/rev/mas.txt} ex x1 x2 file2 <will be in different folder> (/home/rev/per/.....) ex x3 x4 the filesinside per folder i need to compare with master file... (1 Reply)
Discussion started by: revenna
1 Replies
Login or Register to Ask a Question