Replace text in a .txt file from another .txt file [closed]
I want to automate a process that I do manually right now. I currently compare two .txt documents (we will call them "D1" and "D2") and create a third (call it "D3") that is a combination of the other two. What I need to do is find a certain string in "D1" and find a similar/same string in "D2". Once there is a match, I need to replace the string in "D1" with the following string that matched "D2" and put it in "D3" as a master file. This sounds a bit confusing so here is what I would like it to look like:
The .txt file format for "D1" is as follows:
1 0010 12345 DEF, DEF-0320
1 0020 ABC-00010G ABC-A,1xx,10%,x1x,0603
1 0020A ABC开发者_JAVA技巧-00010G ABC-A,1xx,10%,x1x,0603
1 0030A ABC-00127G ABC,4.7xx,10%,x1x,0805
The .txt file format for "D2" is as follows:
10 BARE PCB
20 T C40, C3112
B C5, C45, C48
30 B C25
I want to compare both "D1" and "D2" and find the values '0010', '0020', '0020A', '0030A' from "D1" and the values '10', '20', '30' from "D2". If (when) there is a match between the two sets of numbers (ie, '0010' and '10') I would like to format a new document, "D3", as follows:
1 AAAA BCD 142717 DEF, DEF-0320 T
1 C40 ABC-00010G ABC-A,1xx,10%,x1x,0603 T
1 C3112 ABC-00010G ABC-A,1xx,10%,x1x,0603 T
1 C5 ABC-00010G ABC-A,1xx,20%,x1x,0603 B
1 C45 ABC-00010G ABC-A,1xx,20%,x1x,0603 B
1 C48 ABC-00010G ABC-A,1xx,20%,x1x,0603 B
1 C25 ABC-00127G ABC,4.7xx,100%,x1x,0805 B
The 'T/B' is assigned based on whether or not there is an 'A' following the second number in "D1". 'T' is assigned if there is just a number, 'B' is assigned if there is an 'A'. Also, there are more lines in the "D3" .txt document because of the ',' seperating the different values (ie, for '20', there is 'C40' and 'C3112'). For every ',' in "D2" there needs to be a new line in "D3" that has the same following format as the line above it.
Colton wrote
So my question is, what is the best way to go about reading "D1" and "D2" and comparing the two for similar strings and than proceeding to replace the text in "D1"?
Here's a way (there is no best way, at least not one where everyone agrees with one another):
- read both files line by line [?]
- split each line on their white spaces [?]
- for both files, create a 2 dimensional array [?]
- compare the appropriate columns from your 2 dimensional arrays to each other and check if one contains the other [?]
- write something back to a 3rd file [?]
精彩评论