sed Remove Leading and Trailing Whitespace

sed remove leading and trailing whitespace

Using sed Command with Substitutions

Use the sed command with multiple substitutions to remove leading and trailing whitespaces from a single-line string in Bash.

First, we created a variable named str and set its value to a string value which contained leading and trailing whitespaces. Then, we piped the echo $str command to sed via pipeline (|) to pass the output of echo to the sed command, a stream editor in Unix-like operating systems.

We used the sed command to remove the leading and trailing whitespace; it also removed multiple consecutive occurrences of space within the line and replaced them with a single space. Further, we used the $(...) to capture the output of echo $str | sed 's/^ *//;s/ *$//;' command, which was an updated string; we stored it in modified_str variable.

After that, we used the echo command to display the value of the str and modified_str variables on the Bash console. While displaying the original and updated string, we used the | sign to clearly show leading and trailing whitespaces (you can omit | if you want).

Now, how the sed command removed the leading and trailing whitespaces. We had three consecutive substitution commands with the sed command, s/^ *// and s/ *$//. The semicolon (;) separated one substitution command from another. In each substitution command:

  • s/ – Represented the substitute command.
  • ^ – Denoted the start of the line.
  • $ – Denoted the end of the line.
  • * – Matched zero or more occurrences of the preceding space character.
  • // – It indicated an empty string between the second and third slashes, which means the matched pattern must be replaced with nothing.

So, the first substitution command (s/^ *//) matched and removed leading whitespaces at the start of every line in the input string. The second substitution command (s/ *$//) matched and removed trailing whitespaces at the end of each line in the given input string.

Use the sed command with multiple substitutions to remove leading and trailing whitespaces from a multiline string in Bash.

This code is the same as the previous one, but we removed leading, trailing, and consecutive multiple occurrences of whitespaces from a multiline string.

Alternatively, we can use the sed command with two substitutions as follows.

This code snippet resembles the previous ones, excluding the sed command and removing leading, trailing, and consecutive multiple spaces within the line. Here, we used the -e option with the sed command, which specified the 's/^[ \t]*//;s/[ \t]*$//' argument as a script to be run by the sed command. Note that the 's/^[ \t]*//;s/[ \t]*$//' defined the substitution operation in the sed command.

Let’s break down the substitution command below:

  • s/ denoted the substitute command in sed, used to look for a specified pattern and perform a substitution.
  • ^ represented the start of a line.
  • $ indicated the end of a line.
  • [ \t]* pattern matched zero or more occurrences of a space or a tab character.
  • / separated the given pattern from the replacement.
  • // represented the empty string ('') between the second and third slashes denoting that the matched pattern must be replaced with nothing.

So, when we ran the above script with the specified input string (value of str variable), the sed with the mentioned script argument would look for the leading and trailing whitespaces or tabs from each line and remove them via replacing them with the empty string.

The whitespaces, tab, and newlines are removed using the substitution command. This is why you get a single-line string after removing spaces from the multiline string.

Using sed Command with POSIX Substitutions

We can also perform substitution operations using POSIX classes. Let’s explore them below.

Use the sed command with a POSIX class ([:blank:]) to remove leading and trailing whitespaces from a single-line string in Bash.

Again, we set the str variable with the string value having leading and trailing whitespaces. Then, we piped the output of the echo $str to the sed command, where a substitution operation was performed using the POSIX class ([:blank:]) to eradicate leading and trailing whitespaces or tabs.

Then, we captured the modified string using the $(...) syntax and stored it in the modified_str, which was further used with the echo command to display the updated string on the console. Finally, we displayed both strings on the Bash console, the original and modified string (without leading & trailing whitespaces).

Now, how the script argument defined the substitution operation in the sed command to remove the leading and trailing whitespaces. Let’s break down the script argument ('s/^[[:blank:]]*//;s/[[:blank:]]*$//') below:

  • s/ was the substitute command in sed used to search for a particular pattern and do a substitution.
  • ^ indicated the start of a line, while $ meant the end of the line.
  • [[:blank:]]* pattern matched zero or more occurrences of a blank character (whitespaces or tabs).
  • / separated the given pattern from the replacement.
  • // denoted the empty string indicating the matched pattern must be replaced with nothing.

In the above code example, two consecutive substitution commands were separated by the semicolon. The s/^[[:blank:]]*// matched and removed the leading whitespace (tabs and spaces) at the start of each line. While the s/[[:blank:]]*$// command matched and removed trailing whitespace (tabs and spaces) at the end of every line in the given input.

Use the sed command with a POSIX class ([:blank:]) to remove leading and trailing whitespaces from a multiline string in Bash.

This example is the same as above, but we removed leading and trailing whitespaces from a multiline string.

Alternatively, we can use the [:space] POSIX class as follows.

Use the sed command with a POSIX class ([:space:]) to remove leading and trailing whitespaces from a single-line string in Bash.

Use the sed command with a POSIX class ([:space:]) to remove leading and trailing whitespaces from a multiline string in Bash.

That’s all about sed remove leading and trailing whitespace.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *