Help with removal of blank spaces in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with removal of blank spaces in a file
# 1  
Old 05-03-2011
Bug 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
Code:
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
Code:
1<tab>a,<space> b,c<tab>        c,<space>d


Then if I use the above command, I get the following output
Code:
1<tab>a,b,c<space>c,d

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

Can anyone help me out??
Thanks in advance.

Last edited by Franklin52; 05-03-2011 at 07:04 AM.. Reason: Please use code tags
# 2  
Old 05-03-2011
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.
This User Gave Thanks to 116@434 For This Post:
# 3  
Old 05-03-2011
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.
This User Gave Thanks to decent For This Post:
# 4  
Old 05-03-2011
Quote:
Originally Posted by 116@434
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.
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 ----------

Quote:
Originally Posted by decent
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.

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

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

Quote:
Originally Posted by 116@434
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.
Hey its indeed a tab.. Thank you!! Smilie
# 5  
Old 05-03-2011
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.
# 6  
Old 05-03-2011
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? Smilie You can read all about it here: Tab key - Wikipedia, the free encyclopedia
# 7  
Old 05-03-2011
To view tab characters in vi...

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

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

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

Code:
:set nolist

turns in back off. Also see:

Code:
:set all

and have a look at the ts= (tab stop) setting. It sets how many spaces a tab character will produce.
This User Gave Thanks to gary_w For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Detect blank spaces in a CSV file

I'm monitoring a WLAN network to keep track of new SSIDs popping up. The SSIDs are stored along with the AP MAC address and a few other parameters in a CSV file. A typical line could look like this: 18:70:9f:e3:80:aa 10:11:15 MyNetwork 2437 Now, the problem is that some networks use SSIDs... (4 Replies)
Discussion started by: Zooma
4 Replies

2. UNIX for Advanced & Expert Users

Delete blank spaces and blank lines in a file

Hi Gurus, Somebody can say me how to delete blank spaces and blank lines in a file unix, please. Thank you for advanced. (10 Replies)
Discussion started by: systemoper
10 Replies

3. Shell Programming and Scripting

How to remove all blank spaces in a file

I have a file which contains data such as that shown below. How do i remove all the blcnak spaces, before, during and at the end of each line in one command? 300015, 58.0823212, 230.424728 300016, 58.2276459, 229.141602 300017, 58.7590027, 226.960846 ... (9 Replies)
Discussion started by: carlr
9 Replies

4. Shell Programming and Scripting

Help with removal of blank spaces from the second field!

Hi everyone.. I'm trying to eliminate multiple whitespaces from a file.. I must make use of shell script to eliminate whitespaces.. Take a look at the sample file 1 int main() 2 { 3 int a,b; 4 printf("Enter the values of a and b"); 5 scanf("%d%d",&a,&b); 6 if(a>b) ... (6 Replies)
Discussion started by: abk07
6 Replies

5. Shell Programming and Scripting

how to remove blank spaces in file

hi i have a file which store some data.the contents of my file is data1:data2 data3:data4 i have a script which read this file correct="$(cat /root/sh | cut -d: -f1)" i used this syntax..please help me which syntax is used to remove blank spaces..then how to read this file.. (1 Reply)
Discussion started by: shubhig15
1 Replies

6. Shell Programming and Scripting

Blank spaces missing in the file

I have a file which has blank spaces($) and the blank spaces at the end of the file are not coming up while cutting the files.. please find the following file, desired output.. file1: 001_AHaris$$$$$020$$$$$$$$$ 001_ATony$$$$$$030$$$$$$$$$ 002_AChris$$$$$090$$$$$$$$$... (5 Replies)
Discussion started by: techmoris
5 Replies

7. UNIX for Dummies Questions & Answers

selective removal of blank spaces in string

Hi, I'm a newbie to shell scripting and I have the following problem: I need all spaces between two letters or a letter and a number exchanged for an underscore, but all spaces between a letter and other characters need to remain. Searching forums didn't help... One example for clarity: ... (3 Replies)
Discussion started by: Cpt_Cell
3 Replies

8. Shell Programming and Scripting

Removing blank spaces, tab spaces from file

Hello All, I am trying to remove all tabspaces and all blankspaces from my file using sed & awk, but not getting proper code. Please help me out. My file is like this (<b> means one blank space, <t> means one tab space)- $ cat file NARESH<b><b><b>KUMAR<t><t>PRADHAN... (3 Replies)
Discussion started by: NARESH1302
3 Replies

9. Shell Programming and Scripting

Remove blank spaces in a text file...

Hi, I have this problem that there are blank spaces in my text file... i want to remove them line 1 line 2 line 3 I want to remove the space between line 2 and line 3... I tried sed... it work but it prints the whole text file at the command prompt which i dont want.... sde i tried was... (4 Replies)
Discussion started by: bhagya2340
4 Replies

10. Shell Programming and Scripting

Removing blank spaces from a file?

Guys, I need some help... how can I remove the blank spaces between the lines below? (between the date and the hour fields) 21/05/07 00:05:00 99 21/05/07 00:10:01 99 21/05/07 00:15:00 99 21/05/07 00:20:00 99 21/05/07 00:25:00 99 I want to make the file... (4 Replies)
Discussion started by: dfs
4 Replies
Login or Register to Ask a Question