Linux ed Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the powerful line-oriented text editor ed in Unix-like operating systems. The ed command provides a versatile set of features for editing text files directly from the command line, making it particularly useful for scripting and automation tasks. You will start by exploring the basic usage of the ed command, including creating new files and manipulating text. Then, you will delve into more advanced ed command usage, such as working with multiple files and using regular expressions for complex text processing. By the end of this lab, you will have a solid understanding of how to leverage the ed command to efficiently edit and manage text files in your daily workflow.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/TextProcessingGroup -.-> linux/sed("`Stream Editing`") subgraph Lab Skills linux/cat -.-> lab-422657{{"`Linux ed Command with Practical Examples`"}} linux/echo -.-> lab-422657{{"`Linux ed Command with Practical Examples`"}} linux/sed -.-> lab-422657{{"`Linux ed Command with Practical Examples`"}} end

Introduction to the ed Command

In this step, you will learn about the basic usage of the ed command, a powerful line-oriented text editor in Unix-like operating systems. The ed command is a powerful tool for editing text files directly from the command line, and it can be particularly useful for scripting and automation tasks.

First, let's start by checking the version of ed installed in our Ubuntu 22.04 Docker container:

ed --version

Example output:

GNU ed version 1.17

The ed command is a line-oriented text editor, which means that you interact with it by entering commands that operate on the current line or a range of lines. To start the ed editor, simply type ed in the terminal:

ed

This will open the ed editor, and you can now enter commands to manipulate the text. Some basic ed commands include:

  • a: Append text after the current line
  • c: Change the current line
  • d: Delete the current line
  • i: Insert text before the current line
  • l: List the current line
  • p: Print the current line
  • q: Quit the ed editor

For example, let's create a new file called example.txt and add some text to it:

ed example.txt
a
This is a sample text file.
This is the second line.
.
w
q

In this example, we:

  • Opened the ed editor and specified the file example.txt
  • Used the a command to append two lines of text
  • Ended the append mode by entering a single period (.)
  • Wrote the changes to the file using the w command
  • Quit the ed editor using the q command

Now, let's verify that the file was created and contains the expected content:

cat example.txt

Example output:

This is a sample text file.
This is the second line.

In the next step, you will learn how to edit existing text files using the ed command.

Editing Text Files with ed

In this step, you will learn how to edit existing text files using the ed command.

First, let's create a new file called example.txt with some sample content:

echo "This is the first line." > example.txt
echo "This is the second line." >> example.txt
echo "This is the third line." >> example.txt

Now, let's open the example.txt file in the ed editor:

ed example.txt

You should see the following output:

34

This number represents the number of characters in the file. Now, let's try some basic editing commands:

1p

This will print the first line of the file:

This is the first line.

To change the first line, we can use the c (change) command:

1c
This is the updated first line.
.

The . on a new line indicates the end of the change. Let's verify the changes:

1p
This is the updated first line.
2p
This is the second line.
3p
This is the third line.

To delete a line, we can use the d (delete) command:

2d

This will delete the second line. Let's print the file again to see the changes:

1p
This is the updated first line.
2p
This is the third line.

Finally, to save the changes and quit the ed editor, we can use the w (write) and q (quit) commands:

w
41
q

The number 41 represents the number of characters in the updated file.

Let's verify the changes by checking the contents of the example.txt file:

cat example.txt

Example output:

This is the updated first line.
This is the third line.

In the next step, you will learn about more advanced ed command usage.

Advanced ed Command Usage

In this step, you will learn about some more advanced usage of the ed command, including searching, substituting, and working with multiple files.

Let's start by creating another file called example2.txt with some sample content:

echo "This is the first line of example2.txt." > example2.txt
echo "This is the second line of example2.txt." >> example2.txt
echo "This is the third line of example2.txt." >> example2.txt

Now, let's open both example.txt and example2.txt in the ed editor:

ed example.txt example2.txt

You should see the following output:

41
41

This indicates that both files have been opened in the ed editor.

To switch between the files, you can use the f (file) command:

f example.txt
1p
This is the updated first line.
f example2.txt
1p
This is the first line of example2.txt.

To search for a pattern in the current file, you can use the ?pattern? command. For example, to search for the word "line" in the current file:

?line?
This is the updated first line.
This is the third line.

This will print all the lines containing the word "line".

To substitute a pattern with a new string, you can use the s/pattern/replacement/ command. For example, to replace all occurrences of "line" with "sentence" in the current file:

g/line/s//sentence/p
This is the updated first sentence.
This is the third sentence.

The g command is used to perform the substitution globally (on all matching lines).

Finally, let's try appending the contents of example2.txt to example.txt:

f example.txt
$a
$(cat example2.txt)
.
w
q

This will append the contents of example2.txt to the end of example.txt.

Let's verify the changes:

cat example.txt

Example output:

This is the updated first line.
This is the third line.
This is the first line of example2.txt.
This is the second line of example2.txt.
This is the third line of example2.txt.

Congratulations! You have now learned about the advanced usage of the ed command, including working with multiple files, searching, and substituting.

Summary

In this lab, you learned about the basic usage of the ed command, a powerful line-oriented text editor in Unix-like operating systems. You started by checking the version of ed installed in your Ubuntu 22.04 Docker container and then explored the various commands available in the ed editor, such as a for appending text, c for changing the current line, d for deleting the current line, i for inserting text, l for listing the current line, p for printing the current line, and q for quitting the editor. You then created a new file called example.txt and added some text to it using the ed command. Finally, you learned how to edit existing text files using the ed command.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like