Comparing two .txt files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing two .txt files
# 1  
Old 09-05-2012
Debian 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:
Code:
name1
name2
name3

file1 has to be comared with file2

defaultfile.txt


Code:
Code:
name1
name2
name3 
name4

and during comparision with defaultfile.txt

if file1.txt contains conents of defaultfile, it should echo file authenticated

if file1.txt contains content which is not in defaultfile.txt, and it should echo file not authenticated....

i tried diff command but it doesnt seeem to help me

Last edited by draghun9; 09-05-2012 at 03:07 PM..
# 2  
Old 09-05-2012
Code:
awk 'NR==FNR { A[$1]++; next } !($1 in A) { print "Not Auth"; exit 1 } END { print "Auth" }' defaultfile.txt file1.txt

# 3  
Old 09-05-2012
Thanks corona but Is there anyway i can do the same without the awk command.
# 4  
Old 09-05-2012
What's the problem with using awk?
# 5  
Old 09-05-2012
How about using comm command (assuming files are sorted)? Note that I added name5 to file1.txt to satisfy your "not authenticated" requirement.

Code:
cat file1.txt      
name1
name2
name3
name5
 
cat defaultfile.txt
name1
name2
name3
name4

Code:
comm -12 file1.txt defaultfile.txt | xargs -Ifn echo fn "authenticated"    
 
name1 authenticated
name2 authenticated
name3 authenticated

Code:
comm -23 file1.txt defaultfile.txt | xargs -Ifn echo fn "not authenticated"
 
name5 not authenticated

Code:
comm -13 file1.txt defaultfile.txt | xargs -Ifn echo fn "whateveryouwant"
 
name4 whateveryouwant

# 6  
Old 09-05-2012
Quote:
Originally Posted by draghun9
Thanks corona but Is there anyway i can do the same without the awk command.
Why must awk be avoided? I tried using comm, but that a) needs to be sorted, and b) ended up wanting awk anyway to test the properties of the output...
# 7  
Old 09-06-2012
Thanks guys for your reply...i see both codes working for me.....@corona..i am a newbie and heard from someone that using awk command doesnt workout right sometimes when we have while and for loops...but for me as of now it works fine without any problem...Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Comparing two files and list the difference with common first line content of both files

I have two file as given below which shows the ACL permissions of each file. I need to compare the source file with target file and list down the difference as specified below in required output. Can someone help me on this ? Source File ************* # file: /local/test_1 # owner: own #... (4 Replies)
Discussion started by: sarathy_a35
4 Replies

2. 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

3. UNIX for Advanced & Expert Users

How to find duplicates contents in a files by comparing other files?

Hi Guys , we have one directory ...in that directory all files will be set on each day.. files must have header ,contents ,footer.. i wants to compare the header,contents,footer ..if its same means display an error message as 'files contents same' (7 Replies)
Discussion started by: Venkatesh1
7 Replies

4. 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

5. Shell Programming and Scripting

Comparing the matches in two files using awk when both files have their own field separators

I've two files with data like below: file1.txt: AAA,Apples,123 BBB,Bananas,124 CCC,Carrot,125 file2.txt: Store1|AAA|123|11 Store2|BBB|124|23 Store3|CCC|125|57 Store4|DDD|126|38 So,the field separator in file1.txt is a comma and in file2.txt,it is | Now,the output should be... (2 Replies)
Discussion started by: asyed
2 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. UNIX for Dummies Questions & Answers

echo "ABC" > file1.txt file2.txt file3.txt

Hi Guru's, I need to create 3 files with the contents "ABC" using single command. Iam using: echo "ABC" > file1.txt file2.txt file3.txt the above command is not working. pls help me... With Regards / Ganapati (4 Replies)
Discussion started by: ganapati
4 Replies

10. Shell Programming and Scripting

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... (5 Replies)
Discussion started by: jisha
5 Replies
Login or Register to Ask a Question