UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 30th, 2006, 05:17 AM
mswartz mswartz is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Posts: 14 mswartz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 35 m 57 sec
Reputation Power: 0
Replacing tabs with spaces

I have a file with lots of tabs and I want to replace all the tabs with five spaces. The tabs are scattered around the text file and in no particular order. Here's what I've tried:

>>> sed 's/\t/ /g' oldfile > newfile

On this code I used \t because it's the escape character in C++ to print tabs, plus the ascii table says that what tabs are. This code literally replaced all the lower case t's with 5 spaces.

***************************************

>>> sed 's/\011/ /g' oldfile > newfile

On this code I used \011 because it's the octal notation for tabs. It didn't do anything as far as I can tell because as far as I know there aren't any strings containg the chars 011. So that didn't work either. Also, since the octal notation didn't work I don't see why the decimal or hex notation would work either.

***************************************

>>> sed 's/\^I/ /g' oldfile > newfile

On this code I tried ^I because it's the hidden character for tabs in vi, I didn't think it would work and it didn't.

I'm sure there's a simple solution to this, but right now I'm drawing a blank. Any help would be appreciated. thanks.

Last edited by mswartz : July 30th, 2006 at 05:20 AM. Reason: grammar corrections

Reply With Quote
  #2  
Old July 30th, 2006, 08:06 AM
mu's Avatar
mu mu is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Location: Dublin, Ireland
Posts: 174 mu User rank is Sergeant Major (2000 - 5000 Reputation Level)mu User rank is Sergeant Major (2000 - 5000 Reputation Level)mu User rank is Sergeant Major (2000 - 5000 Reputation Level)mu User rank is Sergeant Major (2000 - 5000 Reputation Level)mu User rank is Sergeant Major (2000 - 5000 Reputation Level)mu User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 23 h 39 m 56 sec
Reputation Power: 39
Hmm.. strange that \t doesn't work. As you can see it works for me.

Code:
$ cat bla.txt 
aaa     bbb     ccc     ttt
ddd     eee     fff     ttt


Code:
$ sed --posix -e 's/\t/     /g' < bla.txt 
aaa     bbb     ccc     ttt
aaa     bbb     ccc     ttt


What version of sed are you using?

Reply With Quote
  #3  
Old July 30th, 2006, 04:10 PM
mswartz mswartz is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Posts: 14 mswartz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 35 m 57 sec
Reputation Power: 0
The remote server I log into is using GNU sed version 3.02. Maybe, for some reason the admin doesn't allow this??? Also, just to be thorough I also tried (but also didn't work) >>>

cat oldfile | sed 's/\t/ /g' oldfile > newfile

I still want to try and find a solution. Thanks.

Reply With Quote
  #4  
Old July 30th, 2006, 06:31 PM
mu's Avatar
mu mu is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Location: Dublin, Ireland
Posts: 174 mu User rank is Sergeant Major (2000 - 5000 Reputation Level)mu User rank is Sergeant Major (2000 - 5000 Reputation Level)mu User rank is Sergeant Major (2000 - 5000 Reputation Level)mu User rank is Sergeant Major (2000 - 5000 Reputation Level)mu User rank is Sergeant Major (2000 - 5000 Reputation Level)mu User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 23 h 39 m 56 sec
Reputation Power: 39
That's a fairly old version of sed... rather than trying to find a workaround for sed, might I suggest a perl solution?

perl Code:
Original - perl Code
  1. perl -e '($_ = join "",<>) =~ s/(\t)/     /g; print;' < foo > bar

Reply With Quote
  #5  
Old July 30th, 2006, 07:55 PM
mswartz mswartz is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Posts: 14 mswartz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 35 m 57 sec
Reputation Power: 0
That perl code works! Thank you.

At work, I'm told we use the latest version of Sun, tomorrow I'm going to try out the sed code. I'm willing to bet it works just fine.

Reply With Quote
  #6  
Old July 30th, 2006, 08:51 PM
joej0e joej0e is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Posts: 142 joej0e User rank is Sergeant Major (2000 - 5000 Reputation Level)joej0e User rank is Sergeant Major (2000 - 5000 Reputation Level)joej0e User rank is Sergeant Major (2000 - 5000 Reputation Level)joej0e User rank is Sergeant Major (2000 - 5000 Reputation Level)joej0e User rank is Sergeant Major (2000 - 5000 Reputation Level)joej0e User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 23 h 28 m 27 sec
Reputation Power: 25
did you forgot to turn off the \ in the regex?

Reply With Quote
  #7  
Old July 31st, 2006, 05:26 AM
mu's Avatar
mu mu is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Location: Dublin, Ireland
Posts: 174 mu User rank is Sergeant Major (2000 - 5000 Reputation Level)mu User rank is Sergeant Major (2000 - 5000 Reputation Level)mu User rank is Sergeant Major (2000 - 5000 Reputation Level)mu User rank is Sergeant Major (2000 - 5000 Reputation Level)mu User rank is Sergeant Major (2000 - 5000 Reputation Level)mu User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 23 h 39 m 56 sec
Reputation Power: 39
Quote:
Originally Posted by mswartz
That perl code works! Thank you.

At work, I'm told we use the latest version of Sun, tomorrow I'm going to try out the sed code. I'm willing to bet it works just fine.


I just tested it out on Solaris 10 update 10 with no luck. However I did discover that the `expand` command will do exactly what you're looking for.

Reply With Quote
  #8  
Old August 2nd, 2006, 02:56 PM
mswartz mswartz is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2006
Posts: 14 mswartz User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 35 m 57 sec
Reputation Power: 0
Quote:
Originally Posted by iota
I just tested it out on Solaris 10 update 10 with no luck. However I did discover that the `expand` command will do exactly what you're looking for.



YES! That works nicely. calling up >>>

$ info expand

Says, using the general form of the command >>>

$ expand oldfile > newfile

says that the default is to replace all the tabs with eight spaces. However, on the remote antiquated system I tried this on it was seven spaces. No big deal because you can specify the number of spaces for tab replacement, for example, if you want five spaces then >>>

$ expand -t5 oldfile > newfile


* * * * * * * * *

Someone asked if I turned off the \ in the regex, for the original sed code I tried >>>

$ sed 's/\t/ /g' oldfile > newfile

I didn't since I didn't know I would need to. How is that done? The perl code works great and expand command works great, but I still want to see if I can get it to work with sed. I'm just stubborn like that.

Reply With Quote
  #9  
Old August 2nd, 2006, 03:37 PM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,308 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 5 h 20 m 41 sec
Reputation Power: 48
Use the tab key, not the "tab" character.
Code:
vcspnm:/home/jmcnama> wc -c list.c ;sed 's/     /     /g' list.c | wc -c
2212 list.c
2611
vcspnm:/home/jmcnama>

As you will notice you cannot "see" the tab character but sed clearly replaced tabs with spaces because number of characters in the file list.c grew.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Replacing tabs with spaces


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT