Borrar filtros
Borrar filtros

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

19 visualizaciones (últimos 30 días)
Radu Mihail
Radu Mihail el 21 de Jul. de 2024 a las 14:10
Comentada: Voss el 21 de Jul. de 2024 a las 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=...

Respuesta aceptada

Voss
Voss el 21 de Jul. de 2024 a las 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'.

Más respuestas (0)

Categorías

Más información sobre Standard File Formats en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by