How to merge text files vertically?
7 views (last 30 days)
Show older comments
Hello,
How to merge vertically multiple .txt files with the names:
results1.txt, results2.txt,...., results1000.txt?
I want ti merge them to one file.
JHow could I make it?
1 Comment
Stephen23
on 12 Jun 2020
Edited: Stephen23
on 12 Jun 2020
Note that KSSV's answer will not process the files in alphanumeric order as you probably expect, it will concatenate them in the order that the names are returned by the OS, most likely this order:
results1.txt
results10.txt
results100.txt
results1000.txt
results101.txt
results102.txt
results103.txt
results104.txt
results105.txt
results106.txt
results107.txt
results108.txt
results109.txt
results11.txt
results110.txt
... etc.
results988.txt
results989.txt
results99.txt
results990.txt
results991.txt
results992.txt
results993.txt
results994.txt
results995.txt
results996.txt
results997.txt
results998.txt
results999.txt
which means that your data will be concatenated together in an order that is unlikely to make much sense.
If you want the file data concatenated in an order that follows the number in the filename then you will have to sort the filenames yourself, e.g. downloading and using my FEX submission natsortfiles:
Answers (2)
KSSV
on 1 Jun 2020
txtFiles = dir('*.txt') ; % get the text files in the present folder
N = length(txtFiles) ; % Total number of text files
iwant = cell(N,1) ; % initlaize the data required
% loop for each file
for i = 1:N
thisFile = txtFiles(i).name ;
iwant{i} = importdata(thisFile) ; % read data of the text file
end
iwant = cell2mat(iwant) ;
11 Comments
Walter Roberson
on 12 Jun 2020
That file is odd. It has text numbers. It has what appears to be tab separated fields. It has stretches of binary 0 ("NUL") characters.
It appears that the binary 0 is sometimes put in place of missing data. However there are some places where there is only one binary 0 byte, and there are other places where there are 5 in a row. Without more information we cannot guess whether there is any significance to that.
Walter Roberson
on 1 Jun 2020
projectdir = 'directory files are in';
outdir = 'directory files are to be written to'; %should not be same as projectdir
dinfo = dir(fullfile(projectdir, '*.txt')) ; % get the text files in the present folder
filenames = fullfile(projectdir, {dinfo.name});
N = length(filenames);
Nok = 0;
for K = 1:N
inFile = filenames{K};
[~, basename, ext] = fileparts(inFile);
outfile = fullfile(outdir, [basename ext]);
S = fileread(thisFile);
S = regexprep(S, ',', ' ');
[fid, msg] = fopen(outfile, 'w');
if fid < 0
fprintf('Failed to open output file "%s" because "%s"', inFile, msg);
continue;
end
fwrite(fid, S);
fclose(fid);
Nok = Nok + 1;
end
fprintf('%d of %d files written to "%s"\n', Nok, N, outdir);
It is recommended that outdir not be the same as the source directory. If they are the same, then if something goes wrong with the writing of the replacement information, some or all of the file contents could be lost.
Reliable updating of a file "in-place" is tricky, especially if network file systems are involved; see https://www.mathworks.com/matlabcentral/answers/321087-saving-file-over-ssh-not-working#answer_252188
0 Comments
See Also
Categories
Find more on Large Files and Big Data 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!