Differences between revisions 3 and 6 (spanning 3 versions)
Revision 3 as of 2022-01-07 05:22:49
Size: 1777
Comment:
Revision 6 as of 2023-04-05 17:24:05
Size: 2229
Comment:
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
'''`ed(1)`''' is a non-visual text editor. It is a powerful tool for scripting ''in-place'' file manipulation.

Most system also offer `ex(1)`, which is the '''ex'''tended editor.
'''`ed(1)`''' is a non-visual text editor. It remains a powerful utility for scripted in-place file manipulation.
Line 8: Line 6:

----



== Installation ==

Most modern systems do not offer `ed(1)`. Instead `ex(1)`, which is bundled within the `vim` package, is symlinked to `ed(1)`. This will be pre-installed on any [[Linux]] or [[BSD]] operating system, as a POSIX utility.
Line 80: Line 86:
----



== See also ==

[[https://www.gnu.org/software/ed/manual/ed_manual.html|Ed manual]], the GNU reference document

[[https://docs.freebsd.org/44doc/usd/10.exref/paper.pdf|Ex Reference Manual]], the BSD4.4 UNIX User's Supplementary Document (USD)

Ed

ed(1) is a non-visual text editor. It remains a powerful utility for scripted in-place file manipulation.


Installation

Most modern systems do not offer ed(1). Instead ex(1), which is bundled within the vim package, is symlinked to ed(1). This will be pre-installed on any Linux or BSD operating system, as a POSIX utility.


Input Mode

ed(1) enters input mode after one the following commands has been given:

  • append (a)

  • change (c)

  • insert (i)

Exit input mode by giving a period (.) alone.


Examples

Insert a Line into a File

Shell scripts make it trivial to append to a file. There are excellent tools such as sed(1) for search/replacing text. But there isn't a trivial way to, for example, insert a new line of text into the Nth line of a file.

This script demonstrates how such a task could be automated in part.

# Inserts a new third line into the file,
# which sets `someYamlProperty` to `true` in a Markdown header of some file.
ed "$1" <<EOF
3i
someYamlProperty: true
.
w
EOF

Line Editing

This script will rewrite itself to echo a new message.

message=

#This function uses a heredoc to pass commands into `ed`.
#It works like this:
#  - `1c` targets the first line for changing. Everything entered in input mode will replace this line.
#  - `message="$1"` is being inserted. Note that shell variables are evaluated in a heredoc.
#  - `.` exits input mode
#  - `w` writes the edited file to disk
edScript() {
  ed -s "$(realpath -s $0)" <<EOF
1c
message="$1"
.
w
EOF
}

#If this script is given an arguments, the first argument is passed to the above function.
#Otherwise, the script just echoes "$message".
if [ $# -ge 1 ]; then
  edScript "$1"
else
  echo "$message"
fi


See also

Ed manual, the GNU reference document

Ex Reference Manual, the BSD4.4 UNIX User's Supplementary Document (USD)


CategoryRicottone

Ed (last edited 2023-04-05 17:25:24 by DominicRicottone)