matlab 或 C++提取文本含特定字符行数据

2025-03-28 08:58:57
推荐回答(2个)
回答1:

参考代码:

[f,p] = uigetfile('*.*','选择输入文件','runlog');
if f==0, return, end
file = [p f];
fi = fopen(file, 'rt');
if fi == -1
    error(['无法打开文件 ' file])
end
fo = fopen('reslut.dat', 'wt');
if fo == -1
    error(['无法打开文件 reslut.dat'])
end
fprintf(fo,'abc\ndef\n');
ptn = 'delete  number';
while ~feof(fi)
    s = fgetl(fi);
    inx = findstr(s, ptn);
    if isempty(inx), continue, end
    d = sscanf(s(inx(1)+length(ptn)+1:end),'%d');
    fprintf(fo,'create,%i\n',d);
end
fprintf(fo,'ghj\nqwe\n');
fclose(fi);
fclose(fo);

回答2:

搞得这么复杂。。。linux下面一条命令就搞定