How to delete text file some perticular lines using Matlab script

5 次查看(过去 30 天)
Hi I have one text file and in which if variable is not matching i need to delete that lines till end For example file is like this
FESTW Cbx_asdf_gh Xyz xyz Abs abs Ster sox END
GHRT Vbx_asdf_gh Xyz ans Anstg END
GBLOS xxx_asdf_gh Sstf Sgohe END
SO if the variable is not matching with defined variable in this case last one xxx then i need to delete that whole lines but i don't know how to delete lines till END because everytime END is ending at diff position like line no is not standard so how to delete that whole lines starting from GBLOS TO END (GBLOS xxx_asdf_gh Sstf Sgohe END)
So can you help me how to delete these lines it is having 40+ end like this and some variable is not matching so i need to delete all that till end
Thank you

回答(1 个)

Mathieu NOE
Mathieu NOE 2022-12-5
编辑:Mathieu NOE 2022-12-5
hello
its' a bit confusing.... in other words you want to keep the lines that contain a certain variable name ?
then try this :
ll = readlines('text.txt'); % read text and convert to strings
defined_variable = 'Anstg'; % if ll does not contain that variable name , delete lines where it's missing
for ci = 1:numel(ll)
tf = strfind(ll(ci),defined_variable);
if isempty(tf)
check(ci) = false;
else
check(ci) = true;
end
end
ll_out = ll(check)
  3 个评论
Mathieu NOE
Mathieu NOE 2023-1-2
编辑:Mathieu NOE 2023-1-2
hello
it's still not clear to me
other that you want to keep "Cbx line" i don't fully understand the rest.
If you could wirte down what is / are the key words to keep lines (or key words to delete lines) , it would make things more understandable to me

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Language Support 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by