Find string in the special file

조회 수: 3 (최근 30일)
Farzad Fakhraeiroudsari
Farzad Fakhraeiroudsari 2020년 9월 7일
댓글: Rik 2020년 9월 8일
Hello Everyone.
I'm trying to export scenario names from a file that is not a txt file but I can see the names if I open it in the notepad. Here is a sample of the data in the file
O(1}(2019-12-4 12:12:43Szenario 7: OS - Haventransport - Copy has been deleted.>'1['2019-12-4 12:12:43Szenario 6: OS - Copy has been deleted.:&1S&2019-12-3 16:35:12Modifikation 10: has been created.N%/}%2019-12-3 11:21:2Szenario 8: AS - Haventransport - Copy has been created.N$/}$2019-12-3 11:21:2Szenario 7: OS - Haventransport - Copy has been created.="/["2019-12-3 11:21:2Szenario 5: AS - Copy has been created.N!/}!2019-12-3 11:20:7Szenario 5: AS - Haventransport - Copy has been deleted.9 1Q 2019-12-3 11:19:48Modifikation 9: has been created.O­1}­2019-12-3 11:17:59Szenario 5: AS - Haventransport - Copy has been created.:‑3Q‑2019-11-26 11:48:22Modifikation 8: has been created.91Q2019-11-26 9:26:42Modifikation 7: has been created.S12019-11-26 …..
The name is written after Szenario and end by (“has been created.”). for example the first one is Scenario 8 and the name is “AS - Haventransport – Copy”. I want to export the Scenario number and It’s name. since the file is not text and the sequence of characters is not consistent, I cannot use regexp or I can but I don’t know how.
I appreciate any suggestion or help.
Best regards
Farzad

답변 (2개)

Rik
Rik 2020년 9월 7일
If notepad can open it and you see plain text, why do conclude it is not a text file? Even if there are some special characters in there you can just ignore them.
I suggest you use fileread to read the file to a single long char array. You can then remove any invalid chars if you like, after which you can use regexp or any other method to find this pattern.
If you need help, attach an example file to your question (simply change the extension to txt or zip the file). You should also mention your release if you aren't using the latest release.
  댓글 수: 4
Rik
Rik 2020년 9월 7일
When I get back I'll try my sqlite3 function to read it. In the mean time you can have a go yourself.
Rik
Rik 2020년 9월 8일
Running the code below with my sqlite3 function (on Octave, because for some reason the interface refuses to work on Windows 8 + R2020a and I don't have time for thorough debugging now) returned a cell array:
fn='C:\Users\Rik\Desktop\R2_AS - Copy.db';
a=sqlite3(fn,'SELECT ENTRY FROM LOGENTRY');
data={a.ENTRY};

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


Jan
Jan 2020년 9월 7일
편집: Jan 2020년 9월 7일
S = fileread('R2_AS - Copy.txt');
i1 = [strfind(S, 'Szenario'), Inf];
i2 = strfind(S, 'has been created.');
width = numel('has been created.') - 1;
Result = cell(1, numel(i1));
iResult = 0;
for k = 1:numel(i1) - 1
ini = i1(k);
fin = i2(find(i2 > ini, 1));
if ~isempty(fin) && fin < i1(k + 1)
iResult = iResult + 1;
Result{iResult} = S(ini:fin + width);
end
end
Result = Result(1:iResult);
You will get some strange results as:
'Szenario 9: OS - uitbereden - Copy has been deleted.:/S —2020-2-7 12:26:34Modifikation 28: has been created.'
The results are not unique also for a specific Szenario number - some examples:
'Szenario 9: AS - Copy has been created.' }
'Szenario 9: OS - uitbereden - Copy has been deleted.:/S —2020-2-7 12:26:34Modifikation 28: has been created.' }
'Szenario 9: AS - Uitbereden R2 - Copy has been created.'
So what do you want to extract exactly?Where does the wanted "name" end?
  댓글 수: 1
Farzad Fakhraeiroudsari
Farzad Fakhraeiroudsari 2020년 9월 8일
Thanks Jan. This is from a traffic planning software and the file came from it's database where it's mentioned the user create or delete a scenario from the database. As you mentioned for scenario 9 several names were mentioned but I'm interested in the last occurance, AS - Uitbereden R2 - Copy for Scenario 9 for instance.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by