Help with removal of blank spaces in a file

Hello..
I have a text file. I want to remove all the blank spaces(except tab) from the file..

I tried using sed command as shown below

sed 's/ //g' file1

But the problem with the above command is that it also eliminates 'tab' which is between the columns..

For example if the contents of the file are

1<tab>a,<space> b,c<tab>        c,<space>d

Then if I use the above command, I get the following output

1<tab>a,b,c<space>c,d

Observe that a tab between the 2nd and the 3rd column has been converted to space!! :wall:
I really don't want to remove tabs!

Can anyone help me out??
Thanks in advance.

Hey, it does not seem to be a problem exactly with your code.

One possible fallout can be that your c<space>c is really a c<tab>c, but while on display it looks like one single space. A <tab> moves the character to next tab stop.

1 Like

I can't reproduce the problem either. Only spaces are removed in my test file and it all works beautifully well.

So either it's a version that's different enough (GNU sed v4.2.1 here) or the characters you replace are really just multiple spaces. Try 'hexdump -C textfile' or use the sequence 'ga' in normal mode in vim to get info about the character under your cursor.

1 Like

Then why the gap between them is so less??
I don't understand..
But the first tab is intact!

---------- Post updated at 03:44 PM ---------- Previous update was at 03:40 PM ----------

Hey 'hexdump -C file' helped me!!
Its indeed a tab.. Don't know why it is displaying in that fashion!! :slight_smile:
Anyway thank you so much!! :slight_smile:

---------- Post updated at 03:45 PM ---------- Previous update was at 03:44 PM ----------

Hey its indeed a tab.. Thank you!! :slight_smile:

A tab does not have fix length, it depends on how much space the previous word occupies, it can look like 4 spaces, and sometimes 1 space when displayed on your console.

A tab character is designed to help you produce tables using columns that are aligned with each other. If it always produced a fixed number of spaces it would be pretty pointless in aligning columns, wouldn't it? :slight_smile: You can read all about it here: Tab key - Wikipedia, the free encyclopedia

FYI - Another way to view tab characters in a file.

If you have the file open in vi, in command mode type:

:set list

to see a tab character represented as a ^I and the end-of-line as a $:

space [ ]$
tab[^I]$
:set nolist

turns in back off. Also see:

:set all

and have a look at the ts= (tab stop) setting. It sets how many spaces a tab character will produce.

1 Like

Thank you all for your information!! :slight_smile: