delete lines in txt based off phrase

1 次查看(过去 30 天)
I would like to delete sections of a txt file based off a phrase. so i want to find NaN as seen bewlow then delete anythign around that phrase to the { then below to the }, . so from origonal file to desired file. my code is attached. i can manage to delete NaN but nothing else.
origonal
{
Name = PTC_FAILURE_CRITERION_TYPE
Type = String
Default = 'NONE'
Access = Locked
},
{
Name = PTC_TENSILE_YIELD_STRESS
Type = Real
Default = NaN psi
Access = Full
},
{
Name = PTC_TENSILE_ULTIMATE_STRESS
Type = Real
Default = NaN psi
Access = Full
},
{
Name = PTC_MASS_DENSITY
Type = Real
Default = 1.000000 lbm/in^3
Access = Full
},
{
Name = MATL_TYPE
Type = String
Default = 'UNASSIGNED MATERIAL'
Access = Full
},
desired
{
Name = PTC_FAILURE_CRITERION_TYPE
Type = String
Default = 'NONE'
Access = Locked
},
{
Name = PTC_MASS_DENSITY
Type = Real
Default = 1.000000 lbm/in^3
Access = Full
},
{
Name = MATL_TYPE
Type = String
Default = 'UNASSIGNED MATERIAL'
Access = Full
},
%working
fid = fopen('trial.txt', 'rt') % Open source file.
g= fscanf(fid,'%c');
% gc=convertCharsToStrings (g)
missing =strfind(g,'NaN')
numg= strlength(g);
fid2 = fopen('trial.mtl', 'w+');
for i = 1:1
if isempty(missing)
full=g;
fprintf(fid2,'%s', g);
elseif missing>=true %
%delet NaN
expression = '.NaN';
replace = '';
newStr = regexprep(g,expression,replace);
%delet area
%these are my tries (deosnt work)
%expression2 = 'Name = PTC_POISSON_RATIO' %([\n\r]+\ Type = Real[\n\r]+) Default =
% expression3= 'Access ='
%expression2 = ' </?{}.*?>'
% replace2 = 'blank';
% newStr1 = regexprep(newStr,{expression2, expression3}, {replace2, replace2})
%expression = 'Default(\w+)=';
%newStr1= strrep(newStr,expression2,replace2);
% replace = '';
% newStr1 = regexprep(newStr,expression,replace)
end
finalword=newStr1;
end
fprintf(fid2,'%s', finalword) ;
fclose(fid2);
fclose(fid);
  3 个评论
dpb
dpb 2021-12-1
See Answer I posted probably while you were posting this... :)

请先登录,再进行评论。

回答(1 个)

dpb
dpb 2021-12-1
编辑:dpb 2021-12-2
txt=textread('bogar.txt','%s','whitespace','','delimiter',newline); % read file a cellstr array
idxHdr=find(contains(txt,'Name'))-1; % find the section headers
idxTrlr=find(contains(txt,'},')); % and the closing trailers
idxNaN=find(contains(txt,'NaN')); % then offending NaN locations
ixDel=flip(interp1(idxHdr,1:numel(idxHdr),idxNaN,'previous')); % find sections containing NaN
for i=ixDel(:).' % iterate over sections
txt(idxHdr(i):idxTrlr(i))=[]; % remove between header/trailer
end
Applied to your example file, the above produces:
>> txt
txt =
18×1 cell array
{'{' }
{' Name = PTC_FAILURE_CRITERION_TYPE'}
{' Type = String' }
{' Default = 'NONE'' }
{' Access = Locked' }
{' },' }
{'{' }
{' Name = PTC_MASS_DENSITY' }
{' Type = Real' }
{' Default = 1.000000 lbm/in^3' }
{' Access = Full' }
{' },' }
{'{' }
{' Name = MATL_TYPE' }
{' Type = String' }
{' Default = 'UNASSIGNED MATERIAL'' }
{' Access = Full' }
{' },' }
>>
rewrite to a file... writecell would be good for that.
  1 个评论
dpb
dpb 2021-12-1
idxHdr=find(contains(txt,'Name'))-1; % find the section headers
Actually, I notice that the file doesn't have embedded "{}" blocks so one could use
idxHdr=find(contains(txt,'{')); % find the section headers
instead and eliminate the offset from the 'Name' record. It's a nit, but...

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by