delete lines in txt based off phrase
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
dpb
le 1 Déc 2021
Modifié(e) : dpb
le 2 Déc 2021
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 commentaire
dpb
le 1 Déc 2021
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...
Voir également
Catégories
En savoir plus sur Characters and Strings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!