Extract part of the text file

1 Ansicht (letzte 30 Tage)
Thomas Luk
Thomas Luk am 14 Mär. 2012
Sorry if duplicated post.. but i don't know where I should put the question....
There is the situation... I am using ABAQUS to simulate some tests, and i have to edit the input file (normal text file) to incorporate switches that are not available in the GUI version of ABAQUS.
Two Question
Question 1:-
Is it possible to: search for two keywords, then select the lines between them and delete it?
the sample input file is as follow:
**
** STEP: Eigen
**
(SOME MORE LINES)
** FIELD OUTPUT: F-Output-1
**
*Output, field, variable=PRESELECT
*End Step
** ----------------------------------------------------------------
**
** STEP: Ax Load
**
the first keyword will be STEP: Eigen, and the second one is STEP: Ax Load. What i hope matlab can do is: search for STEP: Eigen, remember the line number search for STEP: Ax Load, remember the line number delete all the lines in between the two saved lines.
Question 2:- At the end of the input file, I need to add two lines below the keyword. The sample input file is as follow:
*Output, field, variable=PRESELECT
**
** HISTORY OUTPUT: H-Output-1
**
*Output, history, variable=PRESELECT
*End Step
The keywords will be: *Output, history, variable=PRESELECT and i want to add two lines, something like: *node file, global=yes U
I will be very thank to the person that can help me on this....
Thank You!!!

Akzeptierte Antwort

Geoff
Geoff am 15 Mär. 2012
I would do it as follows:
First, slurp the entire file into a cell array.
fileID = fopen('input.txt', 'r');
lines = {};
while 1
line = fgetl(fileID);
if line == -1 break;
lines{end+1} = line;
end
fclose(fileID);
Now search for your open and closing tags.
fnFindLineWithText = @(arr,txt) ...
find(cellfun(@(x) ~isempty(regexp(x,txt)), arr), 1);
openIdx = fnFindLineWithText(lines, 'STEP: Eigen');
closeIdx = fnFindLineWithText(lines, 'STEP: Ax Load');
if ~isempty(openIdx) && closeIdx > openIdx
% You have your lines.
end
I'll leave the rest as an exercise for you =)
-g-
  1 Kommentar
Thomas Luk
Thomas Luk am 19 Mär. 2012
Thank you so much, the "regexp" solves my problem

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by