2

I have created a script file called "cleanup" which contains a series of regular expressions that clean up an ed file(s) of blank spaces, trailing white-space, blank lines etc. I run it as follows:

ed [name-of-file] < [name-of-script]

I sometimes want to run the script on the file I am currently editing from within ed. I am unsure of the syntax I would need to do that.

Here is an example script:

g/^  */s///
# Remove blank spaces at the beginning of a line
g/  *$/s///
# Remove trailing whitespace at end of line
g/   */s// /g
# Remove additional spaces between words
g/^$/d
# delete blank lines
g/\(‘‘\|’’\)/s//"/g
# Remove curly braces
g/\(“\|”\)/s//"/g
g/\(‘\|’\)/s//'/g
# idem
,p
Q
4
  • 2
    Does the script output the edited file to standard output? If so, just editing the output would be easy with e !ed -s % <script. Commented Jul 14, 2021 at 14:58
  • cat cript | ed - file Commented Jul 14, 2021 at 15:29
  • @nezabudka this unfortunately does not work for me. Can you provide more information? Commented Jul 14, 2021 at 15:43
  • 1
    @devcom, Could you update the thread and add an example script? Commented Jul 14, 2021 at 16:25

2 Answers 2

3

You could possibly use

e !ed -s % <script

if the script ends with outputting the modified editing buffer, i.e. if the script ends with ,p and Q. This would replace the contents of the current editing buffer with the output of the command after !. The % in the command will automatically be replaced by the current file's name (this command will not change the name of the current file).

Just remember to w first so that the spawned ed sees your edits up until that point.

(This does not seem to apply to your example script, but...) If the script does in-place editing of the file so that the file is saved back to disk at the end, then use

!ed -s % <script
e

This executes your ed command in a shell and then replaces the current editing buffer with the updated file contents. Again, do not forget to w first.


Further explanation:

The e !somecommand command discards the current editing buffer and replaces it with the output of somecommand.

This means that e !ed -s % <script would be appropriate if the script editing script outputs the final result on standard output, and you would like to edit that output.

The e command, with no further argument, re-opens the current file and replaces the editing buffer with that file's content. If this is preceded by !somecommand where somecommand changes the current file, then this is how you incorporate those changes into the editing session.

This means that !ed -s % <script followed by e would be what you may want to use if the script editing script saves the modified document back to the same name (rather than writing it to standard output).

0
0

I created a text file that contains two lines:

$ cat helloworld.txt
hello world
goodbye world

ed one-liner equivalent:

$ ed -s helloworld.txt <<< ",p"
hello world
goodbye world

write the script to a file

echo ",p" > edscript

does it work with a file? Sort of...

ed -s helloworld.txt <<< $(cat edscript)

what about multi-line scripts? Here I change the 1st line, substitute the world "world" for "neptune":

echo "1s/world/neptune/" >> edscript
echo ",p" >> edscript

$ ed -s helloworld.txt <<< $(cat edscript)
?

nope... Then I remember that ed is a line-by-line editor which came about when text was printed to a dot-matrix printer and took real, actual time; thus, outputting ALL of the results of cat is stupid. Instead, pipe it through via "|", which pipes output line-by-line :

$ cat edscript | ed -s helloworld.txt
hello neptune
goodbye world

Let's try adding some interesting stuff. Here I insert a line between 1 and 2, which shifts the current line 2 downward:

edscript:
,p
1s/world/neptune/
2i
how are you doing?
.
,p

outputs:

$ cat edscript | ed -s helloworld.txt
hello neptune
how are you doing?
goodbye world

Let's completely rewrite what is now line 3:

edscript:
,p
1s/world/neptune/
2i
how are you doing?
.
3c
so long and thanks for all the fish!
.
,p

and re-run:

$ cat edscript | ed -s helloworld.txt
hello neptune
how are you doing?
so long and thanks for all the fish!

of course, some people might take offense to using cat, but the solution presented itself at the beginning: just replace cat edscript with the equivalent ed:

$ ed -s edscript <<< ",p" | ed -s helloworld.txt
hello neptune
how are you doing?
so long and thanks for all the fish!
6
  • Why are you using cat and a here-string to read and pass the ed script to ed? That's what makes multi-line scripts break (since the unquoted expansion of the command substitution replaces all newlines (and tabs, and consecutive spaces) with single spaces). The first part of your answer is just you realising this, and that can be removed. The rest of the answer does not seem to relate to the question and shows various ways of passing the script to ed (missing the most obvious, ed -s textfile <script). However, the question is about running an ed script from within ed. Commented May 6, 2025 at 9:06
  • Another way of stating the question is: How may I write script (used in the command ed -s textfile <script) so that it calls ed internally to perform some operation? The user wants to do this because they have an existing ed script that cleans up whitespace in a certain way, and they would want to re-use that code. Commented May 6, 2025 at 9:20
  • the previous solution used !ed -s % <script as a solution, which I couldn't get to work on the terminal, hence the above stream of conciousness. Using your solution of ed -s helloworld.txt <edscript produced the desired result. Commented May 7, 2025 at 21:14
  • FWIW though, I did include ways to not use cat or here-strings. Commented May 7, 2025 at 21:33
  • 2
    !ed -s % <script is not a terminal command. It's a command one would run from within an ed editing session to execute the ed-script script on the current document. One would then follow that up with the e command (with no arguments) to re-open the now modified file for editing. Commented May 7, 2025 at 23:11

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.