Differences between revisions 1 and 2
Revision 1 as of 2021-04-02 21:17:01
Size: 1174
Comment:
Revision 2 as of 2021-11-02 19:24:07
Size: 1245
Comment:
Deletions are marked like this. Additions are marked like this.
Line 4: Line 4:

Most system also offer `ex(1)`, which is the '''ex'''tended editor.

Ed

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 extended editor.


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

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


CategoryRicottone

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