Simple sed replacement of tabs mysteriously failing












34














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


















34














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
















34












34








34


12





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

49.8k15132161




49.8k15132161










asked Jul 18 '14 at 14:15









iconoclast

3,76463768




3,76463768








  • 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












7 Answers
7






active

oldest

votes


















52














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 '18 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 '18 at 18:50












  • Is the second double-quotation mark misplaced in the second block of code? I had to move it to where the closing single-quote currently is.
    – Ellen Spertus
    Dec 24 '18 at 22:54



















13














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





















1














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 's{t+}{ }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































    1














    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





















    0














    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



















    -1














    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



















    -4














    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    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















    Required, but never shown

























    7 Answers
    7






    active

    oldest

    votes








    7 Answers
    7






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    52














    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 '18 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 '18 at 18:50












    • Is the second double-quotation mark misplaced in the second block of code? I had to move it to where the closing single-quote currently is.
      – Ellen Spertus
      Dec 24 '18 at 22:54
















    52














    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 '18 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 '18 at 18:50












    • Is the second double-quotation mark misplaced in the second block of code? I had to move it to where the closing single-quote currently is.
      – Ellen Spertus
      Dec 24 '18 at 22:54














    52












    52








    52






    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 Dec 24 '18 at 22:58

























    answered Jul 18 '14 at 23:02









    Gilles

    529k12810601586




    529k12810601586












    • 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 '18 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 '18 at 18:50












    • Is the second double-quotation mark misplaced in the second block of code? I had to move it to where the closing single-quote currently is.
      – Ellen Spertus
      Dec 24 '18 at 22:54


















    • 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 '18 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 '18 at 18:50












    • Is the second double-quotation mark misplaced in the second block of code? I had to move it to where the closing single-quote currently is.
      – Ellen Spertus
      Dec 24 '18 at 22:54
















    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 '18 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 '18 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 '18 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 '18 at 18:50














    Is the second double-quotation mark misplaced in the second block of code? I had to move it to where the closing single-quote currently is.
    – Ellen Spertus
    Dec 24 '18 at 22:54




    Is the second double-quotation mark misplaced in the second block of code? I had to move it to where the closing single-quote currently is.
    – Ellen Spertus
    Dec 24 '18 at 22:54













    13














    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


















    13














    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
















    13












    13








    13






    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,07911621




    2,07911621








    • 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













    1














    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 's{t+}{ }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




























      1














      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 's{t+}{ }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


























        1












        1








        1






        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 's{t+}{ }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 's{t+}{ }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,69232430




        9,69232430























            1














            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


















            1














            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
















            1












            1








            1






            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

            9,19352965




            9,19352965










            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













            0














            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
















            0














            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














            0












            0








            0






            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,8591736




            8,8591736












            • 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











            -1














            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
















            -1














            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














            -1












            -1








            -1






            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











            -4














            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
















            -4














            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














            -4












            -4








            -4






            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




















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            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















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Morgemoulin

            Scott Moir

            Souastre