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
Edman
  • 492

1 Answers1

2

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).

Kusalananda
  • 333,661