Editing/Changing a text file's content based on input variable

2 ビュー (過去 30 日間)
Luffy
Luffy 2015 年 10 月 13 日
コメント済み: Stephen23 2015 年 10 月 16 日
I have a txt file which has the following content in the middle of the text:
set TC_SYS_01 0
set TC_SYS_02 0
...
set TC_SYS_38 0
Now if input is 10,the first 10 lines value has to be set to 1. example:=
set TC_SYS_01 1
...
set TC_SYS_10 1
----------------------------------- Updated Code------------------------------
% if input says 10
content = fileread('run.txt');
newcontent = regexprep(content, '(?<=set TC_SYS_\d*\s+)[01]','0');
% the above line is just to make sure all of them are 0,if any of them are 1;
fid = fopen('run.txt','w');
fwrite(fid,newcontent);
fclose(fid);
% now all lines are value is 0.
% Variable is 10,so first 10 to be set to 1
content = fileread('run.txt');
%%%%%%%%%%%%%%%%%
newcontent = regexprep(content, '(?<=set TC_SYS_[0]\d\s+)[01]','1'); %this line
%%%%%%%%%%%%%%%%%
fid = fopen('run.txt','w');
fwrite(fid,newcontent);
fclose(fid);
how to write the line with comment 'this line' so that i can directly write the variable into expression instead of hard coding as i have done in the above code ?

採用された回答

Thorsten
Thorsten 2015 年 10 月 14 日
編集済み: Thorsten 2015 年 10 月 14 日
The idea is that you run the regexpr N times using the 'once' parameter. And you don't need so save the file intermediately after you have converted all the numbers to 0.
function changeTC_SYS(filename, N)
% Sets <num> to 1 in the first N lines of format "set TC_SYS_10 <num>".
content = fileread(filename);
content = regexprep(content, '(?<=set TC_SYS_\d*\s+)[01]','0');
% the above line is just to make sure all of them are 0,if any of them are 1;
for i = 1:N
content = regexprep(content, '(?<=set TC_SYS_\d*\s+)0', '1', 'once');
end
fid = fopen(filename,'w');
fwrite(fid,content);
fclose(fid);

その他の回答 (1 件)

Stephen23
Stephen23 2015 年 10 月 14 日
編集済み: Stephen23 2015 年 10 月 14 日
Try this code:
N = 5; % <- how many line to change to '1'.
S = fileread('run.txt');
S = regexprep(S, '1\s*?$','0','lineanchors');
for k = 1:N
S = regexprep(S, '0\s*?$','1','lineanchors','once');
end
fid = fopen('new.txt','w');
fwrite(fid,S);
fclose(fid);
Which creates a text file new.txt that looks like this:
set TC_SYS_01 1
set TC_SYS_02 1
set TC_SYS_03 1
set TC_SYS_04 1
set TC_SYS_05 1
set TC_SYS_06 0
set TC_SYS_07 0
set TC_SYS_08 0
set TC_SYS_09 0
set TC_SYS_10 0
... etc
  3 件のコメント
Luffy
Luffy 2015 年 10 月 14 日
@ Thorsten: Yes the above code was changing 6 other similar lines which were above the set TC_SYS_01 line. After running the above code,we can set the lines which got changed back to 0.
Stephen23
Stephen23 2015 年 10 月 16 日
"This works only if there are no other lines than.." which is exactly what was specified in the question. If there are other lines in the file then I am sure that the OP would know this and realize that this important information should be described in their question.

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

カテゴリ

Help Center および File ExchangeText Data Preparation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by