Replace leading tabs and spaces with sed












0















I want to replace leading tabs and spaces with something like <TAB> and <SPACE> respectively.
But I couldn't figure out how to do it in a single pass of sed because tabs and spaces in the original file can be intermixed, so simply doing one replacement and than another doesn't work.



Input example (tabs shown as ^):



^^line with tabs
line with spaces
^ ^intermixed


Desired output:



<TAB><TAB>line with tabs
<SPACE><SPACE>line with spaces
<TAB><SPACE><TAB>intermixed









share|improve this question



























    0















    I want to replace leading tabs and spaces with something like <TAB> and <SPACE> respectively.
    But I couldn't figure out how to do it in a single pass of sed because tabs and spaces in the original file can be intermixed, so simply doing one replacement and than another doesn't work.



    Input example (tabs shown as ^):



    ^^line with tabs
    line with spaces
    ^ ^intermixed


    Desired output:



    <TAB><TAB>line with tabs
    <SPACE><SPACE>line with spaces
    <TAB><SPACE><TAB>intermixed









    share|improve this question

























      0












      0








      0








      I want to replace leading tabs and spaces with something like <TAB> and <SPACE> respectively.
      But I couldn't figure out how to do it in a single pass of sed because tabs and spaces in the original file can be intermixed, so simply doing one replacement and than another doesn't work.



      Input example (tabs shown as ^):



      ^^line with tabs
      line with spaces
      ^ ^intermixed


      Desired output:



      <TAB><TAB>line with tabs
      <SPACE><SPACE>line with spaces
      <TAB><SPACE><TAB>intermixed









      share|improve this question














      I want to replace leading tabs and spaces with something like <TAB> and <SPACE> respectively.
      But I couldn't figure out how to do it in a single pass of sed because tabs and spaces in the original file can be intermixed, so simply doing one replacement and than another doesn't work.



      Input example (tabs shown as ^):



      ^^line with tabs
      line with spaces
      ^ ^intermixed


      Desired output:



      <TAB><TAB>line with tabs
      <SPACE><SPACE>line with spaces
      <TAB><SPACE><TAB>intermixed






      bash sed tabs whitespace






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 12 at 21:41









      AmomumAmomum

      1034




      1034






















          2 Answers
          2






          active

          oldest

          votes


















          2














          I know you said you want to use sed, which is often a wonderful tool. But where there are choices and loops, I find that awk outshines it.





          #!/usr/bin/gawk -f
          { while (/^s/) {
          if (sub(/^ /,"")) printf "<space>";
          if (sub(/^t/,"")) printf "<tab>";
          }
          print;
          }


          If we create a file input.txt containing the input example, and name the script replace, it's run as follows, which produces the desired output.



          replace input.txt




          UPDATE:
          Oops. There's an infinite loop in that code. The sequence s matches [ tnrfv], so if there's a stray form feed, it'll spin forever. But [:blank:] matches just space and tab, so the second line should be this.



          { while (/^[[:blank:]]/) {





          share|improve this answer


























          • This is a really elegant solution.

            – zx485
            Feb 13 at 1:26











          • That is very readable solution! I guess there is no point using sed when awk solution looks simpler. Thank you!

            – Amomum
            Feb 13 at 20:21



















          0














          One solution with sed, it splits the line to separate tabs and spaces at the start from the rest of the line, to avoid replacing any tabs and spaces in the text.



          echo -e 'ttline withttabs
          line with spaces
          t tintermixed' | sed -r '

          # On the lines that start with tab or space.
          /^[t ]/ {

          # Put the whole line in the hold space.
          h

          # Delete all tabs and spaces at the start of line.
          s/^[t ]+//

          # Exchange pattern and hold spaces.
          # This saves the text part to the hold space and
          # bring back the original line to the pattern space.
          x

          # Now let in pattern space only tabs and spaces
          # at the start of line (the rest is on hold space).
          s/^([t ]+).*/1/

          # At least make the substitutions.
          s/t/<TAB>/g
          s/ /<SPACE>/g

          # Add a n (new line) at the end of pattern space,
          # then get the content of hold space and append it
          # to pattern space.
          G

          # Delete the extra n added above.
          s/n//
          }'
          <TAB><TAB>line with tabs
          <SPACE><SPACE>line with spaces
          <TAB><SPACE><TAB>intermixed





          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "3"
            };
            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: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            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%2fsuperuser.com%2fquestions%2f1405022%2freplace-leading-tabs-and-spaces-with-sed%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            I know you said you want to use sed, which is often a wonderful tool. But where there are choices and loops, I find that awk outshines it.





            #!/usr/bin/gawk -f
            { while (/^s/) {
            if (sub(/^ /,"")) printf "<space>";
            if (sub(/^t/,"")) printf "<tab>";
            }
            print;
            }


            If we create a file input.txt containing the input example, and name the script replace, it's run as follows, which produces the desired output.



            replace input.txt




            UPDATE:
            Oops. There's an infinite loop in that code. The sequence s matches [ tnrfv], so if there's a stray form feed, it'll spin forever. But [:blank:] matches just space and tab, so the second line should be this.



            { while (/^[[:blank:]]/) {





            share|improve this answer


























            • This is a really elegant solution.

              – zx485
              Feb 13 at 1:26











            • That is very readable solution! I guess there is no point using sed when awk solution looks simpler. Thank you!

              – Amomum
              Feb 13 at 20:21
















            2














            I know you said you want to use sed, which is often a wonderful tool. But where there are choices and loops, I find that awk outshines it.





            #!/usr/bin/gawk -f
            { while (/^s/) {
            if (sub(/^ /,"")) printf "<space>";
            if (sub(/^t/,"")) printf "<tab>";
            }
            print;
            }


            If we create a file input.txt containing the input example, and name the script replace, it's run as follows, which produces the desired output.



            replace input.txt




            UPDATE:
            Oops. There's an infinite loop in that code. The sequence s matches [ tnrfv], so if there's a stray form feed, it'll spin forever. But [:blank:] matches just space and tab, so the second line should be this.



            { while (/^[[:blank:]]/) {





            share|improve this answer


























            • This is a really elegant solution.

              – zx485
              Feb 13 at 1:26











            • That is very readable solution! I guess there is no point using sed when awk solution looks simpler. Thank you!

              – Amomum
              Feb 13 at 20:21














            2












            2








            2







            I know you said you want to use sed, which is often a wonderful tool. But where there are choices and loops, I find that awk outshines it.





            #!/usr/bin/gawk -f
            { while (/^s/) {
            if (sub(/^ /,"")) printf "<space>";
            if (sub(/^t/,"")) printf "<tab>";
            }
            print;
            }


            If we create a file input.txt containing the input example, and name the script replace, it's run as follows, which produces the desired output.



            replace input.txt




            UPDATE:
            Oops. There's an infinite loop in that code. The sequence s matches [ tnrfv], so if there's a stray form feed, it'll spin forever. But [:blank:] matches just space and tab, so the second line should be this.



            { while (/^[[:blank:]]/) {





            share|improve this answer















            I know you said you want to use sed, which is often a wonderful tool. But where there are choices and loops, I find that awk outshines it.





            #!/usr/bin/gawk -f
            { while (/^s/) {
            if (sub(/^ /,"")) printf "<space>";
            if (sub(/^t/,"")) printf "<tab>";
            }
            print;
            }


            If we create a file input.txt containing the input example, and name the script replace, it's run as follows, which produces the desired output.



            replace input.txt




            UPDATE:
            Oops. There's an infinite loop in that code. The sequence s matches [ tnrfv], so if there's a stray form feed, it'll spin forever. But [:blank:] matches just space and tab, so the second line should be this.



            { while (/^[[:blank:]]/) {






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 13 at 3:18

























            answered Feb 13 at 1:14









            Ken JacksonKen Jackson

            22112




            22112













            • This is a really elegant solution.

              – zx485
              Feb 13 at 1:26











            • That is very readable solution! I guess there is no point using sed when awk solution looks simpler. Thank you!

              – Amomum
              Feb 13 at 20:21



















            • This is a really elegant solution.

              – zx485
              Feb 13 at 1:26











            • That is very readable solution! I guess there is no point using sed when awk solution looks simpler. Thank you!

              – Amomum
              Feb 13 at 20:21

















            This is a really elegant solution.

            – zx485
            Feb 13 at 1:26





            This is a really elegant solution.

            – zx485
            Feb 13 at 1:26













            That is very readable solution! I guess there is no point using sed when awk solution looks simpler. Thank you!

            – Amomum
            Feb 13 at 20:21





            That is very readable solution! I guess there is no point using sed when awk solution looks simpler. Thank you!

            – Amomum
            Feb 13 at 20:21













            0














            One solution with sed, it splits the line to separate tabs and spaces at the start from the rest of the line, to avoid replacing any tabs and spaces in the text.



            echo -e 'ttline withttabs
            line with spaces
            t tintermixed' | sed -r '

            # On the lines that start with tab or space.
            /^[t ]/ {

            # Put the whole line in the hold space.
            h

            # Delete all tabs and spaces at the start of line.
            s/^[t ]+//

            # Exchange pattern and hold spaces.
            # This saves the text part to the hold space and
            # bring back the original line to the pattern space.
            x

            # Now let in pattern space only tabs and spaces
            # at the start of line (the rest is on hold space).
            s/^([t ]+).*/1/

            # At least make the substitutions.
            s/t/<TAB>/g
            s/ /<SPACE>/g

            # Add a n (new line) at the end of pattern space,
            # then get the content of hold space and append it
            # to pattern space.
            G

            # Delete the extra n added above.
            s/n//
            }'
            <TAB><TAB>line with tabs
            <SPACE><SPACE>line with spaces
            <TAB><SPACE><TAB>intermixed





            share|improve this answer




























              0














              One solution with sed, it splits the line to separate tabs and spaces at the start from the rest of the line, to avoid replacing any tabs and spaces in the text.



              echo -e 'ttline withttabs
              line with spaces
              t tintermixed' | sed -r '

              # On the lines that start with tab or space.
              /^[t ]/ {

              # Put the whole line in the hold space.
              h

              # Delete all tabs and spaces at the start of line.
              s/^[t ]+//

              # Exchange pattern and hold spaces.
              # This saves the text part to the hold space and
              # bring back the original line to the pattern space.
              x

              # Now let in pattern space only tabs and spaces
              # at the start of line (the rest is on hold space).
              s/^([t ]+).*/1/

              # At least make the substitutions.
              s/t/<TAB>/g
              s/ /<SPACE>/g

              # Add a n (new line) at the end of pattern space,
              # then get the content of hold space and append it
              # to pattern space.
              G

              # Delete the extra n added above.
              s/n//
              }'
              <TAB><TAB>line with tabs
              <SPACE><SPACE>line with spaces
              <TAB><SPACE><TAB>intermixed





              share|improve this answer


























                0












                0








                0







                One solution with sed, it splits the line to separate tabs and spaces at the start from the rest of the line, to avoid replacing any tabs and spaces in the text.



                echo -e 'ttline withttabs
                line with spaces
                t tintermixed' | sed -r '

                # On the lines that start with tab or space.
                /^[t ]/ {

                # Put the whole line in the hold space.
                h

                # Delete all tabs and spaces at the start of line.
                s/^[t ]+//

                # Exchange pattern and hold spaces.
                # This saves the text part to the hold space and
                # bring back the original line to the pattern space.
                x

                # Now let in pattern space only tabs and spaces
                # at the start of line (the rest is on hold space).
                s/^([t ]+).*/1/

                # At least make the substitutions.
                s/t/<TAB>/g
                s/ /<SPACE>/g

                # Add a n (new line) at the end of pattern space,
                # then get the content of hold space and append it
                # to pattern space.
                G

                # Delete the extra n added above.
                s/n//
                }'
                <TAB><TAB>line with tabs
                <SPACE><SPACE>line with spaces
                <TAB><SPACE><TAB>intermixed





                share|improve this answer













                One solution with sed, it splits the line to separate tabs and spaces at the start from the rest of the line, to avoid replacing any tabs and spaces in the text.



                echo -e 'ttline withttabs
                line with spaces
                t tintermixed' | sed -r '

                # On the lines that start with tab or space.
                /^[t ]/ {

                # Put the whole line in the hold space.
                h

                # Delete all tabs and spaces at the start of line.
                s/^[t ]+//

                # Exchange pattern and hold spaces.
                # This saves the text part to the hold space and
                # bring back the original line to the pattern space.
                x

                # Now let in pattern space only tabs and spaces
                # at the start of line (the rest is on hold space).
                s/^([t ]+).*/1/

                # At least make the substitutions.
                s/t/<TAB>/g
                s/ /<SPACE>/g

                # Add a n (new line) at the end of pattern space,
                # then get the content of hold space and append it
                # to pattern space.
                G

                # Delete the extra n added above.
                s/n//
                }'
                <TAB><TAB>line with tabs
                <SPACE><SPACE>line with spaces
                <TAB><SPACE><TAB>intermixed






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Feb 13 at 0:41









                PauloPaulo

                57428




                57428






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Super User!


                    • 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%2fsuperuser.com%2fquestions%2f1405022%2freplace-leading-tabs-and-spaces-with-sed%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

                    flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

                    Mangá

                     ⁒  ․,‪⁊‑⁙ ⁖, ⁇‒※‌, †,⁖‗‌⁝    ‾‸⁘,‖⁔⁣,⁂‾
”‑,‥–,‬ ,⁀‹⁋‴⁑ ‒ ,‴⁋”‼ ⁨,‷⁔„ ‰′,‐‚ ‥‡‎“‷⁃⁨⁅⁣,⁔
⁇‘⁔⁡⁏⁌⁡‿‶‏⁨ ⁣⁕⁖⁨⁩⁥‽⁀  ‴‬⁜‟ ⁃‣‧⁕‮ …‍⁨‴ ⁩,⁚⁖‫ ,‵ ⁀,‮⁝‣‣ ⁑  ⁂– ․, ‾‽ ‏⁁“⁗‸ ‾… ‹‡⁌⁎‸‘ ‡⁏⁌‪ ‵⁛ ‎⁨ ―⁦⁤⁄⁕