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

34 views (last 30 days)
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

Answers (2)

Jan
Jan on 26 Dec 2022
Edited: Jan on 26 Dec 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');

KSSV
KSSV on 26 Dec 2022
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 Comments
KSSV
KSSV on 26 Dec 2022
Edited: KSSV on 26 Dec 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' }
Jan
Jan on 26 Dec 2022
You can omit the loop:
for i = 1:length(s)
s{i} = ['$',s{i}] ;
end
using:
s = strcat('$', s)

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!