Linux Commands

Sed Replace Newline With Space

In UNIX/Linux, the sed command is a dedicated tool for editing streams. It can perform various operations on a text stream, such as searching, finding and replacing, and insertion/deletion. For the most part, however, sed is used to find and replace text contents.

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:

$ sed -e 's///'

Have a look at the following command:

$ echo "the quick brown fox" | sed -e 's/quick/fast/g'

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:

$ cat test.txt

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:

$ sed -e 's/\n/ /g' test.txt

However, that didn’t work as expected. To achieve this goal, we need some additional options. Have a look at the following command:

$ sed -e ':a;N;$!ba;s/\n/ /g' test.txt

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:

$ sed -z -e 's/\n/ /g' test.txt

Alternative Methods

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:

$ tr '\n' ' ' < test.txt

We can also use perl to do the job. The following syntax is also similar to what we used with sed (but simplified):

$ perl -p -e 's/\n/ /' test.txt

Another way to replace newlines with whitespace is using the paste command. Note that it can only remove one character:

$ paste -s -d ' ' test.txt

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:

$ awk 1 ORS=' ' test.txt

Conclusion

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.

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.