Lunar Mushrooms
Lunar Mushrooms

Reputation: 8928

Bash - How to remove all white spaces from a given text file?

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.

Upvotes: 117

Views: 447744

Answers (11)

Umae
Umae

Reputation: 445

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

Upvotes: 18

DDD
DDD

Reputation: 489

Easiest way for me:

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

Upvotes: 4

Omar6575
Omar6575

Reputation: 73

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

Upvotes: 5

Paulo Scardine
Paulo Scardine

Reputation: 77251

$ 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:]"

Upvotes: 203

keyser
keyser

Reputation: 19189

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

Upvotes: 11

Agnibesh Chauhan
Agnibesh Chauhan

Reputation: 56

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

Upvotes: 2

Keimond
Keimond

Reputation: 11

Try this:

tr -d " \t" <filename

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

Upvotes: 1

Gorton Fishman
Gorton Fishman

Reputation: 98

This is probably the simplest way of doing it:

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

Upvotes: 2

glenn jackman
glenn jackman

Reputation: 246807

If you want to remove ALL whitespace, even newlines:

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

Upvotes: 5

Lucie G
Lucie G

Reputation: 439

Much simpler to my opinion:

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

Upvotes: 33

Jerry Coffin
Jerry Coffin

Reputation: 490128

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

Upvotes: 0

Related Questions