How to Trim Leading and Trailing Whitespace From the Output?

The “leading” whitespaces are located at the start of the character/pattern/line in a text field. On the other hand, the “trailing” whitespaces are placed at the end of a string in a file/script. The unnecessary use of leading and trailing white spaces messes up the data entry in the output. So it is recommended to remove both to enhance text formatting, clean the data entry and clear the command line output.

This post pens down possible methods to trim the leading and trailing whitespace from each line of some output.

Sample File

The “File1.txt” is taken as a sample file that contains some extra leading and trailing whitespaces displayed in the terminal:

$ cat File1.txt

The leading and trailing whitespaces are not visible more clearly in the output.

Execute the “cat” command with the “|(pipe character)” to replace the whitespaces with the “*(asterisk)” in this way:

$ cat File1.txt | tr " " "*"

At this time, the output clearly shows the whitespaces with the “*(asterisks)”. 

Note: The leading and trailing whitespaces can also be represented by any other special character like “/”, “@”, “!”, “%”, and many others.

Method 1: Using the “sed” Command

The “sed” command stands for “stream editor,” which edits and manipulates text quickly in a text file; it is mainly used for text substitution (find and replace) and insertion/deletion of the specified pattern/character.

The “sed” command can also remove the leading and trailing whitespaces from the text file output or the bash script. 

Let’s practice the sed command with the help of examples:

Example 1: Remove Only Leading Whitspaces

To remove the leading spaces (beginning of each line) from the sample “File1.txt,” use the following combination of both “cat” and “sed” commands:

$ cat File1.txt | sed 's/^[ \t]*//'

The above command contains following parameters:

  • The “cat” command displays the “File1.txt” content.
  • The “|(pipe character)” that pipe the output of the “cat” and “sed” commands.
  • The “sed” uses he “s/(command substitution)” that replaces (^[ \t]*) from the File1.txt.
  • The “^ denotes the start of the line and “[\t]*” matches all the blank and tab spaces.
  • The “//” double forward slashes remove all the matched patterns.

All the leading whitespaces have been removed from the “File1.txt”.

Example 2: Remove Only Trailing Whitspaces

The trailing whitespaces, i.e., from the end of each line into a “File1.txt” execute the “sed”a nd cat command using this format:

$ cat File1.txt | sed 's/[ \t]*$//'

In the above command, the “$” sign indicates the “end of the string”.

All the trailing whitespaces have been removed from “File1.txt”.

Example 3: Remove Both Leading and Trailing Whitespaces

To remove both leading and trailing whitespaces at the same time, use the above both commands jointly in the following manner:

$ cat File1.txt | sed 's/^[ \t]*//;s/[ \t]*$//'

The leading and trailing whitespaces from File1.txt have been removed successfully.

Method 2: Using the “awk” Command

The “awk” command is another command line tool that searches the string in a text file and performs specific operations. It can also be used to define strings, variables, arithmetic operators, numeric functions, and much more.

Here, it is used to trim the leading and trailing whitespace with the help of practical examples

Example 1: Remove Only Leading Whitspaces

If the user only wants to remove the leading white spaces, then specify the “cat” command and “awk” command one after the other in this way:

$ cat File1.txt | awk '{ sub(/^[ \t]+/, ""); print }'

The command contains the following components:

  • The “awk” is the main command to replace the leading whitespaces
  • The “sub” denotes the substitution function.
  • The “^” shows the start of the string
  • The “[ \t]+” specifies one or more whitespaces.
  • The “” double quotes denote nothing in the replacement of whitespaces.
  • The “print” keyword prints the output of the substitution function:

The “awk” command has done the specified(trim leading whitespace) job successfully.

Example 2: Remove Only Trailing Whitspaces

To remove the whitespaces from the end of each line, i.e., trailing whitespaces add the “$(end of the string)” in the substitution function of the “awk” command like this:

$ cat File1.txt | awk '{ sub(/^[ \t]+$/, ""); print }'

The trailing whitespaces have been trimmed from File1.txt globally.

Example 3: Remove Both Leading and Trailing Whitespaces

Same as the “sed” command, the awk command can also remove the leading and trailing whitespaces at the same time:

$ cat File1.txt | awk '{ gsub(/^[ \t]+|[ \t]+$/, ""); print }'

In this command, the “gsub” denotes “global substitution function”, the “^[ \t]+” for leading and “[ \t]+$” for trailing whitespaces:

The “awk” command has been trimmed both leading and trailing whitespaces.

Conclusion

Linux comes with pre-installed “sed” and “awk” commands to trim the leading and trailing whitespaces in the output. Both commands can remove the leading/trailing whitespaces separately and jointly. These commands offer the characters and flags to perform the special operations in the output.

This guide has listed all possible aspects to trim leading and trailing whitespaces from each line of some output.