[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [feature] Add edscript as a shortcut for running ed with script
From: |
Andrew L. Moore |
Subject: |
Re: [feature] Add edscript as a shortcut for running ed with script |
Date: |
Sun, 15 Dec 2024 06:01:16 -0500 |
User-agent: |
Mozilla Thunderbird Beta |
On Sat, 28 Sep 2024 at 17:44:03 Alexander Jones wrote:
| On Sat, 28 Sep 2024 at 23:59:55 Artyom Bologov wrote:
| | To edit a file with a
| | script taken from another file, one has to do
| |
| | ed input.txt < script.txt
| |
| | which is unintuitive, I believe. What if it was this?
| |
| | edscript script.txt input.txt
| |
| | I find this more logical—we run ed script upon the file.
|
| I think a flag akin to the -f flag on AWK would be better. (edscript could
| possibly alias "ed -f" in that example.) I could also envision using GNU
| ed's comment handling on '#' to enable shebangs in script files, though I
| admit it would not be portable.
For anyone interested in scripting with ed, please consider:
<https://github.com/slewsys/ed>.
Antonio has hinted that he intends to adopt some of its capabilities,
which include the options -f and -e, external filters, the ability to
edit the output of named pipes, multiple-file support, registers, etc.
This implementation of ed also happens to be more compatible with both
traditional ed and OpenGroup's (SUSv4) ed and has a much more robust
testsuite. Some examples:
$ cat upcase.ed
#!/usr/bin/env -S ed -f
,! tr a-z A-Z
,p
$ ed -f upcase.ed <(cal)
DECEMBER 2024
SU MO TU WE TH FR SA
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
$
Here, ed reads from named pipe `<(cal)' to the editor buffer and
takes commands from the file `upcase.ed'. The ed command `,! tr a-z A-Z'
writes the buffer as standard input to Unix filter `tr', whose output
replaces written buffer lines.
GNU ed is not able to read from named pipes like that; it's not able
to filter buffer lines via external commands; it doesn't support option -f.
The principle advantage of option -f is that one can write pure ed scripts:
$ chmod +x upcase.ed
$ ./upcase.ed <(cal)
DECEMBER 2024
SU MO TU WE TH FR SA
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
$
Another scripting feature adopted from sed is option -e, which allows
writing the upcase script on the command line:
$ ed -e ',!tr a-z A-Z' -e ',p' <(cal)
DECEMBER 2024
SU MO TU WE TH FR SA
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
$
Again, GNU ed does not yet support option -e. Yet another important
scripting feature inpsired by `sed' is support for multiple files:
$ cat file1.txt
hello, world!
$ cat file2.txt
now is the time
$ ./upcase.ed *.txt
HELLO, WORLD!
NOW IS THE TIME
$
An extension inspired by `ex' is registers to copy-and-paste
between files:
$ ed -p '* ' file1.txt
14
* t>
* e file2.txt
16
* <t
* ,p
now is the time
hello, world!
* wq
$ cat file2.txt
now is the time
hello, world!
$
The ed commands `t' and `m' copy and move, respectively, lines from a
source to a destination. Using `>' here as the destination copies
(the current line) to a default register. In the second file, `<t' copies
from the default register to after the current line in the buffer.
And the list goes on. What this implementation of ed does not adopt is
the branching constructs of sed `b', `t' and `T' since ed has enough
strengths without, and higher-level scripting languages like `awk' are
easier to work with.
-AM
- Re: [feature] Add edscript as a shortcut for running ed with script,
Andrew L. Moore <=