write a script to compare two files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting write a script to compare two files
# 15  
Old 02-15-2009
Hope this helps:

Code:
ddl10197@GT109867 ~
$ sort foo | uniq > foo.tmp

ddl10197@GT109867 ~
$ sort bar | uniq > bar.tmp

ddl10197@GT109867 ~
$ comm -23 foo.tmp bar.tmp
GlobalUserManagement
USAccountSummary

ddl10197@GT109867 ~
$ grep GlobalUserManagement bar

ddl10197@GT109867 ~
$ grep USAccountSummary bar

# 16  
Old 02-23-2009
Hi All,
Thanks for your response.But the solution provided seems not to be dynamic.
If any one can help running a for loop comparing the strings one by one in both the files i.e(comparing the first string from first file with all the strings in second file if it is not present outputting into third file).

i have got a code chunk,but it is not working,
can anyone help me modify it for the above requirement?

first code chunk i got is:
awk 'FILENAME=="file1" { if( $0 in arr) {continue} else {print $0 } } FILENAME=="file2" {arr[$0]++ }' file2 file1

but i guess there is some syntax problem as it is not working


The other code chunk i got is:

#!/usr/bin/ksh
set -x
> outputfile
awk '
BEGIN {
while ( getline < "file1" ) { arr[$0]=1 }
}
{ if ( arr[$0] != 1 ) print FILENAME":" $0
else delete arr[$0];
}
END {
for( key in arr )
if ( arr[key] == 1 ) print "file1:" key
} ' file2 >> outputfile


Can anyone help me finding what is the problem with the above two code chunks as i am not able to run these,getting some errors


Else,if both the codes doesnt work,could anyone help me providing the code to compare two files containing strings and output the string that are present in first file but not in second in third file.

You can use arrays or any other way,

The files will look like

file1
BasicServicesClone2
BasicServicesClone1
DocGenClone1
USEStatementClone2
USEStatementClone1

file2
BasicServicesClone2
BasicServicesClone1
DocGenClone1
USEStatementClone2


the output should be
USEStatementClone1


as it is present in file 1 but not in file2
# 17  
Old 02-23-2009
angheloko's solution works.
# 18  
Old 02-23-2009
Quote:
Originally Posted by usha rao
Else,if both the codes doesnt work,could anyone help me providing the code to compare two files containing strings and output the string that are present in first file but not in second in third file.

You can use arrays or any other way,

The files will look like

file1
BasicServicesClone2
BasicServicesClone1
DocGenClone1
USEStatementClone2
USEStatementClone1

file2
BasicServicesClone2
BasicServicesClone1
DocGenClone1
USEStatementClone2


the output should be
USEStatementClone1


as it is present in file 1 but not in file2
Do you have a grep version without the -f option? With awk you can get the desired output with:

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

Regards
# 19  
Old 02-23-2009
Hi Franklin52,

Thanks for your update but when i am trying the code which you have given,



It is showing as below:



awk 'NR==FNR{a[$0];next}!($1 in a)' file2 file1
awk: syntax error near line 1
awk: bailing out near line 1



angheloko's solution was good,there he was grepping seperately for each string which should not be the case, as i have to compare two files containing strings line by line and it should be generic i.e the strings may change and i have to output the strings which are present in first file and not in second.The file format i have mentione in my previous post.
Could anyone help me writing a generic script to first store all the strings of both the files in arrays and compare those arrays and one by one??

Because the above line of code is not working here,Syntax error?

Could anyone help??

Thanks in advance
# 20  
Old 02-23-2009
Quote:
Originally Posted by usha rao
Hi Franklin52,
It is showing as below:
awk 'NR==FNR{a[$0];next}!($1 in a)' file2 file1
awk: syntax error near line 1
awk: bailing out near line 1
Use nawk or /usr/xpg4/bin/awk on Solaris.

Regards
# 21  
Old 02-23-2009
SmilieSmilie
Hey thanks all,its working now..!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to open files and write into new one

Hello! I am a real beginner in scripting, so I am struggling with a really easy task! I want to write a script to concatenate several text files onto each other and generate a new file. I wanted the first argument to be the name of the new file, so: ./my_script.sh new_file file1.txt... (5 Replies)
Discussion started by: malajedala
5 Replies

2. Shell Programming and Scripting

Compare two files and write data to second file using awk

Hi Guys, I wanted to compare a delimited file and positional file, for a particular key files and if it matches then append the positional file with some data. Example: Delimited File -------------- Byer;Amy;NONE1;A5218257;E5218257 Byer;Amy;NONE1;A5218260;E5218260 Positional File... (3 Replies)
Discussion started by: Ajay Venkatesan
3 Replies

3. Shell Programming and Scripting

Request to check: compare two files , match same entries, write data before it

Hi all, I have 2 files:Column1 of first file has to be matched with column 3 of second file first file contain DATA like this in 2 columns one with gene name second with whether CAD,HT,RA T2Dor any one column 1 column2 ARFGEF2 CAD DDEF2 CAD PSCD3 CAD PSCD4 CAD CAMK1... (5 Replies)
Discussion started by: manigrover
5 Replies

4. UNIX for Dummies Questions & Answers

Not sure how to write a script to delete certain files?

I'm a total *nix newb so I really could use some help! :o I plan on running a cron job on my server that deletes some files and then backs up and emails the website for archival purposes. Before I get in too deep I'd like to remove some unnecessary files that phpThumb creates. I'm not sure... (4 Replies)
Discussion started by: Sinistral
4 Replies

5. Shell Programming and Scripting

write script for more then 600 files

Hi, i have 600 or more file on my server and from this 600 files i want to make one file. i have written this script SPATH=/etlstg/DAT TPATH=/etlstg/DAT/DAT_PROCESSED FILE_DATE=`TZ=CST+24 date +20%y%m%d` a1=0 while do a2=0 while do a3=0 while ... (1 Reply)
Discussion started by: prasson_ibm
1 Replies

6. Shell Programming and Scripting

Write a new file from 2 files as input to the script

Hi- I am hoping someone can give me some pointers to get me started. I have a file which contains some dn's .e.g file 1 cn=bob,cn=user,dc=com cn=kev,cn=user,dc=com cn=john,cn=user,dc=com I have a second file e.g. file.template which looks something like :- dn: <dn> objectclass:... (5 Replies)
Discussion started by: sniper57
5 Replies

7. Shell Programming and Scripting

to write a script to compare the file size in the current directory and previous dir

hi, i am new to this site. i want to write a script to compare the file size of the files in the current dir with the files in the previous directory. the files name will be same, but the filename format will be as xyzddddyymm.txt. the files will arrive with the month end date(i want to... (5 Replies)
Discussion started by: tweety
5 Replies

8. Shell Programming and Scripting

Need to compare two csv files values and write into another csv file

Hi all, Am new to scripting. So i just need your ideas to help me out. Here goes my requirement. I have two csv files 1.csv 2.csv abc,1.24 abc,1 def,2.13 def,1 I need to compare the first column of 1.csv with 2.csv and if matches then need to compare... (2 Replies)
Discussion started by: chinnahyd
2 Replies

9. Shell Programming and Scripting

script to compare files

HI i wil get input from sql query and that too i can get a list o f files or just one. i have to pick up a file from another directory which hads prefix to this prefix.x.x.x.x.x. And we have to discard prefix and use that file name. we have to compare this file name(no need... (0 Replies)
Discussion started by: pulse2india
0 Replies

10. Shell Programming and Scripting

Script with read/write Files

Hello, I am a Newbie in ksh Unix Script. So I must write a ksh/sh script who read character at a position in a File. So also it must read all the lines who belongs at these characters , then write these lines in a another File. Can you help me , or give little councils to advance with my... (5 Replies)
Discussion started by: steiner
5 Replies
Login or Register to Ask a Question