Find a specific word in a textfile and insert 2 new lines in front of it

1 回表示 (過去 30 日間)
Sam
Sam 2020 年 8 月 6 日
コメント済み: Rik 2020 年 8 月 6 日
Hello,
I've got a very basic question which I can't seem to figure out. I've got a textfile in which I want to find the word 'Observation_3'. Then I want to insert 2 new lines in front of the word 'Observation_3'. Can anyone help?
Thank you.
  4 件のコメント
Sam
Sam 2020 年 8 月 6 日
Thank you!
Now I have the location in my file where I want to insert 2 new lines... How can I do this? I know how to use fopen and fwrite, but I can't seem to figure it out...
filetext = fileread('test2.txt');
expr = '\w*Observation_3\w*';
[startIndex,endIndex] = regexp(filetext,expr,'match');
%Insert new line at position endIndex - 14;
...
Rik
Rik 2020 年 8 月 6 日
insterttext=sprintf('%s\n%\s\n','hello','world');
newfiletext = [filetext(1:(endIndex-14)) insterttext filetext((endIndex-13):end)];

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeLow-Level File I/O についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by