The ed
editor will assume that the document is a Unix text file. When presented with a DOS text file (a file with CRLF line-endings) it will treat the carriage-return characters at the end of each line as any other character.
This means that if you have a DOS text file like the following (here viewed using ,l
to list the file in an unambiguous way, with the carriage-returns made visible as \r
and each end-of-line displayed as $
),
line 1\r$
line 2\r$
line 3\r$
... and then use 2,3j
to join line three onto the end of line two, you'll get
line 1\r$
line 2\rline3\r$
When ed
outputs line 2\rline3\r
to the terminal, the cursor is moved back to the start of the line by the literal carriage-return character embedded in the middle of the line, which gives the impression that the text line 2
disappears (it is in fact overwritten by line 3
).
The solution is to convert your document to Unix text using a tool such as dos2unix
, or, delete all single trailing carriage-returns from within the editor using
,s/^M$//
... where you type ^M
using Ctrl+VCtrl+M (this types a literal carriage-return).
In GNU ed
release 1.18 and later, you may also choose to start the editor with the --strip-trailing-cr
option, which removes any single trailing carriage-return from the end of the lines when you open a file. This is exactly what the s///
command above does.
hexdump
,od
orhd
to view what's really in the file. You won't be able to see the overwritten characters if you just print to the console – phuclv Jun 25 '22 at 09:11