text file comparison


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting text file comparison
# 1  
Old 10-12-2011
text file comparison

Hi All,

I would like to write script which will compare two text files, however the order of the content in the files might change,

for eg,

File 1 File 2
------- --------
ABC ABC
DEF GHI
GHI DEF

The output should be files are same. So i cannot try with the simple diff command.

One way i can think of is read line by line and put it in two temp tables and read them by order by and compare.
Or loop each line in one file with other in the different file, but if one line matches with more entries in other file this might give wrong result..

Is there any other smart ways to do in shell scripting for this?

Regards
Kalpana
# 2  
Old 10-12-2011
How about using sort on each of the files before doing diff?

Or try this awk program:
Code:
awk '
    NR==FNR{ A[$0]++; next}
    {B[$0]++}
    END {
        for(L in A) {
            if(!(L in B)||A[L]!=B[L]) { print "different"; exit}
            delete B[L];
        }
        if(length(B)) print "different" 
        else print "same"
    }' file1 file2


Last edited by Chubler_XL; 10-13-2011 at 12:00 AM..
# 3  
Old 10-12-2011
Code:
sort file1 file2 | uniq -c | awk '$1~/2/ {print $2}'

# 4  
Old 10-13-2011
Code:
diff <(sort file1) <(sort file2)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. UNIX for Beginners Questions & Answers

Text file parsing and comparison

I have two files (first.txt and second.txt): more first.txt cat mammal lizard reptile Elephant mammal ant Insecta more second.txt ant termite ant army_ant (9 Replies)
Discussion started by: cs_novice
9 Replies

3. Shell Programming and Scripting

File Comparison

HI, I have two files and contains many Fields with | (pipe) delimitor, wanted to compare both the files and get only unmatched perticular fields. this i wanted to use in shell scriting. ex: first.txt 111 |abc| 230| hbc231 |bbb |210 |bbd405 |ghc |555 |cgv second.txt 111 |abc |230 |hbc231... (1 Reply)
Discussion started by: prawinmca
1 Replies

4. Shell Programming and Scripting

File Comparison: Print Lines not present in another file

Hi, I have fileA.txt like this. B01B02 D0011718 B01B03 D0012540 B01B04 D0006145 B01B05 D0004815 B01B06 D0012069 B01B07 D0004064 B01B08 D0011988 B01B09 D0012071 B01B10 D0005596 B01B11 D0011351 B01B12 D0004814 B01C01 D0011804 I want to compare this against another file (fileB.txt)... (3 Replies)
Discussion started by: genehunter
3 Replies

5. Shell Programming and Scripting

Help with file comparison

Hello, I am trying to compare 2 files and get only the new lines as output. Note that new lines can be anywhere in the file and not necessarily at the bottom of the file. I have made the following progress so far. /home/aa>cat old.txt 0001 732 A 0002 732 C 0005 732 D... (7 Replies)
Discussion started by: cartrider
7 Replies

6. Shell Programming and Scripting

two file comparison

now i have a different file zoo.txt with content 123|zoo 234|natan 456|don and file rick.txt with contents 123|dog|pie|pep 123|tail|see|newt 456|som|sin|sim 234|pay|rat|cat i want to look for lines in file zoo.txt column1 that has same corresponding lines in column 1 of... (6 Replies)
Discussion started by: dealerso
6 Replies

7. Shell Programming and Scripting

File Comparison- Need help

I have two text files which have records of thousand rows. Each row is having around 40 columns. Each column is tab delimited. Each row is delimited by newline character. My requirement is to find for each row i need to find whether any column is different between the two files. For each row i... (8 Replies)
Discussion started by: uihnybgte
8 Replies

8. Shell Programming and Scripting

Complicated(?) text file comparison

I've got two files, both plain text. Each file is a datafeed of products, pipe delimited. The current file is in directory 1 and yesterday's file is in directory 2 (literally, those are the directory names). What I'm trying to do is compare the files and pull out products whose price has changed... (3 Replies)
Discussion started by: Daniel M. Clark
3 Replies

9. Shell Programming and Scripting

file comparison

hi I have 2 files to comapre ,in file a sible column it is numbers,in file b2 numbers and other values with coma separated. i want compare numbers in file a with file b,and the out put put should be in C with numbers in both file a and b along with other columns of file b. i used folowing... (7 Replies)
Discussion started by: satish.res
7 Replies

10. Shell Programming and Scripting

File Comparison

I have to compare two text files, very few of the lines in these files will have some difference in some column. The files size is in GB. Sample lines are as below: 11111122222222333333aaaaaaaaaabbbbbbbbbccccccccdddddd 11111122222222333333aaaaaaaaaabbbbbbbbbccccccccddeddd So assuming these... (19 Replies)
Discussion started by: net_shree
19 Replies
Login or Register to Ask a Question