Visit Our UNIX and Linux User Community


Comparing two txt files - pls help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing two txt files - pls help
# 1  
Old 01-23-2008
CPU & Memory Comparing two txt files - pls help

Hi,

I have some text files. I need a separate shell say 1.sh in which i can open a particular text file and compare with another txt file. For example:

1.log.txt contains

apple
ball
cat
goat

2.log.txt contains

goat
cat
lion
apple
fox

In my i.sh i need to write script to compare this two files and write the the o/p.The output should be written onto two different files such that one file contains the words that match and another contains the words that do not match.That is after i run 1.sh i should get common.txt and
notcommon.txt


common.txt should contains words present in both(common to both 1.og.txt & 2.log.txt ).i.e;

apple
ca
goat

nocommon.txt should contain the words present only in 2.log.txt and not in 1.log.txt . i.e;
lion
fox

Thanks in advance
JS
# 2  
Old 01-23-2008
I have searched the forum and got the answer for this...I used "comm"

Thanks
JS
# 3  
Old 01-23-2008
Remmeber that comm will work only for sorted files.
# 4  
Old 01-23-2008
comm

Hi,

If you are not considering the sequence of the line in the original txt file. I think you can use comm command.

Try below one.

Code:
sort file1 > file1.temp
sort file2 > file2.temp
comm -12 file1.temp file2.temp > common.txt
comm -13 file1.temp file2.temp >uncommon.txt

# 5  
Old 01-23-2008
That is a better code than waht i tried ..
i didnot sort my source files but it was almost in a sorted way .. But its always a good method to sort the files before compiling it ..
Thanks a lot for the quote ..
Regards
JS
# 6  
Old 01-23-2008
Quote:
Originally Posted by dennis.jacob
Remmeber that comm will work only for sorted files.
thanks for pointing it out .. Actually teh fils where sorted. Hence I ddint go for sorting .. The example i gave was for the name sake ..its not teh actual format of my file ..

thanks a lot for pointing it out .. because i didnt know about it .. its going to help me in the future ..

thanks a lot
JS

Previous Thread | Next Thread
Test Your Knowledge in Computers #255
Difficulty: Easy
The consumer version of Mac OS X was launched in 2001 with Mac OS X 10.0.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare two txt files,mismatches will be in new txt files

Hi, Below are the sample data for txt files. txt file 1 Partnumber|catgroup_id 10001082|46016 10001093|4680 10001093|386003 10001093|463004 10003251|683 10003251|63005 10003252|463005 10003252|4683 10003260|463005 10003260|4683 10003264|4683 10003264|463005 13420000|67... (5 Replies)
Discussion started by: Ankita Talukdar
5 Replies

2. UNIX for Dummies Questions & Answers

Pls. help with script to remove million files

Hi, one of the server, log directory was never cleaned up. We have so many files. I want to remove all the files that starts with dfr* but I get error message when I use the *. rm qfr* bash: /usr/bin/rm: Arg list too long I am trying to write this script but not working. ... (4 Replies)
Discussion started by: samnyc
4 Replies

3. Shell Programming and Scripting

Help pls...Rename .dat files

Hi, I have some .dat files in some directories , i want them to be moved to another directory by changing file names. for ex: cat > /ai/rcmid/feb_files/temp/temp.txt aaa.dat bbb.dat rm -rf main rm -rf a001 rm -rf a002 mkdir main mkdir a001 mkdir a002 touch a001/aaa.dat touch... (1 Reply)
Discussion started by: Ravindra Swan
1 Replies

4. Shell Programming and Scripting

Comparing two .txt files

i am working on a shell script and need help in the comparing part of it. for e.g. there two text files like this: file1.txt Code: name1 name2 name3 file1 has to be comared with file2 defaultfile.txt Code: name1 name2 (16 Replies)
Discussion started by: draghun9
16 Replies

5. UNIX for Dummies Questions & Answers

Comparing two txt files with AWK

Hi, I need to compare two text files with awk. File1: ------- chr1 43815007 43815009 COSM19193 REF=TG;OBS=AA;ANCHOR=G AMPL495041 chr1 43815008 43815009 COSM18918 REF=G;OBS=T;ANCHOR=T AMPL495041 chr1 115256527 115256528 ... (6 Replies)
Discussion started by: RushiK
6 Replies

6. Shell Programming and Scripting

Comparing Strings in 2 .csv/txt files?

EDIT: My problems have been solved thanks to the help of bartus11 and pravin27 This code is just to help me learn. It serves no purpose other than that. Here's a sample csv that I'm working with - #listofpeeps.csv Jackie Chan,1954,M Chuck Norris,1930,M Bruce Lee,1940,M This code is... (13 Replies)
Discussion started by: chickeneaterguy
13 Replies

7. Solaris

list files .txt and .TXT in one command

Dear experts, In a directory i have both *.TXT and *.txt files. I have a script- for file in `ls *.txt`; do mv $file /tmp/$file How to list both *.txt and*.TXT file in one command so that script will move both .txt or .TXT whatever it find. br//purple (4 Replies)
Discussion started by: thepurple
4 Replies

8. Shell Programming and Scripting

Comparing two .txt files in shell scripting...

Hi, I have two big .txt files.and i need to compare those two files and redirect it into some other file. If any body wants to resolve this issue then i can send the two text files. Need some quick responce. Thanks, prakash (10 Replies)
Discussion started by: prakash123
10 Replies

9. Shell Programming and Scripting

Comparing two files (comm or awk) - help - very urgent pls

I an unix newbie and am confused about the working of "comm". I have two files to be compared and the three columns of output after comparison should be stored a three separate files. File abc -------- pink orange green blue black maroon File xyz --------- pink yellow grey... (3 Replies)
Discussion started by: manthasirisha
3 Replies

Featured Tech Videos