How to remove all white spaces from a given text file

LinuxBashSed

Linux Problem Overview


I want to remove all the white spaces from a given text file. Is there any shell command available for this ? Or, how to use sed for this purpose.

I want something like below:

> $ cat hello.txt | sed ....

I tried this : cat hello.txt | sed 's/ //g' .But it removes only spaces, not tabs.

Linux Solutions


Solution 1 - Linux

$ man tr
NAME
    tr - translate or delete characters

SYNOPSIS
    tr [OPTION]... SET1 [SET2]

DESCRIPTION
   Translate, squeeze, and/or delete characters from standard 
   input, writing to standard output.

In order to wipe all whitespace including newlines you can try:

cat file.txt | tr -d " \t\n\r" 

You can also use the character classes defined by tr (credits to htompkins comment):

cat file.txt | tr -d "[:space:]"

For example, in order to wipe just horizontal white space:

cat file.txt | tr -d "[:blank:]"

Solution 2 - Linux

Much simpler to my opinion:

sed -r 's/\s+//g' filename

Solution 3 - Linux

I think you may use sed to wipe out the space while not losing some infomation like changing to another line.

cat hello.txt | sed '/^$/d;s/[[:blank:]]//g'

Solution 4 - Linux

Try this:

sed -e 's/[\t ]//g;/^$/d' 

(found here)

The first part removes all tabs (\t) and spaces, and the second part removes all empty lines

Solution 5 - Linux

If you want to remove ALL whitespace, even newlines:

perl -pe 's/\s+//g' file

Solution 6 - Linux

Easiest way for me:

echo "Hello my name is Donald" | sed  s/\ //g

Solution 7 - Linux

This answer is similar to other however as some people have been complaining that the output goes to STDOUT i am just going to suggest redirecting it to the original file and overwriting it. I would never normally suggest this but sometimes quick and dirty works.

cat file.txt | tr -d " \t\n\r" > file.txt

Solution 8 - Linux

Dude, Just python test.py in your terminal.

f = open('/home/hduser/Desktop/data.csv' , 'r')

x = f.read().split()
f.close()

y = ' '.join(x)
f = open('/home/hduser/Desktop/data.csv','w')
f.write(y)
f.close()

Solution 9 - Linux

This is probably the simplest way of doing it:

sed -r 's/\s+//g' filename > output
mv ouput filename

Solution 10 - Linux

Try this:

tr -d " \t" <filename

See the manpage for tr(1) for more details.

Solution 11 - Linux

hmm...seems like something on the order of sed -e "s/[ \t\n\r\v]//g" < hello.txt should be in the right ballpark (seems to work under cygwin in any case).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLunar MushroomsView Question on Stackoverflow
Solution 1 - LinuxPaulo ScardineView Answer on Stackoverflow
Solution 2 - LinuxLucie GView Answer on Stackoverflow
Solution 3 - LinuxUmaeView Answer on Stackoverflow
Solution 4 - LinuxkeyserView Answer on Stackoverflow
Solution 5 - Linuxglenn jackmanView Answer on Stackoverflow
Solution 6 - LinuxDDDView Answer on Stackoverflow
Solution 7 - LinuxOmar Essilfie-QuayeView Answer on Stackoverflow
Solution 8 - LinuxAgnibesh ChauhanView Answer on Stackoverflow
Solution 9 - LinuxGorton FishmanView Answer on Stackoverflow
Solution 10 - Linuxuser3653982View Answer on Stackoverflow
Solution 11 - LinuxJerry CoffinView Answer on Stackoverflow