112

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.

1
  • 4
    by "all whitespace", do you mean newlines as well? Commented Jun 19, 2013 at 23:54

11 Answers 11

197
$ 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:]"
2
  • 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
    Commented Jul 24, 2014 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? Commented Oct 14, 2019 at 6:51
32

Much simpler to my opinion:

sed -r 's/\s+//g' filename
4
  • 2
    I've just tried this and it outputs the amended text to STDOUT but doesn't change the file itself. Commented Dec 9, 2013 at 11:23
  • 6
    @MaxWilliams - just use the -i (dash i) flag with sed Commented Sep 13, 2014 at 21:19
  • 2
    This does not remove newlines, not sure if that was wanted by OP. Commented Aug 10, 2016 at 6:49
  • This also removes every instance of the letter S on macOS bash! :)
    – BSUK
    Commented Sep 9, 2022 at 13:23
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
2
  • 4
    Fine, but I don't think I'll be able to bring myself to write tr -d " " < infile.txt > outfile.txt
    – NReilingh
    Commented Oct 3, 2014 at 19:30
  • Hello, this works thanks, but I would appreciate some explaination. Commented May 13, 2016 at 6:07
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

3
  • 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? Commented May 13, 2016 at 6: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
    Commented Jun 4, 2016 at 11:33
  • Removes all instances of letter S on macOS bash.
    – BSUK
    Commented Sep 9, 2022 at 13:26
5

If you want to remove ALL whitespace, even newlines:

perl -pe 's/\s+//g' file
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
4

Easiest way for me:

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

This is probably the simplest way of doing it:

sed -r 's/\s+//g' filename > output
mv ouput filename
0
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()
2
  • Sorry to bump an old thread, but isn't f.read().strip("\t\n\r ") more efficient?
    – Jachdich
    Commented May 8, 2019 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. Commented Sep 5, 2019 at 7:09
1

Try this:

tr -d " \t" <filename

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.