Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.

RHCSA - Understand and Use Essential Tools: Create & Edit Text Files (sed)

To next objective in the exam goes into details on creating files, so more details will be present there. Instead we will focus on how we can edit text files using sed.

Create a Practice File

A file to practice with can be created by running:

Create practice file containing characters from Prison Break:

mkdir -p ~/edit-practice
cd ~/edit-practice

cat << EOF > file.txt
Michael Sc0field
Linc0ln Burr0ws
Sara Tancredi
The0d0re Bagwell
Fernand0 Sucre
Benjamin Miles Franklin
Brad Bellick
Pris0n Break
Paul Kellerman
J0hn Abruzzi
Alexander Mah0ne
Ver0nica D0n0v0n

Editing Text Files With sed

The sed command, short for stream editor, is a powerful and versatile text manipulation tool in Unix-like operating systems. It operates on a stream of text, allowing users to perform various operations such as searching, replacing, deleting, and inserting text based on regular expressions or patterns. It is often used in scripting and text processing tasks, enabling efficient and automated editing of files.

sed Sub Commands

The s command to sed is used to perform substitutions in a text. It follows the format s/pattern/replacement/, where pattern is the regular expression to be matched and replacement is the text to be substituted. The s command replaces the first occurrence of the pattern with the replacement text in each line of input.

The g command is used in conjunction with the s command to perform a global substitution. By default, the s command replaces only the first occurrence of the pattern in each line. However, with the g command, all occurrences of the pattern are replaced.

For instance, sed 's/pattern/replacement/g' will substitute all occurrences of pattern with replacement in each line.

The d command in sed is used to delete lines that match a specified pattern. For example, sed '/pattern/d' will delete all lines that contain the specified pattern.

These options can be combined and used together to perform powerful and complex text transformations with the sed command.

Another option we will use in the exercises is the -i flag which enables the in-place edit of a file. Without it, output from sed is printed to screen instead of editing the file.

Replacing Text

Below are some examples of replacing text using sed that you can follow along with.

Correcting the mistake where o has been entered in the list as 0:

sed -i 's/0/o/' file.txt
cat file.txt

The output of the cat command should show the file edit was successful, but only for the first occurence of 0 on a line as we did not use the g switch. Run the same command again with the g switch to correct the remaining typos.

sed -i 's/0/o/g' file.txt
cat file.txt

You will now see all 0 have been changed to o.

Deleting Text

Below is an example of deleting a line matching an expression:

Deleting the show name from the file as it is meant to be a cast list only:

sed -i '/Prison Break/d' file.txt
cat file.txt

Prison Break is now missing from the output.

Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.