_ _
___ | |__ __ _ _______ | |_
/ _ \| '_ \ / _` |_ / _ \| __|
| (_) | | | | (_| |/ / (_) | |_
\___/|_| |_|\__,_/___\___/ \__|
ed_examples —
interactive edit, scripting and
tips
Commands G and V work the same as the lower case version, but
edits each match interactively.
- G/re/
- Returns the first match.
- User edits the match.
- ed displays the next match.
- The user can edit the match or enter & to repeat the last non-empty
command list.
$ ed -p :
:a
line 1
line 2
line 3
.
:G/l/
line 1
s/n/a/
line 2
s/e/o/
line 3
&
:,n
liae 1
lino 2
lino 3
The following commands can be used with shell commands:
| e |
: edit output of command |
: e !command |
| r |
: read output of command into buffer |
: [range]r !command |
| w |
: write addressed lines into command. |
: [range]w !command |
$ ed -p :
:e !ls
:,n
dir1
dir2
:r !ls dir1
:,n
dir1
dir2
file1
:w !xargs -n1 echo
dir1
dir2
file1
$ ed t
a
hello bye
.
w
10
w !awk 'sub("o ","o\n",$0);' > %
e
10
,n
hello
bye
| NOTE: It's important to always have the. and w on any interactive
commands. |
To add a . on a script without ending the input, one option is
to enter \. and then substitute the \ :
,s@^\\.$@.@ |
$ ed script
a
a
this line is appended
\.
w
.
,s@^\\.$@.@
w
q
$ cat script
a
this line is appended
.
w
$ cat test.txt
line 1
$ ed - test.txt < script
$ cat test.txt
line 1
this line is appended