Bash script: split word on each letter












14















How can I split a word's letters, with each letter in a separate line?



For example, given "StackOver"
I would like to see



S
t
a
c
k
O
v
e
r


I'm new to bash so I have no clue where to start.










share|improve this question





























    14















    How can I split a word's letters, with each letter in a separate line?



    For example, given "StackOver"
    I would like to see



    S
    t
    a
    c
    k
    O
    v
    e
    r


    I'm new to bash so I have no clue where to start.










    share|improve this question



























      14












      14








      14


      3






      How can I split a word's letters, with each letter in a separate line?



      For example, given "StackOver"
      I would like to see



      S
      t
      a
      c
      k
      O
      v
      e
      r


      I'm new to bash so I have no clue where to start.










      share|improve this question
















      How can I split a word's letters, with each letter in a separate line?



      For example, given "StackOver"
      I would like to see



      S
      t
      a
      c
      k
      O
      v
      e
      r


      I'm new to bash so I have no clue where to start.







      command-line split words






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 25 '16 at 12:30









      Jeff Schaller

      41.4k1056131




      41.4k1056131










      asked Jan 4 '16 at 23:41









      Sijaan HallakSijaan Hallak

      122111




      122111






















          15 Answers
          15






          active

          oldest

          votes


















          27














          I would use grep:



          $ grep -o . <<<"StackOver"
          S
          t
          a
          c
          k
          O
          v
          e
          r


          or sed:



          $ sed 's/./&n/g' <<<"StackOver"
          S
          t
          a
          c
          k
          O
          v
          e
          r


          And if empty space at the end is an issue:



          sed 's/B/&n/g' <<<"StackOver"


          All of that assuming GNU/Linux.






          share|improve this answer


























          • grep -o . <<< ¿¿¿ .. -o searches for the PATTERN provided right? and what it does here in your command?

            – Sijaan Hallak
            Jan 5 '16 at 0:06






          • 1





            @jimmij I cant find any help on what <<< really does! any help?

            – Sijaan Hallak
            Jan 5 '16 at 10:50








          • 3





            @SijaanHallak This is so called Here string, grosso modo equivalent of echo foo | ... just less typing. See tldp.org/LDP/abs/html/x17837.html

            – jimmij
            Jan 5 '16 at 11:02






          • 1





            @SijaanHallak change . to B (doesn't match on word boundary).

            – jimmij
            Jan 5 '16 at 17:40








          • 1





            @SijaanHallak - you can drop the second sed like: sed -et -e's/./n&/g;//D'

            – mikeserv
            Jan 6 '16 at 6:30



















          18














          You may want to break on grapheme clusters instead of characters if the intent is to print text vertically. For instance with a e with an acute accent:





          • With grapheme clusters (e with its acute accent would be one grapheme cluster):



            $ perl -CLAS -le 'for (@ARGV) {print for /X/g}' $'Steu301phane'
            S
            t
            é
            p
            h
            a
            n
            e


            (or grep -Po 'X' with GNU grep built with PCRE support)




          • With characters (here with GNU grep):



            $ printf '%sn' $'Steu301phane' | grep -o .
            S
            t
            e

            p
            h
            a
            n
            e



          • fold is meant to break on characters, but GNU fold doesn't support multi-byte characters, so it breaks on bytes instead:



            $ printf '%sn' $'Steu301phane' | fold -w 1
            S
            t
            e


            p
            h
            a
            n
            e



          On StackOver which only consists of ASCII characters (so one byte per character, one character per grapheme cluster), all three would give the same result.






          share|improve this answer


























          • I'm surprised grep -Po doesn't do what one would expect (like grep -P does).

            – jimmij
            Jan 5 '16 at 0:19













          • @jimmij, what do you mean? grep -Po . finds characters (and a combining acute accent following a newline character is invalid), and grep -Po 'X' finds graphem clusters for me. You may need a recent version of grep and/or PCRE for it to work properly (or try grep -Po '(*UTF8)X')

            – Stéphane Chazelas
            Jan 5 '16 at 0:23








          • 2





            @SijaanHallak These might be helpful: joelonsoftware.com/articles/Unicode.html, eev.ee/blog/2015/09/12/dark-corners-of-unicode

            – jpmc26
            Jan 5 '16 at 21:55



















          6














          If you have perl6 in your box:



          $ perl6 -e 'for @*ARGS -> $w { .say for $w.comb }' 'cường'       
          c
          ư

          n
          g


          work regardless of your locale.






          share|improve this answer































            6














            With many awk versions



            awk -F '' -v OFS='n' '{$1=$1};1' <<<'StackOver'





            share|improve this answer


























            • Great! But on my version of nAWK ("One True AWK") that doesn't work. However this does the trick: awk -v FS='' -v OFS='n' '{$1=$1};1' (wondering if that's more portable since -F '' might yield the ERE: //)

              – eruve
              Feb 4 at 6:48





















            4














            The below will be generic:



            $ awk -F '' 
            'BEGIN { RS = ""; OFS = "n"} {for (i=1;i<=NF;i++) $i = $i; print }' <file_name>





            share|improve this answer

































              4














              echo StackOver | sed -e 's/./&n/g'
              S
              t
              a
              c
              k
              O
              v
              e
              r





              share|improve this answer


























              • This won't help as it prints a new line at the end

                – Sijaan Hallak
                Jan 5 '16 at 10:56



















              4














              Since you specifically asked for an answer in bash, here's a way to do it in pure bash:



              while read -rn1; do echo "$REPLY" ; done <<< "StackOver"


              Note that this will catch the newline at the end of the "here document". If you want to avoid that, but still iterate over the characters with a bash loop, use printf to avoid the newline.



              printf StackOver | while read -rn1; do echo "$REPLY" ; done





              share|improve this answer

































                4














                Also Python 2 can be used from the command line:



                python <<< "for x in 'StackOver':
                print x"


                or:



                echo "for x in 'StackOver':
                print x" | python


                or (as commented by 1_CR) with Python 3:



                python3 -c "print(*'StackOver',sep='n')"





                share|improve this answer

































                  4














                  You can use the fold (1) command. It is more efficient than grep and sed.



                  $ time grep -o . <bigfile >/dev/null

                  real 0m3.868s
                  user 0m3.784s
                  sys 0m0.056s
                  $ time fold -b1 <bigfile >/dev/null

                  real 0m0.555s
                  user 0m0.528s
                  sys 0m0.016s
                  $


                  One significant difference is that fold will reproduce empty lines in the output:



                  $ grep -o . <(printf "AnBnnCnnnDn")
                  A
                  B
                  C
                  D
                  $ fold -b1 <(printf "AnBnnCnnnDn")
                  A
                  B

                  C


                  D
                  $





                  share|improve this answer

































                    3














                    You can handle multibyte characters like:



                    <input 
                    dd cbs=1 obs=2 conv=unblock |
                    sed -e:c -e '/^.*$/!N;s/n//;tc'


                    Which can be pretty handy when you're working with live input because there's no buffering there and a character is printed as soon it is whole.






                    share|improve this answer


























                    • NP, should we add a note about the locale?

                      – cuonglm
                      Jan 5 '16 at 9:35











                    • Does not work for combining characters like Stéphane Chazelas answer, but with proper normalization this should not matter.

                      – kay
                      Jan 5 '16 at 13:06











                    • @Kay - it's works for combining characters if you want it to - that's what sed scripts are for. i'm not likely to write one right about now - im pretty sleepy. it's really useful, though, when reading a terminal.

                      – mikeserv
                      Jan 5 '16 at 14:30













                    • @cuonglm - if you like. it should just work for the locale, given a sane libc, though.

                      – mikeserv
                      Jan 5 '16 at 14:33











                    • Note that dd will break multibyte characters, so the output will not be text anymore so the behaviour of sed will be unspecified as per POSIX.

                      – Stéphane Chazelas
                      Jan 5 '16 at 22:09



















                    3














                    You may use word boundaries also..



                    $ perl -pe 's/(?<=.)(B|b)(?=.)/n/g' <<< "StackOver"
                    S
                    t
                    a
                    c
                    k
                    O
                    v
                    e
                    r





                    share|improve this answer































                      1














                      In bash:



                      This works with any text and with only bash internals (no external utility called), so, should be fast on very short strings.



                      str="Stéphane áàéèëêếe"

                      [[ $str =~ ${str//?/(.)} ]]
                      (set -- "${BASH_REMATCH[@]:1}"; IFS=$'n'; echo "$*")


                      Output:



                      S
                      t
                      é
                      p
                      h
                      a
                      n
                      e

                      á
                      à
                      é
                      è
                      ë
                      ê
                      ế
                      e


                      If it is ok to change IFS and to change the positional parameters, you can also avoid the sub-shell call:



                      str="Stéphane áàéèëêếe"
                      [[ $str =~ ${str//?/(.)} ]]
                      set -- "${BASH_REMATCH[@]:1}"
                      IFS=$'n'
                      echo "$*"





                      share|improve this answer































                        1














                        s=stackoverflow;

                        $ time echo $s | fold -w1
                        s
                        t
                        a
                        c
                        k
                        o
                        v
                        e
                        r

                        real 0m0.014s
                        user 0m0.000s
                        sys 0m0.004s


                        updates
                        here is the hacky|fastest|pureBashBased way !



                        $ time eval eval printf '%s\\n' \${s:{0..$((${#s}-1))}:1}
                        s
                        t
                        a
                        c
                        k
                        o
                        v
                        e
                        r

                        real 0m0.001s
                        user 0m0.000s
                        sys 0m0.000s


                        for more awesomeness



                        function foldh () 
                        {
                        if (($#)); then
                        local s="$@";
                        eval eval printf '%s\\n' \"\${s:{0..$((${#s}-1))}:1}\";
                        else
                        while read s; do
                        eval eval printf '%s\\n' \"\${s:{0..$((${#s}-1))}:1}\";
                        done;
                        fi
                        }
                        function foldv ()
                        {
                        if (($#)); then
                        local s="$@";
                        eval eval echo \"\${s:{0..$((${#s}-1))}:1}\";
                        else
                        while read s; do
                        eval eval echo \"\${s:{0..$((${#s}-1))}:1}\";
                        done;
                        fi
                        }





                        share|improve this answer


























                        • Will this ever give different results to fold -b1 ?

                          – JigglyNaga
                          Jul 25 '16 at 12:10











                        • since each byte have a width=1 the result will be the same !

                          – Jonas
                          Jul 25 '16 at 12:30






                        • 1





                          So how is this not a duplicate of the earlier answer?

                          – JigglyNaga
                          Jul 25 '16 at 13:17











                        • because it shows tha same cmd with different argyment , and that is nice to know .

                          – Jonas
                          Jul 25 '16 at 13:45



















                        1














                        read -a var <<< $(echo "$yourWordhere" | grep -o "." | tr 'n' ' ')


                        this will split your word and store it in array var.






                        share|improve this answer

































                          0














                          for x in $(echo "$yourWordhere" | grep -o '.')
                          do
                          code to perform operation on individual character $x of your word
                          done





                          share|improve this answer

























                            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%2f253279%2fbash-script-split-word-on-each-letter%23new-answer', 'question_page');
                            }
                            );

                            Post as a guest















                            Required, but never shown

























                            15 Answers
                            15






                            active

                            oldest

                            votes








                            15 Answers
                            15






                            active

                            oldest

                            votes









                            active

                            oldest

                            votes






                            active

                            oldest

                            votes









                            27














                            I would use grep:



                            $ grep -o . <<<"StackOver"
                            S
                            t
                            a
                            c
                            k
                            O
                            v
                            e
                            r


                            or sed:



                            $ sed 's/./&n/g' <<<"StackOver"
                            S
                            t
                            a
                            c
                            k
                            O
                            v
                            e
                            r


                            And if empty space at the end is an issue:



                            sed 's/B/&n/g' <<<"StackOver"


                            All of that assuming GNU/Linux.






                            share|improve this answer


























                            • grep -o . <<< ¿¿¿ .. -o searches for the PATTERN provided right? and what it does here in your command?

                              – Sijaan Hallak
                              Jan 5 '16 at 0:06






                            • 1





                              @jimmij I cant find any help on what <<< really does! any help?

                              – Sijaan Hallak
                              Jan 5 '16 at 10:50








                            • 3





                              @SijaanHallak This is so called Here string, grosso modo equivalent of echo foo | ... just less typing. See tldp.org/LDP/abs/html/x17837.html

                              – jimmij
                              Jan 5 '16 at 11:02






                            • 1





                              @SijaanHallak change . to B (doesn't match on word boundary).

                              – jimmij
                              Jan 5 '16 at 17:40








                            • 1





                              @SijaanHallak - you can drop the second sed like: sed -et -e's/./n&/g;//D'

                              – mikeserv
                              Jan 6 '16 at 6:30
















                            27














                            I would use grep:



                            $ grep -o . <<<"StackOver"
                            S
                            t
                            a
                            c
                            k
                            O
                            v
                            e
                            r


                            or sed:



                            $ sed 's/./&n/g' <<<"StackOver"
                            S
                            t
                            a
                            c
                            k
                            O
                            v
                            e
                            r


                            And if empty space at the end is an issue:



                            sed 's/B/&n/g' <<<"StackOver"


                            All of that assuming GNU/Linux.






                            share|improve this answer


























                            • grep -o . <<< ¿¿¿ .. -o searches for the PATTERN provided right? and what it does here in your command?

                              – Sijaan Hallak
                              Jan 5 '16 at 0:06






                            • 1





                              @jimmij I cant find any help on what <<< really does! any help?

                              – Sijaan Hallak
                              Jan 5 '16 at 10:50








                            • 3





                              @SijaanHallak This is so called Here string, grosso modo equivalent of echo foo | ... just less typing. See tldp.org/LDP/abs/html/x17837.html

                              – jimmij
                              Jan 5 '16 at 11:02






                            • 1





                              @SijaanHallak change . to B (doesn't match on word boundary).

                              – jimmij
                              Jan 5 '16 at 17:40








                            • 1





                              @SijaanHallak - you can drop the second sed like: sed -et -e's/./n&/g;//D'

                              – mikeserv
                              Jan 6 '16 at 6:30














                            27












                            27








                            27







                            I would use grep:



                            $ grep -o . <<<"StackOver"
                            S
                            t
                            a
                            c
                            k
                            O
                            v
                            e
                            r


                            or sed:



                            $ sed 's/./&n/g' <<<"StackOver"
                            S
                            t
                            a
                            c
                            k
                            O
                            v
                            e
                            r


                            And if empty space at the end is an issue:



                            sed 's/B/&n/g' <<<"StackOver"


                            All of that assuming GNU/Linux.






                            share|improve this answer















                            I would use grep:



                            $ grep -o . <<<"StackOver"
                            S
                            t
                            a
                            c
                            k
                            O
                            v
                            e
                            r


                            or sed:



                            $ sed 's/./&n/g' <<<"StackOver"
                            S
                            t
                            a
                            c
                            k
                            O
                            v
                            e
                            r


                            And if empty space at the end is an issue:



                            sed 's/B/&n/g' <<<"StackOver"


                            All of that assuming GNU/Linux.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jan 5 '16 at 22:07

























                            answered Jan 4 '16 at 23:49









                            jimmijjimmij

                            31.4k872108




                            31.4k872108













                            • grep -o . <<< ¿¿¿ .. -o searches for the PATTERN provided right? and what it does here in your command?

                              – Sijaan Hallak
                              Jan 5 '16 at 0:06






                            • 1





                              @jimmij I cant find any help on what <<< really does! any help?

                              – Sijaan Hallak
                              Jan 5 '16 at 10:50








                            • 3





                              @SijaanHallak This is so called Here string, grosso modo equivalent of echo foo | ... just less typing. See tldp.org/LDP/abs/html/x17837.html

                              – jimmij
                              Jan 5 '16 at 11:02






                            • 1





                              @SijaanHallak change . to B (doesn't match on word boundary).

                              – jimmij
                              Jan 5 '16 at 17:40








                            • 1





                              @SijaanHallak - you can drop the second sed like: sed -et -e's/./n&/g;//D'

                              – mikeserv
                              Jan 6 '16 at 6:30



















                            • grep -o . <<< ¿¿¿ .. -o searches for the PATTERN provided right? and what it does here in your command?

                              – Sijaan Hallak
                              Jan 5 '16 at 0:06






                            • 1





                              @jimmij I cant find any help on what <<< really does! any help?

                              – Sijaan Hallak
                              Jan 5 '16 at 10:50








                            • 3





                              @SijaanHallak This is so called Here string, grosso modo equivalent of echo foo | ... just less typing. See tldp.org/LDP/abs/html/x17837.html

                              – jimmij
                              Jan 5 '16 at 11:02






                            • 1





                              @SijaanHallak change . to B (doesn't match on word boundary).

                              – jimmij
                              Jan 5 '16 at 17:40








                            • 1





                              @SijaanHallak - you can drop the second sed like: sed -et -e's/./n&/g;//D'

                              – mikeserv
                              Jan 6 '16 at 6:30

















                            grep -o . <<< ¿¿¿ .. -o searches for the PATTERN provided right? and what it does here in your command?

                            – Sijaan Hallak
                            Jan 5 '16 at 0:06





                            grep -o . <<< ¿¿¿ .. -o searches for the PATTERN provided right? and what it does here in your command?

                            – Sijaan Hallak
                            Jan 5 '16 at 0:06




                            1




                            1





                            @jimmij I cant find any help on what <<< really does! any help?

                            – Sijaan Hallak
                            Jan 5 '16 at 10:50







                            @jimmij I cant find any help on what <<< really does! any help?

                            – Sijaan Hallak
                            Jan 5 '16 at 10:50






                            3




                            3





                            @SijaanHallak This is so called Here string, grosso modo equivalent of echo foo | ... just less typing. See tldp.org/LDP/abs/html/x17837.html

                            – jimmij
                            Jan 5 '16 at 11:02





                            @SijaanHallak This is so called Here string, grosso modo equivalent of echo foo | ... just less typing. See tldp.org/LDP/abs/html/x17837.html

                            – jimmij
                            Jan 5 '16 at 11:02




                            1




                            1





                            @SijaanHallak change . to B (doesn't match on word boundary).

                            – jimmij
                            Jan 5 '16 at 17:40







                            @SijaanHallak change . to B (doesn't match on word boundary).

                            – jimmij
                            Jan 5 '16 at 17:40






                            1




                            1





                            @SijaanHallak - you can drop the second sed like: sed -et -e's/./n&/g;//D'

                            – mikeserv
                            Jan 6 '16 at 6:30





                            @SijaanHallak - you can drop the second sed like: sed -et -e's/./n&/g;//D'

                            – mikeserv
                            Jan 6 '16 at 6:30













                            18














                            You may want to break on grapheme clusters instead of characters if the intent is to print text vertically. For instance with a e with an acute accent:





                            • With grapheme clusters (e with its acute accent would be one grapheme cluster):



                              $ perl -CLAS -le 'for (@ARGV) {print for /X/g}' $'Steu301phane'
                              S
                              t
                              é
                              p
                              h
                              a
                              n
                              e


                              (or grep -Po 'X' with GNU grep built with PCRE support)




                            • With characters (here with GNU grep):



                              $ printf '%sn' $'Steu301phane' | grep -o .
                              S
                              t
                              e

                              p
                              h
                              a
                              n
                              e



                            • fold is meant to break on characters, but GNU fold doesn't support multi-byte characters, so it breaks on bytes instead:



                              $ printf '%sn' $'Steu301phane' | fold -w 1
                              S
                              t
                              e


                              p
                              h
                              a
                              n
                              e



                            On StackOver which only consists of ASCII characters (so one byte per character, one character per grapheme cluster), all three would give the same result.






                            share|improve this answer


























                            • I'm surprised grep -Po doesn't do what one would expect (like grep -P does).

                              – jimmij
                              Jan 5 '16 at 0:19













                            • @jimmij, what do you mean? grep -Po . finds characters (and a combining acute accent following a newline character is invalid), and grep -Po 'X' finds graphem clusters for me. You may need a recent version of grep and/or PCRE for it to work properly (or try grep -Po '(*UTF8)X')

                              – Stéphane Chazelas
                              Jan 5 '16 at 0:23








                            • 2





                              @SijaanHallak These might be helpful: joelonsoftware.com/articles/Unicode.html, eev.ee/blog/2015/09/12/dark-corners-of-unicode

                              – jpmc26
                              Jan 5 '16 at 21:55
















                            18














                            You may want to break on grapheme clusters instead of characters if the intent is to print text vertically. For instance with a e with an acute accent:





                            • With grapheme clusters (e with its acute accent would be one grapheme cluster):



                              $ perl -CLAS -le 'for (@ARGV) {print for /X/g}' $'Steu301phane'
                              S
                              t
                              é
                              p
                              h
                              a
                              n
                              e


                              (or grep -Po 'X' with GNU grep built with PCRE support)




                            • With characters (here with GNU grep):



                              $ printf '%sn' $'Steu301phane' | grep -o .
                              S
                              t
                              e

                              p
                              h
                              a
                              n
                              e



                            • fold is meant to break on characters, but GNU fold doesn't support multi-byte characters, so it breaks on bytes instead:



                              $ printf '%sn' $'Steu301phane' | fold -w 1
                              S
                              t
                              e


                              p
                              h
                              a
                              n
                              e



                            On StackOver which only consists of ASCII characters (so one byte per character, one character per grapheme cluster), all three would give the same result.






                            share|improve this answer


























                            • I'm surprised grep -Po doesn't do what one would expect (like grep -P does).

                              – jimmij
                              Jan 5 '16 at 0:19













                            • @jimmij, what do you mean? grep -Po . finds characters (and a combining acute accent following a newline character is invalid), and grep -Po 'X' finds graphem clusters for me. You may need a recent version of grep and/or PCRE for it to work properly (or try grep -Po '(*UTF8)X')

                              – Stéphane Chazelas
                              Jan 5 '16 at 0:23








                            • 2





                              @SijaanHallak These might be helpful: joelonsoftware.com/articles/Unicode.html, eev.ee/blog/2015/09/12/dark-corners-of-unicode

                              – jpmc26
                              Jan 5 '16 at 21:55














                            18












                            18








                            18







                            You may want to break on grapheme clusters instead of characters if the intent is to print text vertically. For instance with a e with an acute accent:





                            • With grapheme clusters (e with its acute accent would be one grapheme cluster):



                              $ perl -CLAS -le 'for (@ARGV) {print for /X/g}' $'Steu301phane'
                              S
                              t
                              é
                              p
                              h
                              a
                              n
                              e


                              (or grep -Po 'X' with GNU grep built with PCRE support)




                            • With characters (here with GNU grep):



                              $ printf '%sn' $'Steu301phane' | grep -o .
                              S
                              t
                              e

                              p
                              h
                              a
                              n
                              e



                            • fold is meant to break on characters, but GNU fold doesn't support multi-byte characters, so it breaks on bytes instead:



                              $ printf '%sn' $'Steu301phane' | fold -w 1
                              S
                              t
                              e


                              p
                              h
                              a
                              n
                              e



                            On StackOver which only consists of ASCII characters (so one byte per character, one character per grapheme cluster), all three would give the same result.






                            share|improve this answer















                            You may want to break on grapheme clusters instead of characters if the intent is to print text vertically. For instance with a e with an acute accent:





                            • With grapheme clusters (e with its acute accent would be one grapheme cluster):



                              $ perl -CLAS -le 'for (@ARGV) {print for /X/g}' $'Steu301phane'
                              S
                              t
                              é
                              p
                              h
                              a
                              n
                              e


                              (or grep -Po 'X' with GNU grep built with PCRE support)




                            • With characters (here with GNU grep):



                              $ printf '%sn' $'Steu301phane' | grep -o .
                              S
                              t
                              e

                              p
                              h
                              a
                              n
                              e



                            • fold is meant to break on characters, but GNU fold doesn't support multi-byte characters, so it breaks on bytes instead:



                              $ printf '%sn' $'Steu301phane' | fold -w 1
                              S
                              t
                              e


                              p
                              h
                              a
                              n
                              e



                            On StackOver which only consists of ASCII characters (so one byte per character, one character per grapheme cluster), all three would give the same result.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Aug 31 '18 at 6:34

























                            answered Jan 5 '16 at 0:07









                            Stéphane ChazelasStéphane Chazelas

                            305k57574929




                            305k57574929













                            • I'm surprised grep -Po doesn't do what one would expect (like grep -P does).

                              – jimmij
                              Jan 5 '16 at 0:19













                            • @jimmij, what do you mean? grep -Po . finds characters (and a combining acute accent following a newline character is invalid), and grep -Po 'X' finds graphem clusters for me. You may need a recent version of grep and/or PCRE for it to work properly (or try grep -Po '(*UTF8)X')

                              – Stéphane Chazelas
                              Jan 5 '16 at 0:23








                            • 2





                              @SijaanHallak These might be helpful: joelonsoftware.com/articles/Unicode.html, eev.ee/blog/2015/09/12/dark-corners-of-unicode

                              – jpmc26
                              Jan 5 '16 at 21:55



















                            • I'm surprised grep -Po doesn't do what one would expect (like grep -P does).

                              – jimmij
                              Jan 5 '16 at 0:19













                            • @jimmij, what do you mean? grep -Po . finds characters (and a combining acute accent following a newline character is invalid), and grep -Po 'X' finds graphem clusters for me. You may need a recent version of grep and/or PCRE for it to work properly (or try grep -Po '(*UTF8)X')

                              – Stéphane Chazelas
                              Jan 5 '16 at 0:23








                            • 2





                              @SijaanHallak These might be helpful: joelonsoftware.com/articles/Unicode.html, eev.ee/blog/2015/09/12/dark-corners-of-unicode

                              – jpmc26
                              Jan 5 '16 at 21:55

















                            I'm surprised grep -Po doesn't do what one would expect (like grep -P does).

                            – jimmij
                            Jan 5 '16 at 0:19







                            I'm surprised grep -Po doesn't do what one would expect (like grep -P does).

                            – jimmij
                            Jan 5 '16 at 0:19















                            @jimmij, what do you mean? grep -Po . finds characters (and a combining acute accent following a newline character is invalid), and grep -Po 'X' finds graphem clusters for me. You may need a recent version of grep and/or PCRE for it to work properly (or try grep -Po '(*UTF8)X')

                            – Stéphane Chazelas
                            Jan 5 '16 at 0:23







                            @jimmij, what do you mean? grep -Po . finds characters (and a combining acute accent following a newline character is invalid), and grep -Po 'X' finds graphem clusters for me. You may need a recent version of grep and/or PCRE for it to work properly (or try grep -Po '(*UTF8)X')

                            – Stéphane Chazelas
                            Jan 5 '16 at 0:23






                            2




                            2





                            @SijaanHallak These might be helpful: joelonsoftware.com/articles/Unicode.html, eev.ee/blog/2015/09/12/dark-corners-of-unicode

                            – jpmc26
                            Jan 5 '16 at 21:55





                            @SijaanHallak These might be helpful: joelonsoftware.com/articles/Unicode.html, eev.ee/blog/2015/09/12/dark-corners-of-unicode

                            – jpmc26
                            Jan 5 '16 at 21:55











                            6














                            If you have perl6 in your box:



                            $ perl6 -e 'for @*ARGS -> $w { .say for $w.comb }' 'cường'       
                            c
                            ư

                            n
                            g


                            work regardless of your locale.






                            share|improve this answer




























                              6














                              If you have perl6 in your box:



                              $ perl6 -e 'for @*ARGS -> $w { .say for $w.comb }' 'cường'       
                              c
                              ư

                              n
                              g


                              work regardless of your locale.






                              share|improve this answer


























                                6












                                6








                                6







                                If you have perl6 in your box:



                                $ perl6 -e 'for @*ARGS -> $w { .say for $w.comb }' 'cường'       
                                c
                                ư

                                n
                                g


                                work regardless of your locale.






                                share|improve this answer













                                If you have perl6 in your box:



                                $ perl6 -e 'for @*ARGS -> $w { .say for $w.comb }' 'cường'       
                                c
                                ư

                                n
                                g


                                work regardless of your locale.







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Jan 5 '16 at 1:42









                                cuonglmcuonglm

                                104k24203302




                                104k24203302























                                    6














                                    With many awk versions



                                    awk -F '' -v OFS='n' '{$1=$1};1' <<<'StackOver'





                                    share|improve this answer


























                                    • Great! But on my version of nAWK ("One True AWK") that doesn't work. However this does the trick: awk -v FS='' -v OFS='n' '{$1=$1};1' (wondering if that's more portable since -F '' might yield the ERE: //)

                                      – eruve
                                      Feb 4 at 6:48


















                                    6














                                    With many awk versions



                                    awk -F '' -v OFS='n' '{$1=$1};1' <<<'StackOver'





                                    share|improve this answer


























                                    • Great! But on my version of nAWK ("One True AWK") that doesn't work. However this does the trick: awk -v FS='' -v OFS='n' '{$1=$1};1' (wondering if that's more portable since -F '' might yield the ERE: //)

                                      – eruve
                                      Feb 4 at 6:48
















                                    6












                                    6








                                    6







                                    With many awk versions



                                    awk -F '' -v OFS='n' '{$1=$1};1' <<<'StackOver'





                                    share|improve this answer















                                    With many awk versions



                                    awk -F '' -v OFS='n' '{$1=$1};1' <<<'StackOver'






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Jan 5 '16 at 22:11









                                    Stéphane Chazelas

                                    305k57574929




                                    305k57574929










                                    answered Jan 5 '16 at 4:16









                                    iruvariruvar

                                    11.9k62960




                                    11.9k62960













                                    • Great! But on my version of nAWK ("One True AWK") that doesn't work. However this does the trick: awk -v FS='' -v OFS='n' '{$1=$1};1' (wondering if that's more portable since -F '' might yield the ERE: //)

                                      – eruve
                                      Feb 4 at 6:48





















                                    • Great! But on my version of nAWK ("One True AWK") that doesn't work. However this does the trick: awk -v FS='' -v OFS='n' '{$1=$1};1' (wondering if that's more portable since -F '' might yield the ERE: //)

                                      – eruve
                                      Feb 4 at 6:48



















                                    Great! But on my version of nAWK ("One True AWK") that doesn't work. However this does the trick: awk -v FS='' -v OFS='n' '{$1=$1};1' (wondering if that's more portable since -F '' might yield the ERE: //)

                                    – eruve
                                    Feb 4 at 6:48







                                    Great! But on my version of nAWK ("One True AWK") that doesn't work. However this does the trick: awk -v FS='' -v OFS='n' '{$1=$1};1' (wondering if that's more portable since -F '' might yield the ERE: //)

                                    – eruve
                                    Feb 4 at 6:48













                                    4














                                    The below will be generic:



                                    $ awk -F '' 
                                    'BEGIN { RS = ""; OFS = "n"} {for (i=1;i<=NF;i++) $i = $i; print }' <file_name>





                                    share|improve this answer






























                                      4














                                      The below will be generic:



                                      $ awk -F '' 
                                      'BEGIN { RS = ""; OFS = "n"} {for (i=1;i<=NF;i++) $i = $i; print }' <file_name>





                                      share|improve this answer




























                                        4












                                        4








                                        4







                                        The below will be generic:



                                        $ awk -F '' 
                                        'BEGIN { RS = ""; OFS = "n"} {for (i=1;i<=NF;i++) $i = $i; print }' <file_name>





                                        share|improve this answer















                                        The below will be generic:



                                        $ awk -F '' 
                                        'BEGIN { RS = ""; OFS = "n"} {for (i=1;i<=NF;i++) $i = $i; print }' <file_name>






                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Jan 5 '16 at 7:24









                                        slm

                                        251k66528685




                                        251k66528685










                                        answered Jan 5 '16 at 6:56









                                        user150073user150073

                                        411




                                        411























                                            4














                                            echo StackOver | sed -e 's/./&n/g'
                                            S
                                            t
                                            a
                                            c
                                            k
                                            O
                                            v
                                            e
                                            r





                                            share|improve this answer


























                                            • This won't help as it prints a new line at the end

                                              – Sijaan Hallak
                                              Jan 5 '16 at 10:56
















                                            4














                                            echo StackOver | sed -e 's/./&n/g'
                                            S
                                            t
                                            a
                                            c
                                            k
                                            O
                                            v
                                            e
                                            r





                                            share|improve this answer


























                                            • This won't help as it prints a new line at the end

                                              – Sijaan Hallak
                                              Jan 5 '16 at 10:56














                                            4












                                            4








                                            4







                                            echo StackOver | sed -e 's/./&n/g'
                                            S
                                            t
                                            a
                                            c
                                            k
                                            O
                                            v
                                            e
                                            r





                                            share|improve this answer















                                            echo StackOver | sed -e 's/./&n/g'
                                            S
                                            t
                                            a
                                            c
                                            k
                                            O
                                            v
                                            e
                                            r






                                            share|improve this answer














                                            share|improve this answer



                                            share|improve this answer








                                            edited Jan 5 '16 at 14:37









                                            mikeserv

                                            45.6k668157




                                            45.6k668157










                                            answered Jan 5 '16 at 4:11









                                            hendersonhenderson

                                            391




                                            391













                                            • This won't help as it prints a new line at the end

                                              – Sijaan Hallak
                                              Jan 5 '16 at 10:56



















                                            • This won't help as it prints a new line at the end

                                              – Sijaan Hallak
                                              Jan 5 '16 at 10:56

















                                            This won't help as it prints a new line at the end

                                            – Sijaan Hallak
                                            Jan 5 '16 at 10:56





                                            This won't help as it prints a new line at the end

                                            – Sijaan Hallak
                                            Jan 5 '16 at 10:56











                                            4














                                            Since you specifically asked for an answer in bash, here's a way to do it in pure bash:



                                            while read -rn1; do echo "$REPLY" ; done <<< "StackOver"


                                            Note that this will catch the newline at the end of the "here document". If you want to avoid that, but still iterate over the characters with a bash loop, use printf to avoid the newline.



                                            printf StackOver | while read -rn1; do echo "$REPLY" ; done





                                            share|improve this answer






























                                              4














                                              Since you specifically asked for an answer in bash, here's a way to do it in pure bash:



                                              while read -rn1; do echo "$REPLY" ; done <<< "StackOver"


                                              Note that this will catch the newline at the end of the "here document". If you want to avoid that, but still iterate over the characters with a bash loop, use printf to avoid the newline.



                                              printf StackOver | while read -rn1; do echo "$REPLY" ; done





                                              share|improve this answer




























                                                4












                                                4








                                                4







                                                Since you specifically asked for an answer in bash, here's a way to do it in pure bash:



                                                while read -rn1; do echo "$REPLY" ; done <<< "StackOver"


                                                Note that this will catch the newline at the end of the "here document". If you want to avoid that, but still iterate over the characters with a bash loop, use printf to avoid the newline.



                                                printf StackOver | while read -rn1; do echo "$REPLY" ; done





                                                share|improve this answer















                                                Since you specifically asked for an answer in bash, here's a way to do it in pure bash:



                                                while read -rn1; do echo "$REPLY" ; done <<< "StackOver"


                                                Note that this will catch the newline at the end of the "here document". If you want to avoid that, but still iterate over the characters with a bash loop, use printf to avoid the newline.



                                                printf StackOver | while read -rn1; do echo "$REPLY" ; done






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Jan 5 '16 at 22:34









                                                Stéphane Chazelas

                                                305k57574929




                                                305k57574929










                                                answered Jan 5 '16 at 22:16









                                                wyrmwyrm

                                                32015




                                                32015























                                                    4














                                                    Also Python 2 can be used from the command line:



                                                    python <<< "for x in 'StackOver':
                                                    print x"


                                                    or:



                                                    echo "for x in 'StackOver':
                                                    print x" | python


                                                    or (as commented by 1_CR) with Python 3:



                                                    python3 -c "print(*'StackOver',sep='n')"





                                                    share|improve this answer






























                                                      4














                                                      Also Python 2 can be used from the command line:



                                                      python <<< "for x in 'StackOver':
                                                      print x"


                                                      or:



                                                      echo "for x in 'StackOver':
                                                      print x" | python


                                                      or (as commented by 1_CR) with Python 3:



                                                      python3 -c "print(*'StackOver',sep='n')"





                                                      share|improve this answer




























                                                        4












                                                        4








                                                        4







                                                        Also Python 2 can be used from the command line:



                                                        python <<< "for x in 'StackOver':
                                                        print x"


                                                        or:



                                                        echo "for x in 'StackOver':
                                                        print x" | python


                                                        or (as commented by 1_CR) with Python 3:



                                                        python3 -c "print(*'StackOver',sep='n')"





                                                        share|improve this answer















                                                        Also Python 2 can be used from the command line:



                                                        python <<< "for x in 'StackOver':
                                                        print x"


                                                        or:



                                                        echo "for x in 'StackOver':
                                                        print x" | python


                                                        or (as commented by 1_CR) with Python 3:



                                                        python3 -c "print(*'StackOver',sep='n')"






                                                        share|improve this answer














                                                        share|improve this answer



                                                        share|improve this answer








                                                        edited Jan 6 '16 at 10:12









                                                        terdon

                                                        130k32255433




                                                        130k32255433










                                                        answered Jan 5 '16 at 11:57









                                                        agoldagold

                                                        363311




                                                        363311























                                                            4














                                                            You can use the fold (1) command. It is more efficient than grep and sed.



                                                            $ time grep -o . <bigfile >/dev/null

                                                            real 0m3.868s
                                                            user 0m3.784s
                                                            sys 0m0.056s
                                                            $ time fold -b1 <bigfile >/dev/null

                                                            real 0m0.555s
                                                            user 0m0.528s
                                                            sys 0m0.016s
                                                            $


                                                            One significant difference is that fold will reproduce empty lines in the output:



                                                            $ grep -o . <(printf "AnBnnCnnnDn")
                                                            A
                                                            B
                                                            C
                                                            D
                                                            $ fold -b1 <(printf "AnBnnCnnnDn")
                                                            A
                                                            B

                                                            C


                                                            D
                                                            $





                                                            share|improve this answer






























                                                              4














                                                              You can use the fold (1) command. It is more efficient than grep and sed.



                                                              $ time grep -o . <bigfile >/dev/null

                                                              real 0m3.868s
                                                              user 0m3.784s
                                                              sys 0m0.056s
                                                              $ time fold -b1 <bigfile >/dev/null

                                                              real 0m0.555s
                                                              user 0m0.528s
                                                              sys 0m0.016s
                                                              $


                                                              One significant difference is that fold will reproduce empty lines in the output:



                                                              $ grep -o . <(printf "AnBnnCnnnDn")
                                                              A
                                                              B
                                                              C
                                                              D
                                                              $ fold -b1 <(printf "AnBnnCnnnDn")
                                                              A
                                                              B

                                                              C


                                                              D
                                                              $





                                                              share|improve this answer




























                                                                4












                                                                4








                                                                4







                                                                You can use the fold (1) command. It is more efficient than grep and sed.



                                                                $ time grep -o . <bigfile >/dev/null

                                                                real 0m3.868s
                                                                user 0m3.784s
                                                                sys 0m0.056s
                                                                $ time fold -b1 <bigfile >/dev/null

                                                                real 0m0.555s
                                                                user 0m0.528s
                                                                sys 0m0.016s
                                                                $


                                                                One significant difference is that fold will reproduce empty lines in the output:



                                                                $ grep -o . <(printf "AnBnnCnnnDn")
                                                                A
                                                                B
                                                                C
                                                                D
                                                                $ fold -b1 <(printf "AnBnnCnnnDn")
                                                                A
                                                                B

                                                                C


                                                                D
                                                                $





                                                                share|improve this answer















                                                                You can use the fold (1) command. It is more efficient than grep and sed.



                                                                $ time grep -o . <bigfile >/dev/null

                                                                real 0m3.868s
                                                                user 0m3.784s
                                                                sys 0m0.056s
                                                                $ time fold -b1 <bigfile >/dev/null

                                                                real 0m0.555s
                                                                user 0m0.528s
                                                                sys 0m0.016s
                                                                $


                                                                One significant difference is that fold will reproduce empty lines in the output:



                                                                $ grep -o . <(printf "AnBnnCnnnDn")
                                                                A
                                                                B
                                                                C
                                                                D
                                                                $ fold -b1 <(printf "AnBnnCnnnDn")
                                                                A
                                                                B

                                                                C


                                                                D
                                                                $






                                                                share|improve this answer














                                                                share|improve this answer



                                                                share|improve this answer








                                                                edited Jan 6 '16 at 10:19

























                                                                answered Jan 6 '16 at 5:45









                                                                joeytwiddlejoeytwiddle

                                                                55939




                                                                55939























                                                                    3














                                                                    You can handle multibyte characters like:



                                                                    <input 
                                                                    dd cbs=1 obs=2 conv=unblock |
                                                                    sed -e:c -e '/^.*$/!N;s/n//;tc'


                                                                    Which can be pretty handy when you're working with live input because there's no buffering there and a character is printed as soon it is whole.






                                                                    share|improve this answer


























                                                                    • NP, should we add a note about the locale?

                                                                      – cuonglm
                                                                      Jan 5 '16 at 9:35











                                                                    • Does not work for combining characters like Stéphane Chazelas answer, but with proper normalization this should not matter.

                                                                      – kay
                                                                      Jan 5 '16 at 13:06











                                                                    • @Kay - it's works for combining characters if you want it to - that's what sed scripts are for. i'm not likely to write one right about now - im pretty sleepy. it's really useful, though, when reading a terminal.

                                                                      – mikeserv
                                                                      Jan 5 '16 at 14:30













                                                                    • @cuonglm - if you like. it should just work for the locale, given a sane libc, though.

                                                                      – mikeserv
                                                                      Jan 5 '16 at 14:33











                                                                    • Note that dd will break multibyte characters, so the output will not be text anymore so the behaviour of sed will be unspecified as per POSIX.

                                                                      – Stéphane Chazelas
                                                                      Jan 5 '16 at 22:09
















                                                                    3














                                                                    You can handle multibyte characters like:



                                                                    <input 
                                                                    dd cbs=1 obs=2 conv=unblock |
                                                                    sed -e:c -e '/^.*$/!N;s/n//;tc'


                                                                    Which can be pretty handy when you're working with live input because there's no buffering there and a character is printed as soon it is whole.






                                                                    share|improve this answer


























                                                                    • NP, should we add a note about the locale?

                                                                      – cuonglm
                                                                      Jan 5 '16 at 9:35











                                                                    • Does not work for combining characters like Stéphane Chazelas answer, but with proper normalization this should not matter.

                                                                      – kay
                                                                      Jan 5 '16 at 13:06











                                                                    • @Kay - it's works for combining characters if you want it to - that's what sed scripts are for. i'm not likely to write one right about now - im pretty sleepy. it's really useful, though, when reading a terminal.

                                                                      – mikeserv
                                                                      Jan 5 '16 at 14:30













                                                                    • @cuonglm - if you like. it should just work for the locale, given a sane libc, though.

                                                                      – mikeserv
                                                                      Jan 5 '16 at 14:33











                                                                    • Note that dd will break multibyte characters, so the output will not be text anymore so the behaviour of sed will be unspecified as per POSIX.

                                                                      – Stéphane Chazelas
                                                                      Jan 5 '16 at 22:09














                                                                    3












                                                                    3








                                                                    3







                                                                    You can handle multibyte characters like:



                                                                    <input 
                                                                    dd cbs=1 obs=2 conv=unblock |
                                                                    sed -e:c -e '/^.*$/!N;s/n//;tc'


                                                                    Which can be pretty handy when you're working with live input because there's no buffering there and a character is printed as soon it is whole.






                                                                    share|improve this answer















                                                                    You can handle multibyte characters like:



                                                                    <input 
                                                                    dd cbs=1 obs=2 conv=unblock |
                                                                    sed -e:c -e '/^.*$/!N;s/n//;tc'


                                                                    Which can be pretty handy when you're working with live input because there's no buffering there and a character is printed as soon it is whole.







                                                                    share|improve this answer














                                                                    share|improve this answer



                                                                    share|improve this answer








                                                                    edited Jan 5 '16 at 7:27









                                                                    cuonglm

                                                                    104k24203302




                                                                    104k24203302










                                                                    answered Jan 5 '16 at 1:12









                                                                    mikeservmikeserv

                                                                    45.6k668157




                                                                    45.6k668157













                                                                    • NP, should we add a note about the locale?

                                                                      – cuonglm
                                                                      Jan 5 '16 at 9:35











                                                                    • Does not work for combining characters like Stéphane Chazelas answer, but with proper normalization this should not matter.

                                                                      – kay
                                                                      Jan 5 '16 at 13:06











                                                                    • @Kay - it's works for combining characters if you want it to - that's what sed scripts are for. i'm not likely to write one right about now - im pretty sleepy. it's really useful, though, when reading a terminal.

                                                                      – mikeserv
                                                                      Jan 5 '16 at 14:30













                                                                    • @cuonglm - if you like. it should just work for the locale, given a sane libc, though.

                                                                      – mikeserv
                                                                      Jan 5 '16 at 14:33











                                                                    • Note that dd will break multibyte characters, so the output will not be text anymore so the behaviour of sed will be unspecified as per POSIX.

                                                                      – Stéphane Chazelas
                                                                      Jan 5 '16 at 22:09



















                                                                    • NP, should we add a note about the locale?

                                                                      – cuonglm
                                                                      Jan 5 '16 at 9:35











                                                                    • Does not work for combining characters like Stéphane Chazelas answer, but with proper normalization this should not matter.

                                                                      – kay
                                                                      Jan 5 '16 at 13:06











                                                                    • @Kay - it's works for combining characters if you want it to - that's what sed scripts are for. i'm not likely to write one right about now - im pretty sleepy. it's really useful, though, when reading a terminal.

                                                                      – mikeserv
                                                                      Jan 5 '16 at 14:30













                                                                    • @cuonglm - if you like. it should just work for the locale, given a sane libc, though.

                                                                      – mikeserv
                                                                      Jan 5 '16 at 14:33











                                                                    • Note that dd will break multibyte characters, so the output will not be text anymore so the behaviour of sed will be unspecified as per POSIX.

                                                                      – Stéphane Chazelas
                                                                      Jan 5 '16 at 22:09

















                                                                    NP, should we add a note about the locale?

                                                                    – cuonglm
                                                                    Jan 5 '16 at 9:35





                                                                    NP, should we add a note about the locale?

                                                                    – cuonglm
                                                                    Jan 5 '16 at 9:35













                                                                    Does not work for combining characters like Stéphane Chazelas answer, but with proper normalization this should not matter.

                                                                    – kay
                                                                    Jan 5 '16 at 13:06





                                                                    Does not work for combining characters like Stéphane Chazelas answer, but with proper normalization this should not matter.

                                                                    – kay
                                                                    Jan 5 '16 at 13:06













                                                                    @Kay - it's works for combining characters if you want it to - that's what sed scripts are for. i'm not likely to write one right about now - im pretty sleepy. it's really useful, though, when reading a terminal.

                                                                    – mikeserv
                                                                    Jan 5 '16 at 14:30







                                                                    @Kay - it's works for combining characters if you want it to - that's what sed scripts are for. i'm not likely to write one right about now - im pretty sleepy. it's really useful, though, when reading a terminal.

                                                                    – mikeserv
                                                                    Jan 5 '16 at 14:30















                                                                    @cuonglm - if you like. it should just work for the locale, given a sane libc, though.

                                                                    – mikeserv
                                                                    Jan 5 '16 at 14:33





                                                                    @cuonglm - if you like. it should just work for the locale, given a sane libc, though.

                                                                    – mikeserv
                                                                    Jan 5 '16 at 14:33













                                                                    Note that dd will break multibyte characters, so the output will not be text anymore so the behaviour of sed will be unspecified as per POSIX.

                                                                    – Stéphane Chazelas
                                                                    Jan 5 '16 at 22:09





                                                                    Note that dd will break multibyte characters, so the output will not be text anymore so the behaviour of sed will be unspecified as per POSIX.

                                                                    – Stéphane Chazelas
                                                                    Jan 5 '16 at 22:09











                                                                    3














                                                                    You may use word boundaries also..



                                                                    $ perl -pe 's/(?<=.)(B|b)(?=.)/n/g' <<< "StackOver"
                                                                    S
                                                                    t
                                                                    a
                                                                    c
                                                                    k
                                                                    O
                                                                    v
                                                                    e
                                                                    r





                                                                    share|improve this answer




























                                                                      3














                                                                      You may use word boundaries also..



                                                                      $ perl -pe 's/(?<=.)(B|b)(?=.)/n/g' <<< "StackOver"
                                                                      S
                                                                      t
                                                                      a
                                                                      c
                                                                      k
                                                                      O
                                                                      v
                                                                      e
                                                                      r





                                                                      share|improve this answer


























                                                                        3












                                                                        3








                                                                        3







                                                                        You may use word boundaries also..



                                                                        $ perl -pe 's/(?<=.)(B|b)(?=.)/n/g' <<< "StackOver"
                                                                        S
                                                                        t
                                                                        a
                                                                        c
                                                                        k
                                                                        O
                                                                        v
                                                                        e
                                                                        r





                                                                        share|improve this answer













                                                                        You may use word boundaries also..



                                                                        $ perl -pe 's/(?<=.)(B|b)(?=.)/n/g' <<< "StackOver"
                                                                        S
                                                                        t
                                                                        a
                                                                        c
                                                                        k
                                                                        O
                                                                        v
                                                                        e
                                                                        r






                                                                        share|improve this answer












                                                                        share|improve this answer



                                                                        share|improve this answer










                                                                        answered Jan 5 '16 at 9:31









                                                                        Avinash RajAvinash Raj

                                                                        2,60731227




                                                                        2,60731227























                                                                            1














                                                                            In bash:



                                                                            This works with any text and with only bash internals (no external utility called), so, should be fast on very short strings.



                                                                            str="Stéphane áàéèëêếe"

                                                                            [[ $str =~ ${str//?/(.)} ]]
                                                                            (set -- "${BASH_REMATCH[@]:1}"; IFS=$'n'; echo "$*")


                                                                            Output:



                                                                            S
                                                                            t
                                                                            é
                                                                            p
                                                                            h
                                                                            a
                                                                            n
                                                                            e

                                                                            á
                                                                            à
                                                                            é
                                                                            è
                                                                            ë
                                                                            ê
                                                                            ế
                                                                            e


                                                                            If it is ok to change IFS and to change the positional parameters, you can also avoid the sub-shell call:



                                                                            str="Stéphane áàéèëêếe"
                                                                            [[ $str =~ ${str//?/(.)} ]]
                                                                            set -- "${BASH_REMATCH[@]:1}"
                                                                            IFS=$'n'
                                                                            echo "$*"





                                                                            share|improve this answer




























                                                                              1














                                                                              In bash:



                                                                              This works with any text and with only bash internals (no external utility called), so, should be fast on very short strings.



                                                                              str="Stéphane áàéèëêếe"

                                                                              [[ $str =~ ${str//?/(.)} ]]
                                                                              (set -- "${BASH_REMATCH[@]:1}"; IFS=$'n'; echo "$*")


                                                                              Output:



                                                                              S
                                                                              t
                                                                              é
                                                                              p
                                                                              h
                                                                              a
                                                                              n
                                                                              e

                                                                              á
                                                                              à
                                                                              é
                                                                              è
                                                                              ë
                                                                              ê
                                                                              ế
                                                                              e


                                                                              If it is ok to change IFS and to change the positional parameters, you can also avoid the sub-shell call:



                                                                              str="Stéphane áàéèëêếe"
                                                                              [[ $str =~ ${str//?/(.)} ]]
                                                                              set -- "${BASH_REMATCH[@]:1}"
                                                                              IFS=$'n'
                                                                              echo "$*"





                                                                              share|improve this answer


























                                                                                1












                                                                                1








                                                                                1







                                                                                In bash:



                                                                                This works with any text and with only bash internals (no external utility called), so, should be fast on very short strings.



                                                                                str="Stéphane áàéèëêếe"

                                                                                [[ $str =~ ${str//?/(.)} ]]
                                                                                (set -- "${BASH_REMATCH[@]:1}"; IFS=$'n'; echo "$*")


                                                                                Output:



                                                                                S
                                                                                t
                                                                                é
                                                                                p
                                                                                h
                                                                                a
                                                                                n
                                                                                e

                                                                                á
                                                                                à
                                                                                é
                                                                                è
                                                                                ë
                                                                                ê
                                                                                ế
                                                                                e


                                                                                If it is ok to change IFS and to change the positional parameters, you can also avoid the sub-shell call:



                                                                                str="Stéphane áàéèëêếe"
                                                                                [[ $str =~ ${str//?/(.)} ]]
                                                                                set -- "${BASH_REMATCH[@]:1}"
                                                                                IFS=$'n'
                                                                                echo "$*"





                                                                                share|improve this answer













                                                                                In bash:



                                                                                This works with any text and with only bash internals (no external utility called), so, should be fast on very short strings.



                                                                                str="Stéphane áàéèëêếe"

                                                                                [[ $str =~ ${str//?/(.)} ]]
                                                                                (set -- "${BASH_REMATCH[@]:1}"; IFS=$'n'; echo "$*")


                                                                                Output:



                                                                                S
                                                                                t
                                                                                é
                                                                                p
                                                                                h
                                                                                a
                                                                                n
                                                                                e

                                                                                á
                                                                                à
                                                                                é
                                                                                è
                                                                                ë
                                                                                ê
                                                                                ế
                                                                                e


                                                                                If it is ok to change IFS and to change the positional parameters, you can also avoid the sub-shell call:



                                                                                str="Stéphane áàéèëêếe"
                                                                                [[ $str =~ ${str//?/(.)} ]]
                                                                                set -- "${BASH_REMATCH[@]:1}"
                                                                                IFS=$'n'
                                                                                echo "$*"






                                                                                share|improve this answer












                                                                                share|improve this answer



                                                                                share|improve this answer










                                                                                answered Nov 23 '16 at 21:37









                                                                                sorontarsorontar

                                                                                4,448928




                                                                                4,448928























                                                                                    1














                                                                                    s=stackoverflow;

                                                                                    $ time echo $s | fold -w1
                                                                                    s
                                                                                    t
                                                                                    a
                                                                                    c
                                                                                    k
                                                                                    o
                                                                                    v
                                                                                    e
                                                                                    r

                                                                                    real 0m0.014s
                                                                                    user 0m0.000s
                                                                                    sys 0m0.004s


                                                                                    updates
                                                                                    here is the hacky|fastest|pureBashBased way !



                                                                                    $ time eval eval printf '%s\\n' \${s:{0..$((${#s}-1))}:1}
                                                                                    s
                                                                                    t
                                                                                    a
                                                                                    c
                                                                                    k
                                                                                    o
                                                                                    v
                                                                                    e
                                                                                    r

                                                                                    real 0m0.001s
                                                                                    user 0m0.000s
                                                                                    sys 0m0.000s


                                                                                    for more awesomeness



                                                                                    function foldh () 
                                                                                    {
                                                                                    if (($#)); then
                                                                                    local s="$@";
                                                                                    eval eval printf '%s\\n' \"\${s:{0..$((${#s}-1))}:1}\";
                                                                                    else
                                                                                    while read s; do
                                                                                    eval eval printf '%s\\n' \"\${s:{0..$((${#s}-1))}:1}\";
                                                                                    done;
                                                                                    fi
                                                                                    }
                                                                                    function foldv ()
                                                                                    {
                                                                                    if (($#)); then
                                                                                    local s="$@";
                                                                                    eval eval echo \"\${s:{0..$((${#s}-1))}:1}\";
                                                                                    else
                                                                                    while read s; do
                                                                                    eval eval echo \"\${s:{0..$((${#s}-1))}:1}\";
                                                                                    done;
                                                                                    fi
                                                                                    }





                                                                                    share|improve this answer


























                                                                                    • Will this ever give different results to fold -b1 ?

                                                                                      – JigglyNaga
                                                                                      Jul 25 '16 at 12:10











                                                                                    • since each byte have a width=1 the result will be the same !

                                                                                      – Jonas
                                                                                      Jul 25 '16 at 12:30






                                                                                    • 1





                                                                                      So how is this not a duplicate of the earlier answer?

                                                                                      – JigglyNaga
                                                                                      Jul 25 '16 at 13:17











                                                                                    • because it shows tha same cmd with different argyment , and that is nice to know .

                                                                                      – Jonas
                                                                                      Jul 25 '16 at 13:45
















                                                                                    1














                                                                                    s=stackoverflow;

                                                                                    $ time echo $s | fold -w1
                                                                                    s
                                                                                    t
                                                                                    a
                                                                                    c
                                                                                    k
                                                                                    o
                                                                                    v
                                                                                    e
                                                                                    r

                                                                                    real 0m0.014s
                                                                                    user 0m0.000s
                                                                                    sys 0m0.004s


                                                                                    updates
                                                                                    here is the hacky|fastest|pureBashBased way !



                                                                                    $ time eval eval printf '%s\\n' \${s:{0..$((${#s}-1))}:1}
                                                                                    s
                                                                                    t
                                                                                    a
                                                                                    c
                                                                                    k
                                                                                    o
                                                                                    v
                                                                                    e
                                                                                    r

                                                                                    real 0m0.001s
                                                                                    user 0m0.000s
                                                                                    sys 0m0.000s


                                                                                    for more awesomeness



                                                                                    function foldh () 
                                                                                    {
                                                                                    if (($#)); then
                                                                                    local s="$@";
                                                                                    eval eval printf '%s\\n' \"\${s:{0..$((${#s}-1))}:1}\";
                                                                                    else
                                                                                    while read s; do
                                                                                    eval eval printf '%s\\n' \"\${s:{0..$((${#s}-1))}:1}\";
                                                                                    done;
                                                                                    fi
                                                                                    }
                                                                                    function foldv ()
                                                                                    {
                                                                                    if (($#)); then
                                                                                    local s="$@";
                                                                                    eval eval echo \"\${s:{0..$((${#s}-1))}:1}\";
                                                                                    else
                                                                                    while read s; do
                                                                                    eval eval echo \"\${s:{0..$((${#s}-1))}:1}\";
                                                                                    done;
                                                                                    fi
                                                                                    }





                                                                                    share|improve this answer


























                                                                                    • Will this ever give different results to fold -b1 ?

                                                                                      – JigglyNaga
                                                                                      Jul 25 '16 at 12:10











                                                                                    • since each byte have a width=1 the result will be the same !

                                                                                      – Jonas
                                                                                      Jul 25 '16 at 12:30






                                                                                    • 1





                                                                                      So how is this not a duplicate of the earlier answer?

                                                                                      – JigglyNaga
                                                                                      Jul 25 '16 at 13:17











                                                                                    • because it shows tha same cmd with different argyment , and that is nice to know .

                                                                                      – Jonas
                                                                                      Jul 25 '16 at 13:45














                                                                                    1












                                                                                    1








                                                                                    1







                                                                                    s=stackoverflow;

                                                                                    $ time echo $s | fold -w1
                                                                                    s
                                                                                    t
                                                                                    a
                                                                                    c
                                                                                    k
                                                                                    o
                                                                                    v
                                                                                    e
                                                                                    r

                                                                                    real 0m0.014s
                                                                                    user 0m0.000s
                                                                                    sys 0m0.004s


                                                                                    updates
                                                                                    here is the hacky|fastest|pureBashBased way !



                                                                                    $ time eval eval printf '%s\\n' \${s:{0..$((${#s}-1))}:1}
                                                                                    s
                                                                                    t
                                                                                    a
                                                                                    c
                                                                                    k
                                                                                    o
                                                                                    v
                                                                                    e
                                                                                    r

                                                                                    real 0m0.001s
                                                                                    user 0m0.000s
                                                                                    sys 0m0.000s


                                                                                    for more awesomeness



                                                                                    function foldh () 
                                                                                    {
                                                                                    if (($#)); then
                                                                                    local s="$@";
                                                                                    eval eval printf '%s\\n' \"\${s:{0..$((${#s}-1))}:1}\";
                                                                                    else
                                                                                    while read s; do
                                                                                    eval eval printf '%s\\n' \"\${s:{0..$((${#s}-1))}:1}\";
                                                                                    done;
                                                                                    fi
                                                                                    }
                                                                                    function foldv ()
                                                                                    {
                                                                                    if (($#)); then
                                                                                    local s="$@";
                                                                                    eval eval echo \"\${s:{0..$((${#s}-1))}:1}\";
                                                                                    else
                                                                                    while read s; do
                                                                                    eval eval echo \"\${s:{0..$((${#s}-1))}:1}\";
                                                                                    done;
                                                                                    fi
                                                                                    }





                                                                                    share|improve this answer















                                                                                    s=stackoverflow;

                                                                                    $ time echo $s | fold -w1
                                                                                    s
                                                                                    t
                                                                                    a
                                                                                    c
                                                                                    k
                                                                                    o
                                                                                    v
                                                                                    e
                                                                                    r

                                                                                    real 0m0.014s
                                                                                    user 0m0.000s
                                                                                    sys 0m0.004s


                                                                                    updates
                                                                                    here is the hacky|fastest|pureBashBased way !



                                                                                    $ time eval eval printf '%s\\n' \${s:{0..$((${#s}-1))}:1}
                                                                                    s
                                                                                    t
                                                                                    a
                                                                                    c
                                                                                    k
                                                                                    o
                                                                                    v
                                                                                    e
                                                                                    r

                                                                                    real 0m0.001s
                                                                                    user 0m0.000s
                                                                                    sys 0m0.000s


                                                                                    for more awesomeness



                                                                                    function foldh () 
                                                                                    {
                                                                                    if (($#)); then
                                                                                    local s="$@";
                                                                                    eval eval printf '%s\\n' \"\${s:{0..$((${#s}-1))}:1}\";
                                                                                    else
                                                                                    while read s; do
                                                                                    eval eval printf '%s\\n' \"\${s:{0..$((${#s}-1))}:1}\";
                                                                                    done;
                                                                                    fi
                                                                                    }
                                                                                    function foldv ()
                                                                                    {
                                                                                    if (($#)); then
                                                                                    local s="$@";
                                                                                    eval eval echo \"\${s:{0..$((${#s}-1))}:1}\";
                                                                                    else
                                                                                    while read s; do
                                                                                    eval eval echo \"\${s:{0..$((${#s}-1))}:1}\";
                                                                                    done;
                                                                                    fi
                                                                                    }






                                                                                    share|improve this answer














                                                                                    share|improve this answer



                                                                                    share|improve this answer








                                                                                    edited May 20 '17 at 21:49

























                                                                                    answered Jul 25 '16 at 12:01









                                                                                    JonasJonas

                                                                                    883515




                                                                                    883515













                                                                                    • Will this ever give different results to fold -b1 ?

                                                                                      – JigglyNaga
                                                                                      Jul 25 '16 at 12:10











                                                                                    • since each byte have a width=1 the result will be the same !

                                                                                      – Jonas
                                                                                      Jul 25 '16 at 12:30






                                                                                    • 1





                                                                                      So how is this not a duplicate of the earlier answer?

                                                                                      – JigglyNaga
                                                                                      Jul 25 '16 at 13:17











                                                                                    • because it shows tha same cmd with different argyment , and that is nice to know .

                                                                                      – Jonas
                                                                                      Jul 25 '16 at 13:45



















                                                                                    • Will this ever give different results to fold -b1 ?

                                                                                      – JigglyNaga
                                                                                      Jul 25 '16 at 12:10











                                                                                    • since each byte have a width=1 the result will be the same !

                                                                                      – Jonas
                                                                                      Jul 25 '16 at 12:30






                                                                                    • 1





                                                                                      So how is this not a duplicate of the earlier answer?

                                                                                      – JigglyNaga
                                                                                      Jul 25 '16 at 13:17











                                                                                    • because it shows tha same cmd with different argyment , and that is nice to know .

                                                                                      – Jonas
                                                                                      Jul 25 '16 at 13:45

















                                                                                    Will this ever give different results to fold -b1 ?

                                                                                    – JigglyNaga
                                                                                    Jul 25 '16 at 12:10





                                                                                    Will this ever give different results to fold -b1 ?

                                                                                    – JigglyNaga
                                                                                    Jul 25 '16 at 12:10













                                                                                    since each byte have a width=1 the result will be the same !

                                                                                    – Jonas
                                                                                    Jul 25 '16 at 12:30





                                                                                    since each byte have a width=1 the result will be the same !

                                                                                    – Jonas
                                                                                    Jul 25 '16 at 12:30




                                                                                    1




                                                                                    1





                                                                                    So how is this not a duplicate of the earlier answer?

                                                                                    – JigglyNaga
                                                                                    Jul 25 '16 at 13:17





                                                                                    So how is this not a duplicate of the earlier answer?

                                                                                    – JigglyNaga
                                                                                    Jul 25 '16 at 13:17













                                                                                    because it shows tha same cmd with different argyment , and that is nice to know .

                                                                                    – Jonas
                                                                                    Jul 25 '16 at 13:45





                                                                                    because it shows tha same cmd with different argyment , and that is nice to know .

                                                                                    – Jonas
                                                                                    Jul 25 '16 at 13:45











                                                                                    1














                                                                                    read -a var <<< $(echo "$yourWordhere" | grep -o "." | tr 'n' ' ')


                                                                                    this will split your word and store it in array var.






                                                                                    share|improve this answer






























                                                                                      1














                                                                                      read -a var <<< $(echo "$yourWordhere" | grep -o "." | tr 'n' ' ')


                                                                                      this will split your word and store it in array var.






                                                                                      share|improve this answer




























                                                                                        1












                                                                                        1








                                                                                        1







                                                                                        read -a var <<< $(echo "$yourWordhere" | grep -o "." | tr 'n' ' ')


                                                                                        this will split your word and store it in array var.






                                                                                        share|improve this answer















                                                                                        read -a var <<< $(echo "$yourWordhere" | grep -o "." | tr 'n' ' ')


                                                                                        this will split your word and store it in array var.







                                                                                        share|improve this answer














                                                                                        share|improve this answer



                                                                                        share|improve this answer








                                                                                        edited Aug 31 '18 at 11:30









                                                                                        αғsнιη

                                                                                        16.8k102865




                                                                                        16.8k102865










                                                                                        answered Aug 31 '18 at 3:43









                                                                                        Chinmay KatilChinmay Katil

                                                                                        111




                                                                                        111























                                                                                            0














                                                                                            for x in $(echo "$yourWordhere" | grep -o '.')
                                                                                            do
                                                                                            code to perform operation on individual character $x of your word
                                                                                            done





                                                                                            share|improve this answer






























                                                                                              0














                                                                                              for x in $(echo "$yourWordhere" | grep -o '.')
                                                                                              do
                                                                                              code to perform operation on individual character $x of your word
                                                                                              done





                                                                                              share|improve this answer




























                                                                                                0












                                                                                                0








                                                                                                0







                                                                                                for x in $(echo "$yourWordhere" | grep -o '.')
                                                                                                do
                                                                                                code to perform operation on individual character $x of your word
                                                                                                done





                                                                                                share|improve this answer















                                                                                                for x in $(echo "$yourWordhere" | grep -o '.')
                                                                                                do
                                                                                                code to perform operation on individual character $x of your word
                                                                                                done






                                                                                                share|improve this answer














                                                                                                share|improve this answer



                                                                                                share|improve this answer








                                                                                                edited 8 mins ago









                                                                                                phuclv

                                                                                                390121




                                                                                                390121










                                                                                                answered Sep 12 '18 at 7:28









                                                                                                Chinmay KatilChinmay Katil

                                                                                                111




                                                                                                111






























                                                                                                    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.




                                                                                                    draft saved


                                                                                                    draft discarded














                                                                                                    StackExchange.ready(
                                                                                                    function () {
                                                                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f253279%2fbash-script-split-word-on-each-letter%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

                                                                                                    宮崎県

                                                                                                    濃尾地震

                                                                                                    6月16日