1

How can I replace all sequences of a dot followed by a space with a dot followed by a new line? On a unix box, with POSIX tools.

I thought sed or tr might be up for the job. But I can not figure it out. I tried things like

sed 's/. /.\n/g' file

Reading How can I replace a newline (\n) using sed? did not make it happen for me.

EDIT: I was lost between the different escapes and forgot that I should escape the dot. so sed 's/\. /\.\n/g' file works for me.

Jona Engel
  • 369
  • 1
  • 13

3 Answers3

1

With awk you could easily do it by using its gsub function. This should work in any version of awk IMHO. You could simply globally substitute literal DOT followed by space with dot and ORS(which is new line) and then simply print the line.

awk '{gsub(/\. /,"."ORS)} 1' Input_file
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
1

Not all sed will treat \n as a newline character in the replacement string. But you can escape the newline.
eg:

$ echo foo. bar | sed 's/\. /.\
> /'
foo.
bar

or

$ n='                               
'
$ echo foo. bar | sed "s/\. /.\\$n/"
foo.
bar
William Pursell
  • 204,365
  • 48
  • 270
  • 300
1

Use this Perl one-liner, where [.] is a character class that consists only of a period:

perl -pe 's{[.] }{.\n}g' in_file

To change the file in-place:

perl -i.bak -pe 's{[.] }{.\n}g' in_file
Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47