How to delete text file some perticular lines using Matlab script

2 views (last 30 days)
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

Answers (1)

Mathieu NOE
Mathieu NOE on 5 Dec 2022
Edited: Mathieu NOE on 5 Dec 2022
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 Comments
Mathieu NOE
Mathieu NOE on 2 Jan 2023
Edited: Mathieu NOE on 2 Jan 2023
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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!