Issue
I have a file with the following text, I want to make a line break, after the S.
character
i have this
1900-01-01 00:00:00|1|S|S|S|S.1900-01-01|S.1900-01-01 00:00:00|1|S|S|S|S.1900-01-01 00:00:00|1|S|S|S|S.
i want
1900-01-01 00:00:00|1|S|S|S|S.
1900-01-01|S.
1900-01-01 00:00:00|1|S|S|S|S.
1900-01-01 00:00:00|1|S|S|S|S.
i tried this
$ sed 's/S./\n/g' file.csv > file2.csv
Solution
With any sed that accepts \n
as meaning "newline":
$ sed 's/S\./&\n/g' file
1900-01-01 00:00:00|1|S|S|S|S.
1900-01-01|S.
1900-01-01 00:00:00|1|S|S|S|S.
1900-01-01 00:00:00|1|S|S|S|S.
or in bash for $'\n'
to get a newline char:
$ sed 's/S\./&\'$'\n''/g' file
1900-01-01 00:00:00|1|S|S|S|S.
1900-01-01|S.
1900-01-01 00:00:00|1|S|S|S|S.
1900-01-01 00:00:00|1|S|S|S|S.
or portably with any sed in any shell:
$ sed 's/S\./&\
/g' file
1900-01-01 00:00:00|1|S|S|S|S.
1900-01-01|S.
1900-01-01 00:00:00|1|S|S|S|S.
1900-01-01 00:00:00|1|S|S|S|S.
Note that the output will end in a blank line since the script adds a newline after every S.
as requested. If you actually only want to add a newline after every S.
mid-line then that'd be:
$ sed 's/\(S\.\)\(.\)/\1\n\2/g' file
1900-01-01 00:00:00|1|S|S|S|S.
1900-01-01|S.
1900-01-01 00:00:00|1|S|S|S|S.
1900-01-01 00:00:00|1|S|S|S|S.
Answered By - Ed Morton Answer Checked By - Gilberto Lyons (WPSolving Admin)