#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    14
    Rep 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
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Location
    Dublin, Ireland
    Posts
    174
    Rep Power
    48
    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?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    14
    Rep 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.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Location
    Dublin, Ireland
    Posts
    174
    Rep Power
    48
    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:
    perl -e '($_ = join "",<>) =~ s/(\t)/     /g; print;' < foo > bar
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    14
    Rep 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.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    142
    Rep Power
    34
    did you forgot to turn off the \ in the regex?
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Location
    Dublin, Ireland
    Posts
    174
    Rep Power
    48
    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.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    14
    Rep Power
    0
    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.
  16. #9
  17. No Profile Picture
    ......@.........
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2004
    Posts
    1,345
    Rep Power
    59
    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.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2009
    Posts
    1
    Rep Power
    0
    Try this:
    cat oldfile | sed -e 's/[EnterTabSpace]//g' > NewFile
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    4
    Rep Power
    0
    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.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2007
    Posts
    44
    Rep Power
    14
    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.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2008
    Posts
    96
    Rep Power
    144
    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.

IMN logo majestic logo threadwatch logo seochat tools logo