How to insert a new line before a character in file?

Hi, I have a file .txt format:
$text-text-text-text-text-text$text-text-text-text-text-text-text$text-text-text-textc....
I want to convert:
$text-text-text-text-text-text
$text-text-text-text-text-text-text
$text-text-text-text
$text-text-text-text
....
And don't have new line at beginning of text.
Thank you

Respuestas (2)

Jan
Jan el 26 de Dic. de 2022
Editada: Jan el 26 de Dic. de 2022
What is the wanted output? A cell string? A string? Another file?
% Perhaps: str = fileread('yourFile.txt');
str = '$text-text-text-text-text-text$text-text-text-text-text-text-text$text-text-text-text';
str = strrep(str, '$', [newline, '$']);
str(1) = [];
And to write this to a file:
writelines(str, 'outFile.txt');
str = '$text-text-text-text-text-text$text-text-text-text-text-text-text$text-text-text-textc'
str = '$text-text-text-text-text-text$text-text-text-text-text-text-text$text-text-text-textc'
s = strsplit(str,'$') ;
s'
ans = 4×1 cell array
{0×0 char } {'text-text-text-text-text-text' } {'text-text-text-text-text-text-text'} {'text-text-text-textc' }

3 comentarios

Thank you. But how can i edit in a file and keep character "$"
KSSV
KSSV el 26 de Dic. de 2022
Editada: KSSV el 26 de Dic. de 2022
You can read the file into a string using textscan. To each line you can append $.
str = '$text-text-text-text-text-text$text-text-text-text-text-text-text$text-text-text-text' ;
s = strsplit(str,'$')' ;
s(1) = [] ;
for i = 1:length(s)
s{i} = ['$',s{i}] ;
end
s
s = 3×1 cell array
{'$text-text-text-text-text-text' } {'$text-text-text-text-text-text-text'} {'$text-text-text-text' }
You can omit the loop:
for i = 1:length(s)
s{i} = ['$',s{i}] ;
end
using:
s = strcat('$', s)

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 26 de Dic. de 2022

Editada:

Jan
el 26 de Dic. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by