Discuss Replacing tabs with spaces in the UNIX Help forum on Dev Shed. Replacing tabs with spaces UNIX Help forum discussing the Unix Operating System and all variants including Irix, Solarix, and AIX. Unix was designed as a true multi-user operating system.
The ASP Free website provides in-depth information on the latest developer tools available from Microsoft. Our cadre of writers, highly experienced industry experts, reveals the best ways to use established technologies as well as new and emerging technologies. Our coverage of Microsoft's development and administration technologies is among the most respected in the IT industry today.
ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month! Download and Activate to enter!
Intel® Graphics Performance Analyzers is a powerful tool suite for analyzing and optimizing your games, media, and graphics-intensive applications. Used by some of the best developers on the planet, Intel GPA lets you maximize your app’s performance.
Posts: 14
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 06:20 AM.
Reason: grammar corrections
Posts: 14
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) >>>
Posts: 14
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.
Posts: 44
Time spent in forums: 1 Day 3 h 39 m 20 sec
Reputation Power: 8
Quote:
Originally Posted by samsungcoffee
I do this all the time. For some reason, neither typing "\t" nor hitting the 'tab' key work for me. Instead, I type:
ctrl + v + i
This is the only way I can enter a tab in my terminal... not sure why, but I've become so used to doing it this way, I don't even notice it anymore.
That behaviour matches mine. I use the Korn shell (ksh) and due to far too many years to admit using vi, the first thing I set up is a set -o vi in my .profile.
Posts: 96
Time spent in forums: 20 h 57 m 33 sec
Reputation Power: 139
Your syntax is fine, the issue is that the version of sed installed with Solaris is a part of the core pkg.. It hasn't changed in a long time and prob will not be updated.
As I'm a Solaris admin and regular user of sed, I almost always dump a freshly compiled copy of it in /usr/local.