2 ms·
This only focuses on using ed interactively. IMHO, ed's greatest utility is in scripts. For example, I still use "diff -e" as I find for small patches, it is
by 1vuio0pswjnm7 4y ago
This only focuses on using ed interactively. IMHO, ed's greatest utility is in scripts. For example, I still use "diff -e" as I find for small patches, it is more concise and no less readable. YMMV. I also like ed for non-interactively editing large files.
sed is most preferred, but IME there are some simple tasks that are not possible (or exceedingly difficult) with sed but possible with ed.
For example, moving lines and joining lines are obvious ones. Another is using line address "0", namely, with the "r" command.
Both sed and ed allow reading in a file after line 1 but AFAIK only ed allows inserting before line 1, at "line 0". According to the ed changelog, in 2006, the source was changed so that the "i" and "c" commands treat address 0 as address 1.
For example, a common task for me is to insert the contents of file #1 at the top of file #2.
AFAIK, this is impossible using sed's "r" command, but it's possible using ed's "r" command.
test $# -eq 2||exec echo usage: $0 srcfile dstfile
printf '0r '$1'\nw\nq\n'|ed -s $2
A less space-efficient solution, without resorting to fifos, might be something like
cat $2 $1 > $1.$$.tmp;
exec mv $1.$$.tmp $1;
- 1vuio0pswjnm7 4y agoTurns out this was mentioned on StackOverflow in 2017. Here is a sed solution sed -i -e1r/dev/stdin -e1N $1 However this still has a limitation. The target file must be more than one line. Using a named pipe for large files test -s 1.fifo||mkfifo 1.fifo cat largefile > 1.fifo & printf '0r 1.fifo\nw\nq\n'|ed -s $1 curl -A "" -4o 1.fifo https://example.com/largefile.gz & gzip -dc largefile.gz|tail -100|sed -i -e 1r/dev/stdin -e1N $1
- 1vuio0pswjnm7 4y agoInstead of sed -i, one can use ired. od -tx1 -An|sed 's/^/w /'|ired -n $1 /dev/stdin Unless I am mistaken, ired does not use a temp file, unlike sed -i.