5

Suppose I have opened a text file in ed, and the current line looks like this:

This is sentence one. Here starts another one.

Now I want to put a newline after one. , such that the new sentence starting with Here starts occupies the next line.

How do I do this in ed?

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125

3 Answers3

6

You use the s command to make substitutions. The format is:

s/pattern/replacement/

To include a newline in the replacement, escape it with a backslash, then press the return key:

s/one. /one.\
/

Where you literally press return, rather than include a \r or \n.

Jim Stewart
  • 16,964
  • 5
  • 69
  • 89
  • Thanks! Yes I was aware that some substitution would do. I was thinking along the lines of .s/\./\.\n/ but it appears that \n yields n. – Dominic van der Zypen Aug 13 '17 at 12:58
  • Sadly this doesn't work as part of a global command (`g/.../s/pattern/replacement/`) --- it's explicitly forbidden by the specification. I have some very old ed scripts which do this, and which don't work on modern eds, and I haven't found a workaround for this yet. – David Given May 23 '18 at 08:59
2

You can do

t.
s/text_before/
-s/text_after/

Explanation:

  1. t. copies the line, in order to get 2 consecutive identical lines, both containing the original text.
  2. Change the 2nd line to contain only the text you want after the added newline.
  3. Do the same, for the 1st line, for text you want before the newline.

NOTE: The '-' prefix means, do it for the previous (of the current addressed) line.

gnued
  • 81
  • 5
1

Use the following command at ed:

s/\. /\.\
/

Be aware that there are two lines.

Using 1,$p you will see the expected result.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
rafarios
  • 11
  • 2