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
# 8  
Old 02-04-2009
There are two files file1.txt and file2.txt
and the contents of files are given as follows:
file1.txt
{
EnterpriseServices
GlobalUserManagement
USAccountSummary
USEStatement
MYCAServices
EnterpriseServices
MYCAServices
USEStatement
}
file2.txt
{
USEStatement
MYCAServices
EnterpriseServices
MYCAServices
USEStatement
DocGen
OnlineDisputes
Transfer
INTLEStatement
}
Now the problem is how to compare these two files and redirect to an output file the contents which are present in file1.txt but which are not present in file2.txt

Please help in this regard.
# 9  
Old 02-04-2009
Quote:
Originally Posted by usha rao
There are two files file1.txt and file2.txt
and the contents of files are given as follows:
file1.txt
{
EnterpriseServices
GlobalUserManagement
USAccountSummary
USEStatement
MYCAServices
EnterpriseServices
MYCAServices
USEStatement
}
file2.txt
{
USEStatement
MYCAServices
EnterpriseServices
MYCAServices
USEStatement
DocGen
OnlineDisputes
Transfer
INTLEStatement
}
Now the problem is how to compare these two files and redirect to an output file the contents which are present in file1.txt but which are not present in file2.txt

Please help in this regard.
You can use comm -13 or comm -23. Just play with it a little
# 10  
Old 02-04-2009
I am not sure how exactly i can use comm -13 or comm -23 because i need to write some generic script to compare two files and redirect the output to a third file with the contents which are present in first file but not present in second file.
# 11  
Old 02-11-2009
Hi all,
i tried comm command in all the possible ways but could not get the expected result.
I will explain one more time.i have two files containing some words ,it is not necessary that the worrds are in same order in both the files but same words will be there.i want to compare those files and redirect the result to a third file which will contain all the words which are present in first file but not in second.

comm command is not solving the purpose as it compares line by line even if a same word is present in 1st line of file one and 4th line of file two then it is giving the output.

The result should be like : if same words are present in both the files no matter on which line number they are then the final output should be zero.

Please help.
Thanks
# 12  
Old 02-11-2009
Something like this???

grep -v -f file2 file1
# 13  
Old 02-11-2009
The problem in the solution you have given is there is no -f option with grep command?
Could anyone help me out??

Can anyone tell how to use a loop inside awk command so that the words of second file are compared with all the words of first file and those that are present in first file but not in second are given as output??
Thanks in advance
# 14  
Old 02-12-2009
Try this it should work as long as you have saved file1.txt & file2.txt seprately
Code:
diff file1.txt file2.txt | grep ">" | sed 's/>//g'


Last edited by vbe; 02-12-2009 at 01:38 PM.. Reason: added tag
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