Simple sed replacement of tabs mysteriously failing

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
30
down vote

favorite
11












This ought to be really simple, but for some reason it is not working:



sed -i.bak -E 's/t/ /' file.txt


Instead of replacing tab characters, it's replacing t characters. I've tried every variation on this I could think of, playing with quoting, etc. I've Googled and found everyone else using pretty similar expressions and they seem to work for them.



The -E is an OS X thing. I thought the failure might be a result of some weird quirk of OS X's sed, so I tried it with Ruby as well (without the -i), and got the same result:



ruby -pe '$_.gsub!(/t/," ")' < file.txt > file.new


I'm using Bash 3.2.51 on OS X, and iTerm, although I can't see how any of those could be terribly relevant. I haven't set any weird environment variables, though I can post any that you think might be relevant.



What could be wrong?



UPDATE: I must have made some other mistake or typo when I tried the Ruby version, since Gilles points out that it does work (and I've never had him steer me wrong!). I'm not sure what happened, but I'm pretty sure it must have been my mistake.










share|improve this question



















  • 5




    May be you should try to replace the t in the sed statement with CTRL-V<TAB> where <TAB> is the tab key and CTRL-V is control key and v pressed together.
    – unxnut
    Jul 18 '14 at 14:25










  • if ruby is also getting wrong answer, then it could be your regexp library. (I have tested both your commands, and both replace tab with 2 spaces.) It so then hopefully if you install Gnu sed it will also install the correct library.
    – ctrl-alt-delor
    Jul 18 '14 at 16:44















up vote
30
down vote

favorite
11












This ought to be really simple, but for some reason it is not working:



sed -i.bak -E 's/t/ /' file.txt


Instead of replacing tab characters, it's replacing t characters. I've tried every variation on this I could think of, playing with quoting, etc. I've Googled and found everyone else using pretty similar expressions and they seem to work for them.



The -E is an OS X thing. I thought the failure might be a result of some weird quirk of OS X's sed, so I tried it with Ruby as well (without the -i), and got the same result:



ruby -pe '$_.gsub!(/t/," ")' < file.txt > file.new


I'm using Bash 3.2.51 on OS X, and iTerm, although I can't see how any of those could be terribly relevant. I haven't set any weird environment variables, though I can post any that you think might be relevant.



What could be wrong?



UPDATE: I must have made some other mistake or typo when I tried the Ruby version, since Gilles points out that it does work (and I've never had him steer me wrong!). I'm not sure what happened, but I'm pretty sure it must have been my mistake.










share|improve this question



















  • 5




    May be you should try to replace the t in the sed statement with CTRL-V<TAB> where <TAB> is the tab key and CTRL-V is control key and v pressed together.
    – unxnut
    Jul 18 '14 at 14:25










  • if ruby is also getting wrong answer, then it could be your regexp library. (I have tested both your commands, and both replace tab with 2 spaces.) It so then hopefully if you install Gnu sed it will also install the correct library.
    – ctrl-alt-delor
    Jul 18 '14 at 16:44













up vote
30
down vote

favorite
11









up vote
30
down vote

favorite
11






11





This ought to be really simple, but for some reason it is not working:



sed -i.bak -E 's/t/ /' file.txt


Instead of replacing tab characters, it's replacing t characters. I've tried every variation on this I could think of, playing with quoting, etc. I've Googled and found everyone else using pretty similar expressions and they seem to work for them.



The -E is an OS X thing. I thought the failure might be a result of some weird quirk of OS X's sed, so I tried it with Ruby as well (without the -i), and got the same result:



ruby -pe '$_.gsub!(/t/," ")' < file.txt > file.new


I'm using Bash 3.2.51 on OS X, and iTerm, although I can't see how any of those could be terribly relevant. I haven't set any weird environment variables, though I can post any that you think might be relevant.



What could be wrong?



UPDATE: I must have made some other mistake or typo when I tried the Ruby version, since Gilles points out that it does work (and I've never had him steer me wrong!). I'm not sure what happened, but I'm pretty sure it must have been my mistake.










share|improve this question















This ought to be really simple, but for some reason it is not working:



sed -i.bak -E 's/t/ /' file.txt


Instead of replacing tab characters, it's replacing t characters. I've tried every variation on this I could think of, playing with quoting, etc. I've Googled and found everyone else using pretty similar expressions and they seem to work for them.



The -E is an OS X thing. I thought the failure might be a result of some weird quirk of OS X's sed, so I tried it with Ruby as well (without the -i), and got the same result:



ruby -pe '$_.gsub!(/t/," ")' < file.txt > file.new


I'm using Bash 3.2.51 on OS X, and iTerm, although I can't see how any of those could be terribly relevant. I haven't set any weird environment variables, though I can post any that you think might be relevant.



What could be wrong?



UPDATE: I must have made some other mistake or typo when I tried the Ruby version, since Gilles points out that it does work (and I've never had him steer me wrong!). I'm not sure what happened, but I'm pretty sure it must have been my mistake.







sed osx regular-expression






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 13 '16 at 23:35









don_crissti

48.2k15127157




48.2k15127157










asked Jul 18 '14 at 14:15









iconoclast

3,70463668




3,70463668







  • 5




    May be you should try to replace the t in the sed statement with CTRL-V<TAB> where <TAB> is the tab key and CTRL-V is control key and v pressed together.
    – unxnut
    Jul 18 '14 at 14:25










  • if ruby is also getting wrong answer, then it could be your regexp library. (I have tested both your commands, and both replace tab with 2 spaces.) It so then hopefully if you install Gnu sed it will also install the correct library.
    – ctrl-alt-delor
    Jul 18 '14 at 16:44













  • 5




    May be you should try to replace the t in the sed statement with CTRL-V<TAB> where <TAB> is the tab key and CTRL-V is control key and v pressed together.
    – unxnut
    Jul 18 '14 at 14:25










  • if ruby is also getting wrong answer, then it could be your regexp library. (I have tested both your commands, and both replace tab with 2 spaces.) It so then hopefully if you install Gnu sed it will also install the correct library.
    – ctrl-alt-delor
    Jul 18 '14 at 16:44








5




5




May be you should try to replace the t in the sed statement with CTRL-V<TAB> where <TAB> is the tab key and CTRL-V is control key and v pressed together.
– unxnut
Jul 18 '14 at 14:25




May be you should try to replace the t in the sed statement with CTRL-V<TAB> where <TAB> is the tab key and CTRL-V is control key and v pressed together.
– unxnut
Jul 18 '14 at 14:25












if ruby is also getting wrong answer, then it could be your regexp library. (I have tested both your commands, and both replace tab with 2 spaces.) It so then hopefully if you install Gnu sed it will also install the correct library.
– ctrl-alt-delor
Jul 18 '14 at 16:44





if ruby is also getting wrong answer, then it could be your regexp library. (I have tested both your commands, and both replace tab with 2 spaces.) It so then hopefully if you install Gnu sed it will also install the correct library.
– ctrl-alt-delor
Jul 18 '14 at 16:44











8 Answers
8






active

oldest

votes

















up vote
49
down vote



accepted










The syntax t for a tab character in sed is not standard. That escape is a GNU sed extension. You find a lot of examples online that use it because a lot of people use GNU sed (it's the sed implementation on non-embedded Linux). But OS X sed, like other *BSD sed, doesn't support t for tab and instead treats t as meaning backslash followed by t.



There are many solutions, such as:




  • Use a literal tab character.



    sed -i.bak 's/ / /' file.txt



  • Use tr or printf to produce a tab character.



    sed -i.bak "s/$(printf 't')"/ /' file.txt
    sed -i.bak "s/$(echo a | tr 'a' 't')"/ /' file.txt



  • Use bash's string syntax allowing backslash escapes.



    sed -i.bak $'s/t/ /' file.txt


  • Use Perl, Python or Ruby. The Ruby snippet that you posted does work.






share|improve this answer






















  • For sed scripts which are contained in a ...sed script (used via -f option), the literal tab characters seem the only possibility to me. When editing this with vim, set noexpandtab is important.
    – Tobias
    Jan 11 at 11:46











  • Warning: Only use that "literal tab character" technique if you want your coworker to come back behind you and break your script later. Only use that tr technique if you want your coworker to stab you in the face when they read your script.
    – Bruno Bronosky
    Sep 6 at 18:50


















up vote
11
down vote













Use a Bash specific quoting which lets you use strings like in C, so that a real tab character is passed to sed, not an escape sequence:



sed -i.bak -E $'s/t/ /' file.txt





share|improve this answer
















  • 1




    Also called "ANSI-C" quoting if others want to look up more info about it.
    – wisbucky
    Jun 24 '16 at 23:28






  • 2




    Seems to work on any bourne shell, works on non-bash UNIXes as well. Doesn't work on csh-variants though.
    – jornane
    Nov 15 '16 at 14:43


















up vote
1
down vote













sed -i $'s/t/ /g' file.txt 


works for me on OS X and is the same command i use on linux all the time.






share|improve this answer






















  • Note that this replaces all tabs on every row whereas the OP intends to only replace the first (judging from the command they use).
    – Kusalananda
    Sep 26 '17 at 19:33


















up vote
0
down vote













If you want a more powerful sed (supporting t and more) than the one on OS X, install GNU sed.






share|improve this answer




















  • Since it didn't work with Ruby either, I'm not sure why I would conclude that OS X's sed is the problem. Do you have a reason to believe that's the problem? I'd be happy to install GNU sed if I had reason to believe it would solve the problem, but it seems like I've pretty much ruled that out.
    – iconoclast
    Jul 18 '14 at 15:36










  • With Ruby, you'll have to use only one backslash: ruby -pe '$_.gsub!(/t/," ")' < file.txt
    – vinc17
    Jul 18 '14 at 15:45

















up vote
0
down vote













As noted, not all sed implementations support the notation of t as a horizontal tab.



You can easily achieve your substitution with:



 perl -pi.old -e 'st+ g' file.txt


This performs an in situ replacment which preserves your original file as "*.old". Perl allows alternate delimiters for the classic / making the expression much more readable (i.e. devoid of the "leaning toothpick" syndrome).



The + says one or more repetitions of a tab character are to be replaced. The g modifier enables global replacements throughout the end of each line.






share|improve this answer





























    up vote
    0
    down vote













    sed -i.bak -E 's/\t/ /' file.txt




    share



























      up vote
      -1
      down vote













      I'm surprised no one suggested the very simple solution of:
      sed -i.bak -E 's/\t/ /' file.txt
      That should do the trick.



      You need to escape the escape (hence the 3 s) to allow sed to understand that you are trying to use a t character in the regular expression when everything is substituted...






      share|improve this answer




















      • Why three backslashes specifically?
        – Michael Homer
        Jan 14 '16 at 0:19






      • 3




        If I use GNU sed, one is enough, as no escaping is necessary. The problem is that BSD sed does not support this syntax for tabs.
        – iconoclast
        Jan 16 '16 at 16:43











      • Does not work on my El Capitan.
        – Franklin Yu
        Jun 19 '16 at 6:46

















      up vote
      -4
      down vote













      This worked for me.



      sed -e 's/[t]/ /g'






      share|improve this answer
















      • 3




        This is because you use GNU sed. This is not what the OP uses.
        – Kusalananda
        Sep 26 '17 at 19:34










      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f145299%2fsimple-sed-replacement-of-tabs-mysteriously-failing%23new-answer', 'question_page');

      );

      Post as a guest






























      8 Answers
      8






      active

      oldest

      votes








      8 Answers
      8






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      49
      down vote



      accepted










      The syntax t for a tab character in sed is not standard. That escape is a GNU sed extension. You find a lot of examples online that use it because a lot of people use GNU sed (it's the sed implementation on non-embedded Linux). But OS X sed, like other *BSD sed, doesn't support t for tab and instead treats t as meaning backslash followed by t.



      There are many solutions, such as:




      • Use a literal tab character.



        sed -i.bak 's/ / /' file.txt



      • Use tr or printf to produce a tab character.



        sed -i.bak "s/$(printf 't')"/ /' file.txt
        sed -i.bak "s/$(echo a | tr 'a' 't')"/ /' file.txt



      • Use bash's string syntax allowing backslash escapes.



        sed -i.bak $'s/t/ /' file.txt


      • Use Perl, Python or Ruby. The Ruby snippet that you posted does work.






      share|improve this answer






















      • For sed scripts which are contained in a ...sed script (used via -f option), the literal tab characters seem the only possibility to me. When editing this with vim, set noexpandtab is important.
        – Tobias
        Jan 11 at 11:46











      • Warning: Only use that "literal tab character" technique if you want your coworker to come back behind you and break your script later. Only use that tr technique if you want your coworker to stab you in the face when they read your script.
        – Bruno Bronosky
        Sep 6 at 18:50















      up vote
      49
      down vote



      accepted










      The syntax t for a tab character in sed is not standard. That escape is a GNU sed extension. You find a lot of examples online that use it because a lot of people use GNU sed (it's the sed implementation on non-embedded Linux). But OS X sed, like other *BSD sed, doesn't support t for tab and instead treats t as meaning backslash followed by t.



      There are many solutions, such as:




      • Use a literal tab character.



        sed -i.bak 's/ / /' file.txt



      • Use tr or printf to produce a tab character.



        sed -i.bak "s/$(printf 't')"/ /' file.txt
        sed -i.bak "s/$(echo a | tr 'a' 't')"/ /' file.txt



      • Use bash's string syntax allowing backslash escapes.



        sed -i.bak $'s/t/ /' file.txt


      • Use Perl, Python or Ruby. The Ruby snippet that you posted does work.






      share|improve this answer






















      • For sed scripts which are contained in a ...sed script (used via -f option), the literal tab characters seem the only possibility to me. When editing this with vim, set noexpandtab is important.
        – Tobias
        Jan 11 at 11:46











      • Warning: Only use that "literal tab character" technique if you want your coworker to come back behind you and break your script later. Only use that tr technique if you want your coworker to stab you in the face when they read your script.
        – Bruno Bronosky
        Sep 6 at 18:50













      up vote
      49
      down vote



      accepted







      up vote
      49
      down vote



      accepted






      The syntax t for a tab character in sed is not standard. That escape is a GNU sed extension. You find a lot of examples online that use it because a lot of people use GNU sed (it's the sed implementation on non-embedded Linux). But OS X sed, like other *BSD sed, doesn't support t for tab and instead treats t as meaning backslash followed by t.



      There are many solutions, such as:




      • Use a literal tab character.



        sed -i.bak 's/ / /' file.txt



      • Use tr or printf to produce a tab character.



        sed -i.bak "s/$(printf 't')"/ /' file.txt
        sed -i.bak "s/$(echo a | tr 'a' 't')"/ /' file.txt



      • Use bash's string syntax allowing backslash escapes.



        sed -i.bak $'s/t/ /' file.txt


      • Use Perl, Python or Ruby. The Ruby snippet that you posted does work.






      share|improve this answer














      The syntax t for a tab character in sed is not standard. That escape is a GNU sed extension. You find a lot of examples online that use it because a lot of people use GNU sed (it's the sed implementation on non-embedded Linux). But OS X sed, like other *BSD sed, doesn't support t for tab and instead treats t as meaning backslash followed by t.



      There are many solutions, such as:




      • Use a literal tab character.



        sed -i.bak 's/ / /' file.txt



      • Use tr or printf to produce a tab character.



        sed -i.bak "s/$(printf 't')"/ /' file.txt
        sed -i.bak "s/$(echo a | tr 'a' 't')"/ /' file.txt



      • Use bash's string syntax allowing backslash escapes.



        sed -i.bak $'s/t/ /' file.txt


      • Use Perl, Python or Ruby. The Ruby snippet that you posted does work.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jul 19 '14 at 5:01









      Cristian Ciupitu

      2,01111520




      2,01111520










      answered Jul 18 '14 at 23:02









      Gilles

      516k12210271556




      516k12210271556











      • For sed scripts which are contained in a ...sed script (used via -f option), the literal tab characters seem the only possibility to me. When editing this with vim, set noexpandtab is important.
        – Tobias
        Jan 11 at 11:46











      • Warning: Only use that "literal tab character" technique if you want your coworker to come back behind you and break your script later. Only use that tr technique if you want your coworker to stab you in the face when they read your script.
        – Bruno Bronosky
        Sep 6 at 18:50

















      • For sed scripts which are contained in a ...sed script (used via -f option), the literal tab characters seem the only possibility to me. When editing this with vim, set noexpandtab is important.
        – Tobias
        Jan 11 at 11:46











      • Warning: Only use that "literal tab character" technique if you want your coworker to come back behind you and break your script later. Only use that tr technique if you want your coworker to stab you in the face when they read your script.
        – Bruno Bronosky
        Sep 6 at 18:50
















      For sed scripts which are contained in a ...sed script (used via -f option), the literal tab characters seem the only possibility to me. When editing this with vim, set noexpandtab is important.
      – Tobias
      Jan 11 at 11:46





      For sed scripts which are contained in a ...sed script (used via -f option), the literal tab characters seem the only possibility to me. When editing this with vim, set noexpandtab is important.
      – Tobias
      Jan 11 at 11:46













      Warning: Only use that "literal tab character" technique if you want your coworker to come back behind you and break your script later. Only use that tr technique if you want your coworker to stab you in the face when they read your script.
      – Bruno Bronosky
      Sep 6 at 18:50





      Warning: Only use that "literal tab character" technique if you want your coworker to come back behind you and break your script later. Only use that tr technique if you want your coworker to stab you in the face when they read your script.
      – Bruno Bronosky
      Sep 6 at 18:50













      up vote
      11
      down vote













      Use a Bash specific quoting which lets you use strings like in C, so that a real tab character is passed to sed, not an escape sequence:



      sed -i.bak -E $'s/t/ /' file.txt





      share|improve this answer
















      • 1




        Also called "ANSI-C" quoting if others want to look up more info about it.
        – wisbucky
        Jun 24 '16 at 23:28






      • 2




        Seems to work on any bourne shell, works on non-bash UNIXes as well. Doesn't work on csh-variants though.
        – jornane
        Nov 15 '16 at 14:43















      up vote
      11
      down vote













      Use a Bash specific quoting which lets you use strings like in C, so that a real tab character is passed to sed, not an escape sequence:



      sed -i.bak -E $'s/t/ /' file.txt





      share|improve this answer
















      • 1




        Also called "ANSI-C" quoting if others want to look up more info about it.
        – wisbucky
        Jun 24 '16 at 23:28






      • 2




        Seems to work on any bourne shell, works on non-bash UNIXes as well. Doesn't work on csh-variants though.
        – jornane
        Nov 15 '16 at 14:43













      up vote
      11
      down vote










      up vote
      11
      down vote









      Use a Bash specific quoting which lets you use strings like in C, so that a real tab character is passed to sed, not an escape sequence:



      sed -i.bak -E $'s/t/ /' file.txt





      share|improve this answer












      Use a Bash specific quoting which lets you use strings like in C, so that a real tab character is passed to sed, not an escape sequence:



      sed -i.bak -E $'s/t/ /' file.txt






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Jul 18 '14 at 16:31









      Cristian Ciupitu

      2,01111520




      2,01111520







      • 1




        Also called "ANSI-C" quoting if others want to look up more info about it.
        – wisbucky
        Jun 24 '16 at 23:28






      • 2




        Seems to work on any bourne shell, works on non-bash UNIXes as well. Doesn't work on csh-variants though.
        – jornane
        Nov 15 '16 at 14:43













      • 1




        Also called "ANSI-C" quoting if others want to look up more info about it.
        – wisbucky
        Jun 24 '16 at 23:28






      • 2




        Seems to work on any bourne shell, works on non-bash UNIXes as well. Doesn't work on csh-variants though.
        – jornane
        Nov 15 '16 at 14:43








      1




      1




      Also called "ANSI-C" quoting if others want to look up more info about it.
      – wisbucky
      Jun 24 '16 at 23:28




      Also called "ANSI-C" quoting if others want to look up more info about it.
      – wisbucky
      Jun 24 '16 at 23:28




      2




      2




      Seems to work on any bourne shell, works on non-bash UNIXes as well. Doesn't work on csh-variants though.
      – jornane
      Nov 15 '16 at 14:43





      Seems to work on any bourne shell, works on non-bash UNIXes as well. Doesn't work on csh-variants though.
      – jornane
      Nov 15 '16 at 14:43











      up vote
      1
      down vote













      sed -i $'s/t/ /g' file.txt 


      works for me on OS X and is the same command i use on linux all the time.






      share|improve this answer






















      • Note that this replaces all tabs on every row whereas the OP intends to only replace the first (judging from the command they use).
        – Kusalananda
        Sep 26 '17 at 19:33















      up vote
      1
      down vote













      sed -i $'s/t/ /g' file.txt 


      works for me on OS X and is the same command i use on linux all the time.






      share|improve this answer






















      • Note that this replaces all tabs on every row whereas the OP intends to only replace the first (judging from the command they use).
        – Kusalananda
        Sep 26 '17 at 19:33













      up vote
      1
      down vote










      up vote
      1
      down vote









      sed -i $'s/t/ /g' file.txt 


      works for me on OS X and is the same command i use on linux all the time.






      share|improve this answer














      sed -i $'s/t/ /g' file.txt 


      works for me on OS X and is the same command i use on linux all the time.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Oct 4 '16 at 18:31









      Tomasz

      8,57052761




      8,57052761










      answered Oct 4 '16 at 18:12









      user193377

      212




      212











      • Note that this replaces all tabs on every row whereas the OP intends to only replace the first (judging from the command they use).
        – Kusalananda
        Sep 26 '17 at 19:33

















      • Note that this replaces all tabs on every row whereas the OP intends to only replace the first (judging from the command they use).
        – Kusalananda
        Sep 26 '17 at 19:33
















      Note that this replaces all tabs on every row whereas the OP intends to only replace the first (judging from the command they use).
      – Kusalananda
      Sep 26 '17 at 19:33





      Note that this replaces all tabs on every row whereas the OP intends to only replace the first (judging from the command they use).
      – Kusalananda
      Sep 26 '17 at 19:33











      up vote
      0
      down vote













      If you want a more powerful sed (supporting t and more) than the one on OS X, install GNU sed.






      share|improve this answer




















      • Since it didn't work with Ruby either, I'm not sure why I would conclude that OS X's sed is the problem. Do you have a reason to believe that's the problem? I'd be happy to install GNU sed if I had reason to believe it would solve the problem, but it seems like I've pretty much ruled that out.
        – iconoclast
        Jul 18 '14 at 15:36










      • With Ruby, you'll have to use only one backslash: ruby -pe '$_.gsub!(/t/," ")' < file.txt
        – vinc17
        Jul 18 '14 at 15:45














      up vote
      0
      down vote













      If you want a more powerful sed (supporting t and more) than the one on OS X, install GNU sed.






      share|improve this answer




















      • Since it didn't work with Ruby either, I'm not sure why I would conclude that OS X's sed is the problem. Do you have a reason to believe that's the problem? I'd be happy to install GNU sed if I had reason to believe it would solve the problem, but it seems like I've pretty much ruled that out.
        – iconoclast
        Jul 18 '14 at 15:36










      • With Ruby, you'll have to use only one backslash: ruby -pe '$_.gsub!(/t/," ")' < file.txt
        – vinc17
        Jul 18 '14 at 15:45












      up vote
      0
      down vote










      up vote
      0
      down vote









      If you want a more powerful sed (supporting t and more) than the one on OS X, install GNU sed.






      share|improve this answer












      If you want a more powerful sed (supporting t and more) than the one on OS X, install GNU sed.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Jul 18 '14 at 15:31









      vinc17

      8,6791636




      8,6791636











      • Since it didn't work with Ruby either, I'm not sure why I would conclude that OS X's sed is the problem. Do you have a reason to believe that's the problem? I'd be happy to install GNU sed if I had reason to believe it would solve the problem, but it seems like I've pretty much ruled that out.
        – iconoclast
        Jul 18 '14 at 15:36










      • With Ruby, you'll have to use only one backslash: ruby -pe '$_.gsub!(/t/," ")' < file.txt
        – vinc17
        Jul 18 '14 at 15:45
















      • Since it didn't work with Ruby either, I'm not sure why I would conclude that OS X's sed is the problem. Do you have a reason to believe that's the problem? I'd be happy to install GNU sed if I had reason to believe it would solve the problem, but it seems like I've pretty much ruled that out.
        – iconoclast
        Jul 18 '14 at 15:36










      • With Ruby, you'll have to use only one backslash: ruby -pe '$_.gsub!(/t/," ")' < file.txt
        – vinc17
        Jul 18 '14 at 15:45















      Since it didn't work with Ruby either, I'm not sure why I would conclude that OS X's sed is the problem. Do you have a reason to believe that's the problem? I'd be happy to install GNU sed if I had reason to believe it would solve the problem, but it seems like I've pretty much ruled that out.
      – iconoclast
      Jul 18 '14 at 15:36




      Since it didn't work with Ruby either, I'm not sure why I would conclude that OS X's sed is the problem. Do you have a reason to believe that's the problem? I'd be happy to install GNU sed if I had reason to believe it would solve the problem, but it seems like I've pretty much ruled that out.
      – iconoclast
      Jul 18 '14 at 15:36












      With Ruby, you'll have to use only one backslash: ruby -pe '$_.gsub!(/t/," ")' < file.txt
      – vinc17
      Jul 18 '14 at 15:45




      With Ruby, you'll have to use only one backslash: ruby -pe '$_.gsub!(/t/," ")' < file.txt
      – vinc17
      Jul 18 '14 at 15:45










      up vote
      0
      down vote













      As noted, not all sed implementations support the notation of t as a horizontal tab.



      You can easily achieve your substitution with:



       perl -pi.old -e 'st+ g' file.txt


      This performs an in situ replacment which preserves your original file as "*.old". Perl allows alternate delimiters for the classic / making the expression much more readable (i.e. devoid of the "leaning toothpick" syndrome).



      The + says one or more repetitions of a tab character are to be replaced. The g modifier enables global replacements throughout the end of each line.






      share|improve this answer


























        up vote
        0
        down vote













        As noted, not all sed implementations support the notation of t as a horizontal tab.



        You can easily achieve your substitution with:



         perl -pi.old -e 'st+ g' file.txt


        This performs an in situ replacment which preserves your original file as "*.old". Perl allows alternate delimiters for the classic / making the expression much more readable (i.e. devoid of the "leaning toothpick" syndrome).



        The + says one or more repetitions of a tab character are to be replaced. The g modifier enables global replacements throughout the end of each line.






        share|improve this answer
























          up vote
          0
          down vote










          up vote
          0
          down vote









          As noted, not all sed implementations support the notation of t as a horizontal tab.



          You can easily achieve your substitution with:



           perl -pi.old -e 'st+ g' file.txt


          This performs an in situ replacment which preserves your original file as "*.old". Perl allows alternate delimiters for the classic / making the expression much more readable (i.e. devoid of the "leaning toothpick" syndrome).



          The + says one or more repetitions of a tab character are to be replaced. The g modifier enables global replacements throughout the end of each line.






          share|improve this answer














          As noted, not all sed implementations support the notation of t as a horizontal tab.



          You can easily achieve your substitution with:



           perl -pi.old -e 'st+ g' file.txt


          This performs an in situ replacment which preserves your original file as "*.old". Perl allows alternate delimiters for the classic / making the expression much more readable (i.e. devoid of the "leaning toothpick" syndrome).



          The + says one or more repetitions of a tab character are to be replaced. The g modifier enables global replacements throughout the end of each line.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jul 18 '14 at 15:48

























          answered Jul 18 '14 at 15:42









          JRFerguson

          9,34032329




          9,34032329




















              up vote
              0
              down vote













              sed -i.bak -E 's/\t/ /' file.txt




              share
























                up vote
                0
                down vote













                sed -i.bak -E 's/\t/ /' file.txt




                share






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  sed -i.bak -E 's/\t/ /' file.txt




                  share












                  sed -i.bak -E 's/\t/ /' file.txt





                  share











                  share


                  share










                  answered 3 mins ago









                  sjas

                  24837




                  24837




















                      up vote
                      -1
                      down vote













                      I'm surprised no one suggested the very simple solution of:
                      sed -i.bak -E 's/\t/ /' file.txt
                      That should do the trick.



                      You need to escape the escape (hence the 3 s) to allow sed to understand that you are trying to use a t character in the regular expression when everything is substituted...






                      share|improve this answer




















                      • Why three backslashes specifically?
                        – Michael Homer
                        Jan 14 '16 at 0:19






                      • 3




                        If I use GNU sed, one is enough, as no escaping is necessary. The problem is that BSD sed does not support this syntax for tabs.
                        – iconoclast
                        Jan 16 '16 at 16:43











                      • Does not work on my El Capitan.
                        – Franklin Yu
                        Jun 19 '16 at 6:46














                      up vote
                      -1
                      down vote













                      I'm surprised no one suggested the very simple solution of:
                      sed -i.bak -E 's/\t/ /' file.txt
                      That should do the trick.



                      You need to escape the escape (hence the 3 s) to allow sed to understand that you are trying to use a t character in the regular expression when everything is substituted...






                      share|improve this answer




















                      • Why three backslashes specifically?
                        – Michael Homer
                        Jan 14 '16 at 0:19






                      • 3




                        If I use GNU sed, one is enough, as no escaping is necessary. The problem is that BSD sed does not support this syntax for tabs.
                        – iconoclast
                        Jan 16 '16 at 16:43











                      • Does not work on my El Capitan.
                        – Franklin Yu
                        Jun 19 '16 at 6:46












                      up vote
                      -1
                      down vote










                      up vote
                      -1
                      down vote









                      I'm surprised no one suggested the very simple solution of:
                      sed -i.bak -E 's/\t/ /' file.txt
                      That should do the trick.



                      You need to escape the escape (hence the 3 s) to allow sed to understand that you are trying to use a t character in the regular expression when everything is substituted...






                      share|improve this answer












                      I'm surprised no one suggested the very simple solution of:
                      sed -i.bak -E 's/\t/ /' file.txt
                      That should do the trick.



                      You need to escape the escape (hence the 3 s) to allow sed to understand that you are trying to use a t character in the regular expression when everything is substituted...







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jan 13 '16 at 23:31









                      Vas

                      1




                      1











                      • Why three backslashes specifically?
                        – Michael Homer
                        Jan 14 '16 at 0:19






                      • 3




                        If I use GNU sed, one is enough, as no escaping is necessary. The problem is that BSD sed does not support this syntax for tabs.
                        – iconoclast
                        Jan 16 '16 at 16:43











                      • Does not work on my El Capitan.
                        – Franklin Yu
                        Jun 19 '16 at 6:46
















                      • Why three backslashes specifically?
                        – Michael Homer
                        Jan 14 '16 at 0:19






                      • 3




                        If I use GNU sed, one is enough, as no escaping is necessary. The problem is that BSD sed does not support this syntax for tabs.
                        – iconoclast
                        Jan 16 '16 at 16:43











                      • Does not work on my El Capitan.
                        – Franklin Yu
                        Jun 19 '16 at 6:46















                      Why three backslashes specifically?
                      – Michael Homer
                      Jan 14 '16 at 0:19




                      Why three backslashes specifically?
                      – Michael Homer
                      Jan 14 '16 at 0:19




                      3




                      3




                      If I use GNU sed, one is enough, as no escaping is necessary. The problem is that BSD sed does not support this syntax for tabs.
                      – iconoclast
                      Jan 16 '16 at 16:43





                      If I use GNU sed, one is enough, as no escaping is necessary. The problem is that BSD sed does not support this syntax for tabs.
                      – iconoclast
                      Jan 16 '16 at 16:43













                      Does not work on my El Capitan.
                      – Franklin Yu
                      Jun 19 '16 at 6:46




                      Does not work on my El Capitan.
                      – Franklin Yu
                      Jun 19 '16 at 6:46










                      up vote
                      -4
                      down vote













                      This worked for me.



                      sed -e 's/[t]/ /g'






                      share|improve this answer
















                      • 3




                        This is because you use GNU sed. This is not what the OP uses.
                        – Kusalananda
                        Sep 26 '17 at 19:34














                      up vote
                      -4
                      down vote













                      This worked for me.



                      sed -e 's/[t]/ /g'






                      share|improve this answer
















                      • 3




                        This is because you use GNU sed. This is not what the OP uses.
                        – Kusalananda
                        Sep 26 '17 at 19:34












                      up vote
                      -4
                      down vote










                      up vote
                      -4
                      down vote









                      This worked for me.



                      sed -e 's/[t]/ /g'






                      share|improve this answer












                      This worked for me.



                      sed -e 's/[t]/ /g'







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Sep 26 '17 at 19:27









                      RChristensen

                      1




                      1







                      • 3




                        This is because you use GNU sed. This is not what the OP uses.
                        – Kusalananda
                        Sep 26 '17 at 19:34












                      • 3




                        This is because you use GNU sed. This is not what the OP uses.
                        – Kusalananda
                        Sep 26 '17 at 19:34







                      3




                      3




                      This is because you use GNU sed. This is not what the OP uses.
                      – Kusalananda
                      Sep 26 '17 at 19:34




                      This is because you use GNU sed. This is not what the OP uses.
                      – Kusalananda
                      Sep 26 '17 at 19:34

















                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f145299%2fsimple-sed-replacement-of-tabs-mysteriously-failing%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay