Remove White Space In Text or String Using Awk and Sed In Linux

In Linux there are couple of ways to remove white space in text. In this post, I will talk about awk and sed to manipulate text in Linux.

Lets start with Sed.

Sed is a great command line utility in Linux. There are whole bunch of things you can do with sed but for the purpose of this article, we would talk only about sed regex for removing space in strings or text. Checkout online utility to remove whitespace. Also checkout sed find and replace cheatsheet with examples.

Remove White Space In Bash Using Sed

Lets create a variable in bash which contains following string.

text=" this is a test text to show how sed works to remove space"

Lets say we want to remove all the white spaces in above text using sed.

echo "$text" | sed 's/ //g'
thisisatesttexttoshowhowsedworkstoremovespace

To explain in brief, we are echoing $text and piping to sed. In sed, we are substituting space with nothing "s/ //g". The last g means remove it globally across the whole text.

Lets say we want to remove leading space using sed.

echo "$text" | sed 's/^ //g'
this is a test text to show how sed works to remove space

sed s/^ / means substitute leading space only

For removing trailing space using sed, do following.

echo "$text" | sed 's/ \$//g'
 this is a test text to show how sed works to remove space

sed s/ $/ means trailing space.

How To Replace Multi Spaces With Single Space Using Sed in Linux

To remove multiple spaces use [ ]+ in sed. [ ]+ means match more than one space. Let do same example by inserting more spacing in words.

text1="This is a test text to       show how to remove     multi spaces using sed     in Linux"
echo "$text1" | sed 's/[ ]\+/ /g'
This is a test text to show how to remove multi spaces using sed in Linux

Note we need to escape special character + with back slash while [ ]+ in sed.

How To Replace Multiple Spaces with Single Space For Each Line In A File Using Sed

Lets create a dummy file and couple of lines in that file.

echo "this a first line    with multiple spaces" > /tmp/test.txt
echo "this a second line with         multiple spaces" >> /tmp/test.txt

Lets check our file.

cat /tmp/test.txt
this a first line    with multiple spaces
this a second line with         multiple spaces

Ok we have our file. Lets say we want to replace multiple spaces with single space using the same sed command.

cat /tmp/test.txt | sed 's/[ ]\+/ /g'
this a first line with multiple spaces
this a second line with multiple spaces

it worked.

Lets say we want to use sed only on 2nd line. Used sed '2s/[ ]+/ /g' to do that as shown below.

cat /tmp/test.txt | sed '2s/[ ]\+/ /g'
this a first line    with multiple spaces
this a second line with multiple spaces

How To Remove White Space In Text Using AWK

Lets echo our text again

echo $text
this is a test text to show how sed works to remove space
Use awk gsub as shown below.
echo "$text" | awk '{ gsub(/ /,""); print }'
thisisatesttexttoshowhowsedworkstoremovespace

How To Replace Multi Spaces With Single Space Using Awk in Linux

We will use $text1 with multiple spaces for this example:

echo "$text1"
This is a test text to       show how to remove     multi spaces using sed     in Linux
echo "$text1" | awk '{ gsub(/[ ]+/," "); print }'
This is a test text to show how to remove multi spaces using sed in Linux

Note we don't need to escape + because it is not a special character in awk gsub

How To Use Awk To Replace Multiple Spaces With Single Space For Each Line In A File.

cat /tmp/test.txt | awk '{ gsub(/[ ]+/," "); print }'
this a first line with multiple spaces
this a second line with multiple spaces

Wrap Up!

This post I have tried to cover two basic utilities awk and sed and shown how these can be used to remove white space.