• Post Reply Bookmark Topic Watch Topic
  • New Topic

Compare two text files and replace the content of one file if the content is matched  RSS feed

 
VijjuAni Reddy
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am having two files with the contents as below.

File 1 has below text: It has two fields GUID and Numeric Long separated by space.

0000334b-456c-43c6-8bbc-0646d812cd6e 187370527
000052d6-93fa-441e-aa7b-a60c4e89e216 182309392
c86b1537-b2a2-4f3b-91a1-11297ca41638 171322512
74a58e5b-4c68-4080-9b14-5ee41077596a 117893365
024fd966-07bf-4987-91f2-a3396fab9e73 182769240
000074f5-b922-45dc-8bd5-71c59c61db07 180424080
000090b8-8f5e-41c1-a37f-e8a7b65097e7 124692533
0000ab04-fe0a-44c7-8bdd-521a824cde59 210584325
0000e5a2-0e77-445e-9f83-264b92947dde 182877504

File 2 has below text:

0000334b-456c-43c6-8bbc-0646d812cd6e,0.0000,0,0,3,0,TRUE
000052d6-93fa-441e-aa7b-a60c4e89e216,0.0000,0,0,3,0,TRUE
c86b1537-b2a2-4f3b-91a1-11297ca41638,1.0000,0,2955,1,0,TRUE
74a58e5b-4c68-4080-9b14-5ee41077596a,1.0000,0,3339,1,0,TRUE
024fd966-07bf-4987-91f2-a3396fab9e73,0.5000,0,154,1,0,TRUE
000074f5-b922-45dc-8bd5-71c59c61db07,0.0000,0,0,3,0,TRUE
000090b8-8f5e-41c1-a37f-e8a7b65097e7,0.0000,0,0,3,0,TRUE

We need to compare the file 1 to file 2 line by line and if we find the GUID of file 1 in file 2 then replace the GUID with the Numeric Long which is present beside to the GUID.

Output should be like below: Either this can be in new text file or even fine if it updates the file 2.

187370527,0.0000,0,0,3,0,TRUE
182309392,0.0000,0,0,3,0,TRUE
171322512,1.0000,0,2955,1,0,TRUE
117893365,1.0000,0,3339,1,0,TRUE
182769240,0.5000,0,154,1,0,TRUE
180424080,0.0000,0,0,3,0,TRUE
124692533,0.0000,0,0,3,0,TRUE

Please help me here. I am in need of this desperately.

Thanks in advance.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the 1st file. Split each line into two strings. Add them to a Map<String,String> where the GUID is the key and the replacement is the value.

Read the 2nd file. Split each line delimited by a comma. Look up the 1st string in the split in the map you built above. IF found, THEN replace with the map's value.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
VijjuAni Reddy wrote:Either this can be in new text file or even fine if it updates the file 2.

Usually it isn't a good idea to modify original file as things go wrong, and when they do - you lose file. Luckily if you have a luxury to retrieve file again from source where you got it, but I'd make sure you can first.

Do your processing, whenever you finish and are sure the new file got created with replaced content - you can delete original in case you don't need, but delete only when you are sure newly created file is indeed what you need - I'd still keep backup file for a week or so and then would have some automated tool to remove them after some time.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing. Does the file encoding matters? In case does, check manually their encoding (you can do so by opening it with a Notepad++, showing at the bottom right corner) - that might matters for some further processing, so you'd need to be sure the newly created file encoding matches those you took information from.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Third thing. Are both files coming from the same source or different? In case latter, might operating systems are different, in case of that, line breaks would differ - so you need to think about those if they matter to you and your further processing.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I would have thought that you will be all right if the files both use the whole encoding. Don't all encodings look the same if the value of every char in the file is < 0x0080?
Line ends will be different; the new file will have the encoding appropriate to the current OS unless you specify \r\n or similar. Not sure how you can identify the line end sequences in the source files except by that dreadful method read() and searching for what the Pattern documentation calls a line terminator. Once you have found a few line terminator sequences, you can stop using read().

If you are on a *nix system, you can probably use Runtime.exec() and call the diff program. Be sure to read Michael Daconta's classic article called When Runtime.exec() won't before going anywhere near Runtime.exec. There is probably an analogue of diff on Windows® too. The problem you are describing probably isn't suitable for diff. I think CB's solution is better. Use a Scanner on the second file and repeatedly read nextLine and create an object to encapsulate the results, and as CB says, put those objects into a Map. The long field in that object can be altered from 0L to whatever you have in the first file, using a Scanner alternating next() and nextLong().
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!