Banner of text editor, ed the text editor

ed text editor: An advanced tutorial with scripts


Category: Linux

Date: February 2023
Views: 788


ed is a line-oriented text editor that was popular in the early days of Unix. Although it's not as widely used today as more modern text editors like vi or emacs, it's still available on many systems and is sometimes used for scripting purposes.

Starting ed

To start the ed editor, simply type "ed" at the command prompt and press Enter. You will be taken to the ed prompt, which looks like this:

    
  $ ed
    

To open an existing file in ed, you can use the following syntax:

    
  $ ed <file name>
    

Basic commands

The ed editor is controlled by typing commands at the prompt. Some basic commands include:

  • a: append text to the end of the file
  • i: insert text before the current line
  • p: print the current line
  • d: delete the current line
  • q: quit the editor

Advanced commands

In addition to the basic commands, ed also supports a number of more advanced commands for editing text:

  • c: change the current line
  • n: print the current line number
  • /text/: search for the specified text
  • s/old/new/: replace the first occurrence of "old" with "new" in the current line
  • s/old/new/g: replace all occurrences of "old" with "new" in the current line
  • u: undo the last change

Adding text

To add text to the file, use the 'a' command. After you type 'a', you can enter the text you want to add. To end the text input and return to the ed prompt, press the escape key (ESC).

    
  $ ed
  a
  <text to be added>
  ESC
    

Saving and quitting

To save your changes and quit the editor, type 'w' followed by 'q'. To quit without saving changes, simply type 'q'.

    
  wq
    

In addition to using ed interactively, it's also possible to use ed to perform one-line commands from the terminal. These commands are useful for quickly editing a file without having to start the interactive editor. For example, to replace text between two patterns in a file, you can use the following syntax:

    
$ echo 'text to be edited' | ed -s file.txt <<< $'/pattern1/,/pattern2/s/old/new/g\nw\nq'
    

This will replace all occurrences of "old" with "new" in the file "file.txt" between the first line containing "pattern1" and the first line containing "pattern2". The changes will be saved to the file and the ed editor will quit.

    
$ printf '%s\n' "/$pattern1/+1,/$pattern2/-1d" "/$pattern1/a" "$TEXT" . wq | ed -s "$texFile"
    

This command uses the `printf` command to generate the ed commands, which are passed to the `ed` editor using a pipe. The first line of the generated commands, `"/$pattern1/+1,/$pattern2/-1d"`, deletes all lines from the line after the first occurrence of `$pattern1` to the line before the first occurrence of `$pattern2`. The second line of the commands, `"/$pattern1/a"`, appends the text `$TEXT` to the line containing `$pattern1`. The `.` on the last line of the commands saves the changes and the `wq` quits the editor.

    
# Append text to the end of a file
$ echo "$TEXT" | ed -s "$file" <<< $'$a\n'"$TEXT"'\n.\nw\nq'

# Insert text at the beginning of a file
$ echo "$TEXT" | ed -s "$file" <<< $'0a\n'"$TEXT"'\n.\nw\nq'

# Replace a line in a file
$ echo "$TEXT" | ed -s "$file" <<< $'$LINE_NUMBER c\n'"$TEXT"'\n.\nw\nq'

# Delete a line from a file
$ ed -s "$file" <<< $"$LINE_NUMBER d\n.\nw\nq"

# Search and replace text in a file
$ ed -s "$file" <<< $'1,$s/old/new/g\n.\nw\nq'

# Search for a pattern and print matching lines
$ ed -n "$file" <<< $'/PATTERN/p\nq'
    

In these examples, `$TEXT` is the text you want to insert, `$LINE_NUMBER` is the line number in the file where you want to perform an operation, `$file` is the name of the file you want to edit, `old` is the text you want to replace, `new` is the replacement text, and `PATTERN` is the pattern you want to search for.

    
#!/bin/sh

# Define the file to edit
file="file.txt"

# Define the text to insert
text="This is some text"

# Define the line number to insert the text at
line_number=3

# Use ed to insert the text at the specified line number
ed -s "$file" <<-EOF
$line_number
a
$text
.
w
q
EOF

    

This script uses ed to insert the text `$text` at line number `$line_number` in the file `$file`. The script starts by defining the file to edit, the text to insert, and the line number to insert the text at. The script then uses ed in a here document to insert the text. The here document contains ed commands to go to the specified line number, append the text, save the changes, and quit the editor.

let's say we have a log file that contains entries. and it is organized this way:

.....
%%12:01-02-2022:start
    ....
%%12:01-02-2022:end
%%13:17-07-2022:start
    ....
%%13:17-07-2022:end
%%14:21-10-2022:start
    ....
%%14:21-10-2022:end
....

Each entry starts with pattern %%entry_number:entry_date:start and ends with %%entry_number:entry_date:end. suppose we want to edit an entry but we only know its date and number but we don't want to browse the millions of lines manually to find it. with "ed" and other commands like "grep" we can do that.

    
#!/bin/sh

# Define the log file
log_file="log.txt"

# Define the start pattern
start_pattern="%%entry_number:entry_date:start"

# Define the end pattern
end_pattern="%%entry_number:entry_date:end"

# Get the start line number of the entry
start_line_number=$(grep -n "$start_pattern" "$log_file" | awk -F: '{print $1}')

# Get the end line number of the entry
end_line_number=$(grep -n "$end_pattern" "$log_file" | awk -F: '{print $1}')

# Use ed to extract the entry into a temporary file
ed -n "$log_file" <<-EOF
$start_line_number,$end_line_number w entry.tmp
q
EOF

# Edit the temporary file as needed
# ...

# Use ed to reinsert the edited entry into the log file
ed -s "$log_file" <<-EOF
$start_line_number,$end_line_number d
$start_line_number
r entry.tmp
.
w
q
EOF
    

In this example, the script uses `grep` to search for the start and end patterns in the log file and retrieve the line numbers where they occur. The script then uses the line numbers in the `ed` commands to extract and reinsert the entry in the same way as in the previous example.

ed text editor and many other similar tools take advantage of the organized and repetitive patterns found in many configuration files, making it easier to automate and manipulate these files in various ways. This is because ed and other tools are designed to work with text files, and these repetitive patterns allow these tools to locate specific information and make changes to it with just a few simple commands. Additionally, these tools can be combined with other utilities, such as grep, to further increase their functionality and allow for even more advanced text manipulation. This is why the well-organized structure of configuration files is so important in the world of computing, as it allows for a wide range of powerful and efficient tools to be used to manage and manipulate these files.

Conclusion

In conclusion, ED is a powerful and versatile text editor that offers many features and functionalities for advanced text manipulation. From basic editing tasks to complex text operations, ED provides a simple and efficient way to work with text files in a Unix environment. Whether you are a seasoned developer or just getting started, ED is definitely worth exploring and learning.



788 views

Previous Article Next Article

2 Comments, latest

  • yassin boulila
    February 2023

    good luck teacher

  • mosaid Admin Reply
    February 2023

    yassin boulila:
    good luck teacher

    thanks my friend