Writing text by comparing two text files
Show older comments
Hello Matlab community
I need some quick reply (if possible)
I have a text file (attached as cod.txt)
The structure is like
36.41 128.95 36.447 128.415 14.1 151005 ADO2 0.2 50.07 22.35 16
0.19534 0.26198 -0.21566 -0.21479 0.0342 -0.01419 0.05419 0.23893 -0.19917 -0.36938 0.01861 -0.07808 -0.15301 0.22595 0.021 0.19407
There is a folder named "fold" which has many text files (attached as a zip file).
The structure of text files is
2017-01-10T151005.txt
The inside of these text files are as
2017 110 1510 6.4 L 36.447 128.415 14.1 TES 14 0.2 1
EUSB HZ EP 1510 11.76 116 0.2310 26.5 112
EUSB HZ ES 1510 15.43 116 0.2910 26.5 112
MGB0 HZ EP 1510 11.95 114 0.0210 29.2 324
MGB0 HZ ES 1510 15.95 114 0.1210 29.2 324
MGY0 EZ EP 1510 13.57 108 0.1010 39.1 306
MGY0 EZ ES 1510 18.54 108 0.0810 39.1 306
CIGB HZ EP 1510 14.31 105 -0.1310 45.3 184
CIGB HZ ES 1510 19.73 105 -0.4010 45.3 184
CPR2 EZ EP 1510 14.59 104 -0.1510 47.1 238
CPR2 EZ ES 1510 20.35 104 -0.2810 47.1 238
ADO2 EZ EP 1510 14.88 104 -0.0110 48.0 95
ADO2 EZ ES 1510 20.81 104 -0.0910 48.0 95
Now
I need to look three columns which are the same (I made it bold)
If they are same then write the id(excluding *.txt) in front of the line which matched as
36.41 128.95 36.447 128.415 14.1 151005 ADO2 0.2 50.07 22.35 16 2017-01-10T151005
Also need to add text value in the folder's text file (bold) as
36.41 128.95 36.447 128.415 14.1 151005 ADO2 0.2 50.07 22.35 16 2017-01-10T151005 1510 14.88 1510 20.81
so the output must be a new text file as
36.41 128.95 36.447 128.415 14.1 151005 ADO2 0.2 50.07 22.35 16 2017-01-10T151005 1510 14.88 1510 20.81
0.19534 0.26198 -0.21566 -0.21479 0.0342 -0.01419 0.05419 0.23893 -0.19917 -0.36938 0.01861 -0.07808 -0.15301 0.22595 0.021 0.19407
I hope to get a fast reply soon.
Regards and thanks in advance.
5 Comments
Image Analyst
on 24 Dec 2019
What columns are you looking at? In the first line,
2017 110 1510 6.4 L 36.447 128.415 14.1 TES 14 0.2
you have bolded columns 6, 7, and 8. Then you have skipped a bunch of lines. Then with the last two lines
ADO2 EZ EP 1510 14.88 104 -0.0110 48.0 95
ADO2 EZ ES 1510 20.81 104 -0.0910 48.0 95
You have bolded columns 1, 2, 3, 4, and 5. So I have no idea what columns you're looking at. It seems to vary depending on what line of the file you're examining.
And with cod.txt, are you taking the same characters out of cod.txt and inserting it into all of the other text files? So, for all other text files, insert columns 3,4, & 5 from cod.txt into somewhere?
And what about the other stuff in the other files:
EUSB HZ EP 1510 11.76 116 0.2310 26.5 112
EUSB HZ ES 1510 15.43 116 0.2910 26.5 112
What's to be done with that? Transfer those lines to the output file also?
Muhammad Zafar Iqbal
on 24 Dec 2019
Image Analyst
on 24 Dec 2019
Maybe it's just too late at night for me to delve into this and understand it, but I'm sure you could do it with strfind() and/or contains(), strsplit(), sprintf(), and fprintf(). If I were to do it, those are the functions I would use.
Muhammad Zafar Iqbal
on 24 Dec 2019
J. Alex Lee
on 24 Dec 2019
this looks like it is a file parsing problem as much as it is a data matching problem.
the files in the folder can probably be read easily with
t = readtable(filename,'HeaderLines',1)
or something. Here's a [crude?] first pass at parsing the cod.txt "index" file
% read the "index" file
fid = fopen('cod.txt','r');
cntr = 0;
while ~feof(fid)
cntr = cntr + 1;
% get first line of line pair
buf = fgetl(fid)
C = textscan(strtrim(buf),"%f %f %f %f %f %d %s %f %f %f %d")
hdr{cntr,1} = C;
% get second line of line pair
buf = fgetl(fid);
C = textscan(strtrim(buf),"%f",'Delimiter',' ');
data{cntr,1} = C{1}';
end
fclose(fid)
% optional
hdr = cell2table(vertcat(hdr{:}))
data = array2table(vertcat(data{:}))
Answers (0)
Categories
Find more on String Parsing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!