Effacer les filtres
Effacer les filtres

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

19 vues (au cours des 30 derniers jours)
Radu Mihail
Radu Mihail le 21 Juil 2024 à 14:10
Commenté : Voss le 21 Juil 2024 à 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=...

Réponse acceptée

Voss
Voss le 21 Juil 2024 à 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'.

  2 commentaires
Radu Mihail
Radu Mihail le 21 Juil 2024 à 15:53
Thank you Voss!
Voss
Voss le 21 Juil 2024 à 16:14
You're welcome!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Standard File Formats dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by