how to edit a content of a document to add \n after each ;

Hi:

I'm trying to write a script that reformats a given document (edit the content of a given document) , I want to add a new line \n after each ; in the document.

Regards

Look into: dos2unix (maybe called dos2ux on your box)

man dos2unix

turns windows text file into unix text file.

This also assumes you don't want a newline at the end of every line, else the above post is your answer. You didn't say if every line ended in a ';'.

sed 's/;/;<Ctrl-v><enter>/g' filename > filename.out

One way is to use the stream editor sed. Search for a ';' and replace it with a ';' followed by a <newline> on every occurrence on a line. Note pressing Ctrl-V allows one to enter a control character.

Good Luck,
Gary

1 Like

I've tried the script below

sed 's/;/;\\n/' testing.txt > testing2.txt

but that didn't work, the new line wasn't added after each ; in the output file (testing2.txt)

---------- Post updated at 05:46 PM ---------- Previous update was at 05:26 PM ----------

This worked perfectly :b:
Thank you.

As you have found out, a '\n' represents a newline when used in the echo or print commands, depending on what shell you are using.

Regular expressions for pattern matching are another story.

Blue Skies,

Gary

The sed command above would replace only the first occurrence of ; on a line -- to replace ALL semicolons, you need to specify global replacement:

sed 's/;/;\\n/g' 

That will replace every instance of a semicolon with a semicolon, backslash, and "n".

The portable way of including a newline in the replacement text requires preceding an actual newline character with a backslash (posix does not recognize \n in the replacement text).

sed 's/;/;\
/g'

Regards,
Alister