How can I delete header from a .txt file and save it?

4 views (last 30 days)
I have a text file like this one:
element, unit, refTime, validTime, (62.000000,-6.250000)
HTSGW, [m], 197901010000, 197901010000, 0.000
HTSGW, [m], 197901010000, 197901010300, 0.220
HTSGW, [m], 197901010000, 197901010600, 0.420
HTSGW, [m], 197901010000, 197901010900, 0.560
HTSGW, [m], 197901010000, 197901011200, 2.190
I just want to remove the header and then save the file in the same .txt file without the header.
I may use the next script, shown in other forums, however I cannot save the generated list.
fid = fopen(Text);
tline = fgetl(fid);
tline = fgetl(fid);
while ischar(tline)
disp(tline)
tline = fgetl(fid);
end
fclose(fid);
I wish someone could help me. Thanks.
  4 Comments
José-Luis
José-Luis on 1 Jul 2016
Edited: José-Luis on 1 Jul 2016
I was mostly thinking that this is a one liner for sed and those utilities tend to be more efficient than what Matlab has to offer.
Guillaume
Guillaume on 1 Jul 2016
Ah yes, certainly it would be more efficient to do that outside of matlab with the OS tools (assuming there is one that does that, not sure about windows...).

Sign in to comment.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 1 Jul 2016
fid=fopen('file.txt')
s=textscan(fid,'%s%s%s%s%s','headerlines',1)
fclose(fid)
ss=[s{:}]
fid1=fopen('file.txt','w')
for k=1:size(ss,1)
fprintf(fid1,'%s%s%s%s%s\r\n',ss{k,:})
end
fclose(fid1)
  3 Comments
Javier Martinez
Javier Martinez on 1 Jul 2016
The only problem is that I am reading many .txt files, and your solution only lets me read a unique .txt file. As I am making a loop, I wish I could generate a unique .txt file with the different data. If you have any idea of that it would be great. I am extremely grateful for your help.
Guillaume
Guillaume on 1 Jul 2016
Why bother parsing each line into 5 different strings, thus slowing down the program. You actually don't care about the content of each line, so
s = textscan(fid, '%s', ...);
should be enough. Same for the fprintf call.

Sign in to comment.

More Answers (1)

Guillaume
Guillaume on 1 Jul 2016
Edited: Guillaume on 1 Jul 2016
Note: when using fopen always specify the permissions ('r' or 'w' mostly with optional 't' for text mode).
Your code only display the text. You also need to open a file for writing if you want to save it.
Probably the easiest way to do it all would be:
filecontent = fileread('somefile');
newcontent = regexp(filecontent, '(?<=[^\n\r].*[\n\r]+).*', 'match'); %keep everything but first line (defined as any number of characters but newline or carriage return followed by any number of newlines or carriage returns
fid = fopen('newfile', 'w');
fprintf(fid, '%s', newcontent);
fclose(fid);
  2 Comments
Javier Martinez
Javier Martinez on 1 Jul 2016
There is an error:
Error using fprintf Function is not defined for 'cell' inputs.
Guillaume
Guillaume on 1 Jul 2016
D'oh! either add , 'once' after the 'match' in the regexp call. or simply dereference the cell as it should only have one element:
fprintf(fid, '%s', newcontent{1});
Adding 'once' would be more correct.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!