1752790 Members
6038 Online
108789 Solutions
New Discussion

Re: Compare file

 
Intothenewworld
Advisor

Compare file

I have two files as below , I would like to compare the content

file 1
======
aaa.txt
111
222
bbb.txt
333
ccc.txt
ddd.txt
eee.txt

file 2
======
aaa.txt
444
bbb.txt
555
ccc.txt
ddd.txt
eee.txt

Result
======
aaa.txt
111
222
aaa.txt
444
bbb.txt
333
bbb.txt
555
ccc.txt
ccc.txt
ddd.txt
ddd.txt
eee.txt
eee.txt

Some content is the same ( eg. aaa.txt , bbb.txt , ccc.txt , ddd.txt , eee.txt ) , I would like to list its content by a condition to a file as the above result file - if the line is the same then output its next line(s) to the result file ,

can advise what can i do ?

thanks.
     

2 REPLIES 2
Hein van den Heuvel
Honored Contributor

Re: Compare file

There is not really enough information for a good solution

0) Always just 2 input files, or more?

1) what pattern can be used to recognize 'some content is the same' 

in the example: ".txt$". But what about for real? line starts with special character or word? NOT a number? Extra long, extra short?

2) is each match line garantueed to be present?

If not, How to sync back up on missing match line? 

3) working with megabytes of data or gigabytes?

When working with megabytes all data can be kept in memory,

When working with gigabytes the solution with need to read the two files in near lock-step

 

Here is a solution using memory....

> variable k is the 'current key' based on matching the 'same content trigger' 

> array x gets an entry per key, concatenating the current line and a new-line char for each line in all input

> array y gets an entry per unique key, ordered by number of arrival to keep printed in order of input.

> at the end for each entry in Y print the content of X, which is all lines for a given 'section'/key.

 

$ awk '/.txt$/{k=$0; if (!( k in x)) y[i++]=k } {x[k] = x[k] $0 "\n"} END{for (j=0;j<i;j++) print x[y[j]]} ' file1 file2
aaa.txt
111
222
aaa.txt
444

bbb.txt
333
bbb.txt
555

ccc.txt
ccc.txt

ddd.txt
ddd.txt

eee.txt
eee.txt

 

 Good luck!

Hein.

 

Dennis Handly
Acclaimed Contributor

Re: Compare file

>I would like to list its content by a condition to a file as the above result file -

 

It appears you are trying to merge the two files?  Where the content in file 2 is added to the end of the same section in file 1?

Are the two files sorted by their *.txt entries?