110

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.

Thanks.

Jerry Chong
  • 7,954
  • 4
  • 45
  • 40
Lunar Mushrooms
  • 8,358
  • 18
  • 66
  • 88

11 Answers11

186
$ 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:]"
Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153
  • 24
    You can also use the character classes defined by `tr`. Examples: To delete _all_ whitespace: `cat file.txt | tr -d "[:space:]"` To delete all horizontal whitespace: `cat file.txt | tr -d "[:blank:]"` – htompkins Jul 24 '14 at 22:54
  • @user3901666 it will remove whitespace, yes, if the output produced will match what you want is hard to say - what have you tried? – Paulo Scardine Oct 14 '19 at 06:51
31

Much simpler to my opinion:

sed -r 's/\s+//g' filename
Thomas Fenzl
  • 4,342
  • 1
  • 17
  • 25
Lucie G
  • 419
  • 4
  • 3
18

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'

To apply into existing file, use following:

sed -i '/^$/d;s/[[:blank:]]//g' hello.txt
Jerry Chong
  • 7,954
  • 4
  • 45
  • 40
Umae
  • 445
  • 2
  • 5
11

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

keyser
  • 18,829
  • 16
  • 59
  • 101
  • 1
    This actually works. Care to explain sed -e 's/[\t ]//g;/^$/d' particularly /^$/d'. I know ^ is for start of string, $ is for end. /d is for delete when using sed. But how does this interpretation lead of deletion of white spaces? – David Okwii May 13 '16 at 06:51
  • I added an explanation. `^$` matches an empty line since it's looking for "start of line" (^) and then immediately after, "end of line" ($). – keyser Jun 04 '16 at 11:33
  • Removes all instances of letter S on macOS bash. – BSUK Sep 09 '22 at 13:26
5

If you want to remove ALL whitespace, even newlines:

perl -pe 's/\s+//g' file
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
5

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
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
4

Easiest way for me:

echo "Hello my name is Donald" | sed  s/\ //g
Asclepius
  • 57,944
  • 17
  • 167
  • 143
DDD
  • 471
  • 1
  • 5
  • 13
2

This is probably the simplest way of doing it:

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

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()
  • Sorry to bump an old thread, but isn't `f.read().strip("\t\n\r ")` more efficient? – Jachdich May 08 '19 at 11:33
  • What if there are two or more subsequent spaces,tabs etc. b/w words? I think your solution is for leading and trailing white-spaces. If you want one liner, you can easily convert the above code. – Agnibesh Chauhan Sep 05 '19 at 07:09
1

Try this:

tr -d " \t" <filename

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

fuz
  • 88,405
  • 25
  • 200
  • 352
0

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).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111