Sed Replace Newline With Space


In UNIX/Linux, the sed command is a devoted instrument for enhancing streams. It can carry out quite a lot of operations on a textual content circulation, reminiscent of looking out, discovering and changing, and insertion/deletion. For probably the most section, then again, sed is used to search out and substitute textual content contents.

In this information, we can exhibit changing newlines with house the use of sed.

Using sed for Replacing Text

First, let’s briefly have a look at acting fundamental to find and substitute the use of sed. The identical manner can be used when changing newlines with areas with some further choices. The command construction is as follows:

Have a have a look at the next command:

$ echo “the quick brown fox” | sed -e ‘s/fast/rapid/g’

The echo command prints the string to STDOUT. The STDOUT circulation is then piped to sed. We’ve advised sed to interchange any example of fast to get replaced with rapid. Finally, the output is imprinted on the display.

Replacing Newlines With Space

For demonstration, I’ve created the next textual content document take a look at.txt with a number of dummy contents:

The sed command accepts common expressions for describing quite a lot of patterns. Leveraging this option, we’re going to explain newline as n. Let’s take a look at changing the newlines with whitespace:

$ sed -e ‘s/n/ /g’ take a look at.txt

However, that didn’t paintings as anticipated. To do so objective, we want some further choices. Have a have a look at the next command:

$ sed -e ‘:a;N;$!ba;s/n/ /g’ take a look at.txt

The sed command has a couple of sections; each and every denotes a particular process:

  • :a: Creates a label ‘a’
  • N: Appends the following line to the development house
  • $!ba: If now not the final line, returns to label ‘a’
  • s/n/ /g: Finds and replaces newline (n) with house (/ /). The development is matched globally (/g)

The sed command loops throughout the steps till it reaches the final line, substituting all n characters with house.

Instead of this advanced command, we will simplify issues by means of the use of the -z flag that specifies sed to paintings with null-separated data. The command would appear to be this:

$ sed -z -e ‘s/n/ /g’ take a look at.txt

Alternative Methods

While sed can do the duty simply high-quality, there are some choice gear. In this phase, we’ll in short read about a few them.

For the next instance, we will use the tr command to interchange newline with whitespace in a easy model:

We too can use perl to do the task. The following syntax could also be very similar to what we used with sed (however simplified):

$ perl -p -e ‘s/n/ /’ take a look at.txt

Another method to substitute newlines with whitespace is the use of the paste command. Note that it might simplest take away one persona:

$ paste -s -d ‘ ‘ take a look at.txt

Similar to sed, Linux comes with every other instrument awk. Similar to sed, it might additionally carry out some complex replacements at the enter. For our function, use the next awk command:


This information explored how we will substitute newline with house the use of sed. It was once accomplished in two alternative ways. This information additionally comprises different related gear that we will use to interchange newline with whitespace.

Instead of the use of advanced instructions which can be onerous to memorize, we use Bash scripting to accomplish many stuff in Linux. Although it comes at the price of some efficiency, the versatility and value are price it. Learn extra about Bash scripting for newcomers.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More