Linux ex Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the powerful ex command in Linux for text processing and editing. The ex command is a line-oriented text editor that allows you to perform various editing operations on files directly from the command line. The lab covers the basics of the ex command, including understanding its syntax, performing basic editing operations, and automating ex commands using scripts. This lab is suitable for users who want to enhance their text processing and editing skills in a Linux environment.

The lab is divided into three main steps. First, you will learn the basics of the ex command, such as opening files, navigating through lines, inserting and appending text, and saving and quitting. Next, you will explore more advanced editing operations, including deleting lines, searching and replacing text, and copying and moving lines. Finally, you will learn how to automate ex commands using scripts, allowing you to streamline your text editing workflows.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) linux/TextProcessingGroup -.-> linux/sed("`Stream Editing`") linux/VersionControlandTextEditorsGroup -.-> linux/vim("`Text Editing`") linux/VersionControlandTextEditorsGroup -.-> linux/nano("`Simple Text Editing`") subgraph Lab Skills linux/sed -.-> lab-422665{{"`Linux ex Command with Practical Examples`"}} linux/vim -.-> lab-422665{{"`Linux ex Command with Practical Examples`"}} linux/nano -.-> lab-422665{{"`Linux ex Command with Practical Examples`"}} end

Understand the ex Command Basics

In this step, you will learn the basics of the ex command, a powerful text editor in Linux. The ex command is a line-oriented text editor that allows you to perform various editing operations on files directly from the command line.

First, let's start by understanding the ex command syntax:

ex [options] [file]

Here, [options] represents the various options you can use with the ex command, and [file] is the file you want to edit.

Now, let's try some basic ex commands:

  1. Open a file in ex mode:

    ex file.txt

    This will open the file.txt in ex mode, where you can perform various editing operations.

  2. Display the current line number:

    :number

    This will display the current line number.

  3. Move to a specific line:

    :10

    This will move the cursor to line 10.

  4. Insert text:

    i
    This is a new line.
    .

    The i command enters insert mode, and the . command exits insert mode.

  5. Append text:

    a
    This is another new line.
    .

    The a command enters append mode, and the . command exits append mode.

  6. Save and quit:

    :wq

    The :wq command saves the file and exits ex mode.

Example output:

$ ex file.txt
"/file.txt" [New File]
:number
1
:10
10
i
This is a new line.
.
a
This is another new line.
.
:wq

These are just a few basic ex commands. In the next step, you will learn how to perform more advanced editing operations with the ex command.

Perform Basic Editing Operations with ex

In this step, you will learn how to perform basic editing operations using the ex command.

First, let's create a sample file to work with:

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

Now, let's try some basic editing operations:

  1. Delete a line:

    ex file.txt
    :2d
    :wq

    The :2d command deletes the second line of the file.

  2. Insert a line:

    ex file.txt
    :2i
    This is a new line.
    .
    :wq

    The :2i command inserts a new line after the second line.

  3. Append text to a line:

    ex file.txt
    :2a
    This is appended text.
    .
    :wq

    The :2a command appends text to the second line.

  4. Replace text in a line:

    ex file.txt
    :%s/first/replaced/g
    :wq

    The :%s/first/replaced/g command replaces all occurrences of "first" with "replaced" in the file.

Example output:

$ cat file.txt
This is the replaced line.
This is a new line.
This is the third line.

As you can see, the ex command provides a powerful way to perform basic editing operations on files directly from the command line.

Automate ex Commands Using Scripts

In this step, you will learn how to automate ex commands using scripts. This can be useful when you need to perform repetitive editing tasks or apply the same set of changes to multiple files.

Let's create a simple script to automate some ex commands:

  1. Create a new file called ex_script.sh in the ~/project directory:

    nano ~/project/ex_script.sh
  2. Add the following content to the script:

    #!/bin/bash
    
    ## Open the file in ex mode
    ex file.txt << EOF
    ## Insert a new line at the beginning
    1i
    This is a new line inserted at the beginning.
    .
    ## Replace "first" with "replaced" in the file
    :%s/first/replaced/g
    ## Save and quit
    :wq
    EOF

    This script will:

    • Open the file.txt in ex mode
    • Insert a new line at the beginning of the file
    • Replace all occurrences of "first" with "replaced"
    • Save the changes and exit ex mode
  3. Make the script executable:

    chmod +x ~/project/ex_script.sh
  4. Run the script:

    ~/project/ex_script.sh

Now, let's verify the changes made by the script:

cat file.txt

The output should be:

This is a new line inserted at the beginning.
This is the replaced line.
This is a new line.
This is the third line.

As you can see, the script has successfully automated the ex commands, making it easy to apply the same set of changes to the file.

Summary

In this lab, you learned the basics of the ex command, a powerful text editor in Linux. You started by understanding the ex command syntax and practicing some basic ex commands, such as opening a file, displaying the current line number, moving to a specific line, inserting and appending text, and saving and quitting. You then learned how to perform basic editing operations using the ex command, such as deleting a line, searching and replacing text, and copying and pasting lines.

The ex command is a versatile tool that allows you to perform various editing tasks directly from the command line, making it a valuable addition to your Linux toolbox. By mastering the ex command, you can streamline your text editing workflows and increase your productivity.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like