How to modify text using regular expressions with sed Stream Editor

Contents

How to modify text using regular expressions with sed Stream Editor

All computer users in the world, sooner or later, will modify a text string, either manually or automatically. Be introduced to the flow editor but, which enables you to make massive text modifications in strings and files.

Regular expressions on the command line

The sed sequence editor, a tool available by default on many Linux distributions, makes it possible for you to analyze and transform text in an easy and direct way, whether said text is inside files or just simple strings on the command line. It can also analyze directory lists, process list results and most other types of information that can flow into or out of your terminal session.

To check if sed is enabled on your Linux distribution, scribe sed --version on the command line:

Sed stream editor version

Don't worry if your version is a bit older than the one shown here. It will almost definitely be fine for the examples we discuss here. If the tool is not enabled in your distribution, you can install it using sudo apt install sed for Ubuntu-based operating systems / Debian, O sudo yum install sed for RedHat-based operating systems / RPM.

sed apply a little text transformation using what's called a regex or regular expression. A regular expression is a way and method of expressing search operations, more complex text-based replacement and modification in a still human-readable format. Regular expressions are complicated and can be difficult for beginners to read. You can read our guide, How do you actually use Regex? For more information about how to read and write Regex.

RELATED: How do you actually use Regex?

For the purpose of this post, we will stick with regular expressions that are easy to understand and read. As an example, the following is a simple replacement:

Hello, Cloudsavvyit fan transformation

As you can see, we echoed a small snippet of text and, subsequently, we transform it using the sed tool. We use the command sed 's’ (substitute) and we indicate what we wanted to change. You can read this another way like s|from|to| where '|’ is the separator between sections / fields, ‘s’ is the command y 'from’ And till’ are the two options passed to the 's' command.

Because, we transform ¡Hola, fans of cloud information technologies!’ a “Hello, systempeaker.com fans!” changing only part of the text using sed stream editor. Let's take a slightly more complex example.

Transform text to a file using the sed stream editor

Here we first create a file with 3 lines using the echo command with redirection to a file (‘>’ create a new file, overwriting a file with the same name if it exists, and '>>’ is added to a file without modifying it otherwise). Next, we show the content of the file using the cat command.

Later we transform the content of the file with the sed tool directly. To do it, we specify the option '-i’ for sed which edits the file in place. The actual text transformation instruction is the same as before. And finally we specify the name of the file at the end of the command. Later we check the content of the file and notice how our input has changed.

How can you begin to see, or you already know from past experiences, there is a lot of power available in the sed tool.

Let's spice things up a bit by extending our regex and writing method of the sed text transform statement:

A more complex regular expression and sed statement

In this example, we substitute the letter 'h’ for ‘_H’ and we did it globally (look at the qualifier 'g’ near the end of the first instruction passed to sed) and without distinction between upper and lower case (observe la ‘i’ qualifier also near the end of the first statement). The H’ matches even when the file contains' H’ due to the qualifier 'i'.

Later we start a new instruction (instructions end / end with ';’ just like in many other coding languages) in which we remove '.com’ just looking for it and replacing it with nothing.

Also note how we have used different field alternative separators / command. In the first example we use '|’ which is generally a fairly uncommon and easy to use field separator. Many people use '/', even though this can lead to problems parsing route names that often contain '/'.

Be able to use different field alternatives separators / command enables you to easily use another separator if your text string actually contains or will likely contain a specific character.

Using sed with files

Let's explore another sed-based example where we'll inject text into a file:

Injecting text using the sed stream editor

In this example, we create a file with 5 lines and then we use a slightly complex sed instruction to inject a separator '-‘ after every second line. Although the syntax is mostly self-explanatory, It is interesting to note that the command 'r’ (read / add text from a file) specifies the name of the injection file directly and while still inside the main statement, without any separator.

You can also begin to see that, while learning a regular expression syntax, it is beneficial to learn the sed syntax in depth. It's almost like learning two new foreign languages, but the benefit is that the sed syntax can grow with your need for additional complexity.

Let's explore an example where we replace a specific text area with something else:

Modify specific text areas using thirst

In this example, we look for the text '3', and for that line we will substitute 'C’ por 'Z'. You see how versatile thirst can be.

To end, we can analyze the analysis of other types of input:

Parsing other types of textual input with sed

As you can see, we create 3 files and request a list of directories using the ls command. Later we take that output as input to sed (using linux pipe command, ‘|’) and we transform part of our directory list into an underscore.

The sed sequence editor is a versatile tool that can help you analyze and transform almost any text directly from the terminal command line, whether the input comes from a simple echo statement, other Linux tools or from a plain text file. Enjoy!

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.