필터 지우기
필터 지우기

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

조회 수: 19 (최근 30일)
Radu Mihail
Radu Mihail 2024년 7월 21일 14:10
댓글: Voss 2024년 7월 21일 16:14
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일 14:57
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에서 Standard File Formats에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by