In this guide, we will showcase replacing newlines with space using sed.
Using sed for Replacing Text
First, let’s quickly look at performing basic find and replace using sed. The same method will be used when replacing newlines with spaces with some additional options. The command structure is as follows:
Have a look at the following command:
The echo command prints the string to STDOUT. The STDOUT stream is then piped to sed. We’ve instructed sed to replace any instance of quick to be replaced with fast. Finally, the output is printed on the screen.
Replacing Newlines With Space
For demonstration, I’ve created the following text file test.txt with several dummy contents:
The sed command accepts regular expressions for describing various patterns. Leveraging this feature, we’re going to describe newline as \n. Let’s try replacing the newlines with whitespace:
However, that didn’t work as expected. To achieve this goal, we need some additional options. Have a look at the following command:
The sed command has multiple sections; each denotes a specific task:
- :a: Creates a label ‘a’
- N: Appends the next line to the pattern space
- $!ba: If not the last line, returns to label ‘a’
- s/\n/ /g: Finds and replaces newline (\n) with space (/ /). The pattern is matched globally (/g)
The sed command loops through the steps until it reaches the last line, substituting all \n characters with space.
Instead of this complex command, we can simplify things by using the -z flag that specifies sed to work with null-separated records. The command would look like this:
While sed can do the task just fine, there are some alternative tools. In this section, we’ll briefly examine a couple of them.
For the following example, we can use the tr command to replace newline with whitespace in a simple fashion:
We can also use perl to do the job. The following syntax is also similar to what we used with sed (but simplified):
Another way to replace newlines with whitespace is using the paste command. Note that it can only remove one character:
Similar to sed, Linux comes with another tool awk. Similar to sed, it can also perform some advanced replacements on the input. For our purpose, use the following awk command:
This guide explored how we can replace newline with space using sed. It was achieved in two different ways. This guide also includes other relevant tools that we can use to replace newline with whitespace.
Instead of using complex commands that are hard to memorize, we use Bash scripting to perform many things in Linux. Although it comes at the cost of some performance, the flexibility and usability are worth it. Learn more about Bash scripting for beginners.