I want to write data at the end of every nth line of a text file

7 次查看(过去 30 天)
I have a long text file which contains several thousands of the following type of data
a0 =
333222
323312
310132
a1=
33222
323312
310132
a2=
133222
323312
310132
a3=
233222
323312
310132
a4=
303222
323312
310132
a5=
313222
323312
310132
a6=
323222
323312
310132
:
:
a2776=...
And I want to add at the end of evry 4th line a letter c and save back that modified file in txt format. After modification it should look like that
a0 =
333222
323312
310132c
a1=
33222
323312
310132c
a2=
133222
323312
310132c
a3=
233222
323312
310132c
a4=
303222
323312
310132c
a5=
313222
323312
310132c
a6=
323222
323312
310132c
:
:
:
a2776=...

采纳的回答

Voss
Voss 2024-7-21
str = readlines(filename);
str(4:4:end) = str(4:4:end) + "c";
writelines(str,filename)

where "filename" is the absolute or relative path to your text file, e.g., filename = 'C:\work\files\my_file.txt'.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by