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!
e !ed -s % <script.cat cript | ed - file