필터 지우기
필터 지우기

Find string in txt and delete searched string line

조회 수: 4 (최근 30일)
Bloodyrhyme
Bloodyrhyme 2019년 4월 26일
댓글: Bob Thompson 2019년 4월 26일
I have a text file.
Example
Bloody, 123456, Rhyme
Fine,6542445,Fate
Faith,9875415,Empty
Faith,45152123,New
I want to search for 'Faith' and i want to delete all Faith lines or i want to search for 'empty' and i want to delete all empty lines.
this is good but its delete all info until searched string, not searched string line.

채택된 답변

Bob Thompson
Bob Thompson 2019년 4월 26일
You can use Jan's method from the linked question to find your lines. Then once you have found them turn them into blank lines (I'm assuming that's also what you mean by 'emtpy'). Once you have found all of the lines, using logic indexing to remove all blanks.
Index = find(strcmp(CStr, SearchedString), 1); % Find lines with 'Faith'
CStr(Index) = []; % Blank found lines
CStr = ~isempty(CStr); % Remove any empty lines, may need ~isempty([CStr(:)]);
  댓글 수: 2
Bloodyrhyme
Bloodyrhyme 2019년 4월 26일
편집: Bloodyrhyme 2019년 4월 26일
Empty is a only word for example. Not situation for txt. i want to find 'empty' as word and i want to delete that line. my mistakes. i tell wrong i guess.
Bob Thompson
Bob Thompson 2019년 4월 26일
That's fine. The 'simplest' way to look for multiple words is to run the search multiple times, once for each word, and blank out the respective lines.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by