|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
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? |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
||||
|
||||
|
|
|
#5
|
|||
|
|||
|
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. |
|
#6
|
|||
|
|||
|
did you forgot to turn off the \ in the regex?
|
|
#7
|
||||
|
||||
|
Quote:
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. |
|
#8
|
|||
|
|||
|
Quote:
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. |
|
#9
|
|||
|
|||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Replacing tabs with spaces |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|