How do I split a string into two strings?












1















I already know how to put h4 h7 h8 h9 h7 into an array with space as delimiter, but after that I am trying to separate the h from the numbers to compare the numbers to each other.



How can I do split these in a bash script?










share|improve this question





























    1















    I already know how to put h4 h7 h8 h9 h7 into an array with space as delimiter, but after that I am trying to separate the h from the numbers to compare the numbers to each other.



    How can I do split these in a bash script?










    share|improve this question



























      1












      1








      1


      1






      I already know how to put h4 h7 h8 h9 h7 into an array with space as delimiter, but after that I am trying to separate the h from the numbers to compare the numbers to each other.



      How can I do split these in a bash script?










      share|improve this question
















      I already know how to put h4 h7 h8 h9 h7 into an array with space as delimiter, but after that I am trying to separate the h from the numbers to compare the numbers to each other.



      How can I do split these in a bash script?







      bash string






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 19 '18 at 11:29









      GAD3R

      27.8k1958114




      27.8k1958114










      asked Dec 19 '18 at 9:48









      MercyfonMercyfon

      205




      205






















          1 Answer
          1






          active

          oldest

          votes


















          4














          #!/bin/bash

          arr=( h4 h7 h8 h9 h7 )

          for thing in "${arr[@]}"; do
          num=${thing#?}
          printf 'The number in "%s" is %dn' "$thing" "$num"
          done


          The variable expansion ${variable#pattern} removes the shortest prefix string matching pattern from $variable. The pattern ? matches a single character.



          The output will be



          The number in "h4" is 4
          The number in "h7" is 7
          The number in "h8" is 8
          The number in "h9" is 9
          The number in "h7" is 7


          Alternatively, to ignore remove the non-digit prefix regardless of how long it is, using sed,



          #!/bin/bash

          arr=( hello4 happy7 hobbit8 hulk9 hr-department7 )

          for thing in "${arr[@]}"; do
          num=$( printf '%sn' "$thing" | sed 's/^[^[:digit:]]*//' )
          printf 'The number in "%s" is %dn' "$thing" "$num"
          done


          or, using regular expression matching of the digits at the end,



          #!/bin/bash

          arr=( hello4 happy7 hobbit8 hulk9 hr-department7 )

          for thing in "${arr[@]}"; do
          if [[ "$thing" =~ ([[:digit:]]+)$ ]]; then
          printf 'The number in "%s" is %dn' "$thing" "${BASH_REMATCH[1]}"
          fi
          done





          share|improve this answer


























          • +1 Nice, Can you please explain this part: printf 'The number in "%s" is %dn' "$thing" "$num" ?

            – User123
            Dec 19 '18 at 10:02






          • 2





            @User123 It's a standard way of outputting a string containing variable data. printf takes a format string containing format specifiers (here %s for a string and %d for a decimal number). The variable data is provided as the extra arguments, and printf will insert the data in the appropriate places in the format string. It works pretty much the same way as in most programming languages (C and Perl, for example). See also Why is printf better than echo?

            – Kusalananda
            Dec 19 '18 at 10:04













          • Thanks!! But if i use: echo "The number in $thing is $num" It'll also produce the same output. Is there any downfall in using like this?

            – User123
            Dec 19 '18 at 10:08








          • 2





            @User123 Not if $thing contains escape sequences like n. I know it's very unlikely that your data may use t, n etc., but other people's data may, and my answer is for anyone who reads it and has a similar problem. Using echo in their code may do unexpected things depending on what their data actually is. printf guarantees more consistent output. See Why is printf better than echo?

            – Kusalananda
            Dec 19 '18 at 10:11













          • @User123 Sorry, I confused you with the person asking the question. Nevertheless...

            – Kusalananda
            Dec 19 '18 at 10:14












          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%2f489868%2fhow-do-i-split-a-string-into-two-strings%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          #!/bin/bash

          arr=( h4 h7 h8 h9 h7 )

          for thing in "${arr[@]}"; do
          num=${thing#?}
          printf 'The number in "%s" is %dn' "$thing" "$num"
          done


          The variable expansion ${variable#pattern} removes the shortest prefix string matching pattern from $variable. The pattern ? matches a single character.



          The output will be



          The number in "h4" is 4
          The number in "h7" is 7
          The number in "h8" is 8
          The number in "h9" is 9
          The number in "h7" is 7


          Alternatively, to ignore remove the non-digit prefix regardless of how long it is, using sed,



          #!/bin/bash

          arr=( hello4 happy7 hobbit8 hulk9 hr-department7 )

          for thing in "${arr[@]}"; do
          num=$( printf '%sn' "$thing" | sed 's/^[^[:digit:]]*//' )
          printf 'The number in "%s" is %dn' "$thing" "$num"
          done


          or, using regular expression matching of the digits at the end,



          #!/bin/bash

          arr=( hello4 happy7 hobbit8 hulk9 hr-department7 )

          for thing in "${arr[@]}"; do
          if [[ "$thing" =~ ([[:digit:]]+)$ ]]; then
          printf 'The number in "%s" is %dn' "$thing" "${BASH_REMATCH[1]}"
          fi
          done





          share|improve this answer


























          • +1 Nice, Can you please explain this part: printf 'The number in "%s" is %dn' "$thing" "$num" ?

            – User123
            Dec 19 '18 at 10:02






          • 2





            @User123 It's a standard way of outputting a string containing variable data. printf takes a format string containing format specifiers (here %s for a string and %d for a decimal number). The variable data is provided as the extra arguments, and printf will insert the data in the appropriate places in the format string. It works pretty much the same way as in most programming languages (C and Perl, for example). See also Why is printf better than echo?

            – Kusalananda
            Dec 19 '18 at 10:04













          • Thanks!! But if i use: echo "The number in $thing is $num" It'll also produce the same output. Is there any downfall in using like this?

            – User123
            Dec 19 '18 at 10:08








          • 2





            @User123 Not if $thing contains escape sequences like n. I know it's very unlikely that your data may use t, n etc., but other people's data may, and my answer is for anyone who reads it and has a similar problem. Using echo in their code may do unexpected things depending on what their data actually is. printf guarantees more consistent output. See Why is printf better than echo?

            – Kusalananda
            Dec 19 '18 at 10:11













          • @User123 Sorry, I confused you with the person asking the question. Nevertheless...

            – Kusalananda
            Dec 19 '18 at 10:14
















          4














          #!/bin/bash

          arr=( h4 h7 h8 h9 h7 )

          for thing in "${arr[@]}"; do
          num=${thing#?}
          printf 'The number in "%s" is %dn' "$thing" "$num"
          done


          The variable expansion ${variable#pattern} removes the shortest prefix string matching pattern from $variable. The pattern ? matches a single character.



          The output will be



          The number in "h4" is 4
          The number in "h7" is 7
          The number in "h8" is 8
          The number in "h9" is 9
          The number in "h7" is 7


          Alternatively, to ignore remove the non-digit prefix regardless of how long it is, using sed,



          #!/bin/bash

          arr=( hello4 happy7 hobbit8 hulk9 hr-department7 )

          for thing in "${arr[@]}"; do
          num=$( printf '%sn' "$thing" | sed 's/^[^[:digit:]]*//' )
          printf 'The number in "%s" is %dn' "$thing" "$num"
          done


          or, using regular expression matching of the digits at the end,



          #!/bin/bash

          arr=( hello4 happy7 hobbit8 hulk9 hr-department7 )

          for thing in "${arr[@]}"; do
          if [[ "$thing" =~ ([[:digit:]]+)$ ]]; then
          printf 'The number in "%s" is %dn' "$thing" "${BASH_REMATCH[1]}"
          fi
          done





          share|improve this answer


























          • +1 Nice, Can you please explain this part: printf 'The number in "%s" is %dn' "$thing" "$num" ?

            – User123
            Dec 19 '18 at 10:02






          • 2





            @User123 It's a standard way of outputting a string containing variable data. printf takes a format string containing format specifiers (here %s for a string and %d for a decimal number). The variable data is provided as the extra arguments, and printf will insert the data in the appropriate places in the format string. It works pretty much the same way as in most programming languages (C and Perl, for example). See also Why is printf better than echo?

            – Kusalananda
            Dec 19 '18 at 10:04













          • Thanks!! But if i use: echo "The number in $thing is $num" It'll also produce the same output. Is there any downfall in using like this?

            – User123
            Dec 19 '18 at 10:08








          • 2





            @User123 Not if $thing contains escape sequences like n. I know it's very unlikely that your data may use t, n etc., but other people's data may, and my answer is for anyone who reads it and has a similar problem. Using echo in their code may do unexpected things depending on what their data actually is. printf guarantees more consistent output. See Why is printf better than echo?

            – Kusalananda
            Dec 19 '18 at 10:11













          • @User123 Sorry, I confused you with the person asking the question. Nevertheless...

            – Kusalananda
            Dec 19 '18 at 10:14














          4












          4








          4







          #!/bin/bash

          arr=( h4 h7 h8 h9 h7 )

          for thing in "${arr[@]}"; do
          num=${thing#?}
          printf 'The number in "%s" is %dn' "$thing" "$num"
          done


          The variable expansion ${variable#pattern} removes the shortest prefix string matching pattern from $variable. The pattern ? matches a single character.



          The output will be



          The number in "h4" is 4
          The number in "h7" is 7
          The number in "h8" is 8
          The number in "h9" is 9
          The number in "h7" is 7


          Alternatively, to ignore remove the non-digit prefix regardless of how long it is, using sed,



          #!/bin/bash

          arr=( hello4 happy7 hobbit8 hulk9 hr-department7 )

          for thing in "${arr[@]}"; do
          num=$( printf '%sn' "$thing" | sed 's/^[^[:digit:]]*//' )
          printf 'The number in "%s" is %dn' "$thing" "$num"
          done


          or, using regular expression matching of the digits at the end,



          #!/bin/bash

          arr=( hello4 happy7 hobbit8 hulk9 hr-department7 )

          for thing in "${arr[@]}"; do
          if [[ "$thing" =~ ([[:digit:]]+)$ ]]; then
          printf 'The number in "%s" is %dn' "$thing" "${BASH_REMATCH[1]}"
          fi
          done





          share|improve this answer















          #!/bin/bash

          arr=( h4 h7 h8 h9 h7 )

          for thing in "${arr[@]}"; do
          num=${thing#?}
          printf 'The number in "%s" is %dn' "$thing" "$num"
          done


          The variable expansion ${variable#pattern} removes the shortest prefix string matching pattern from $variable. The pattern ? matches a single character.



          The output will be



          The number in "h4" is 4
          The number in "h7" is 7
          The number in "h8" is 8
          The number in "h9" is 9
          The number in "h7" is 7


          Alternatively, to ignore remove the non-digit prefix regardless of how long it is, using sed,



          #!/bin/bash

          arr=( hello4 happy7 hobbit8 hulk9 hr-department7 )

          for thing in "${arr[@]}"; do
          num=$( printf '%sn' "$thing" | sed 's/^[^[:digit:]]*//' )
          printf 'The number in "%s" is %dn' "$thing" "$num"
          done


          or, using regular expression matching of the digits at the end,



          #!/bin/bash

          arr=( hello4 happy7 hobbit8 hulk9 hr-department7 )

          for thing in "${arr[@]}"; do
          if [[ "$thing" =~ ([[:digit:]]+)$ ]]; then
          printf 'The number in "%s" is %dn' "$thing" "${BASH_REMATCH[1]}"
          fi
          done






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 19 '18 at 11:41

























          answered Dec 19 '18 at 9:56









          KusalanandaKusalananda

          139k17259430




          139k17259430













          • +1 Nice, Can you please explain this part: printf 'The number in "%s" is %dn' "$thing" "$num" ?

            – User123
            Dec 19 '18 at 10:02






          • 2





            @User123 It's a standard way of outputting a string containing variable data. printf takes a format string containing format specifiers (here %s for a string and %d for a decimal number). The variable data is provided as the extra arguments, and printf will insert the data in the appropriate places in the format string. It works pretty much the same way as in most programming languages (C and Perl, for example). See also Why is printf better than echo?

            – Kusalananda
            Dec 19 '18 at 10:04













          • Thanks!! But if i use: echo "The number in $thing is $num" It'll also produce the same output. Is there any downfall in using like this?

            – User123
            Dec 19 '18 at 10:08








          • 2





            @User123 Not if $thing contains escape sequences like n. I know it's very unlikely that your data may use t, n etc., but other people's data may, and my answer is for anyone who reads it and has a similar problem. Using echo in their code may do unexpected things depending on what their data actually is. printf guarantees more consistent output. See Why is printf better than echo?

            – Kusalananda
            Dec 19 '18 at 10:11













          • @User123 Sorry, I confused you with the person asking the question. Nevertheless...

            – Kusalananda
            Dec 19 '18 at 10:14



















          • +1 Nice, Can you please explain this part: printf 'The number in "%s" is %dn' "$thing" "$num" ?

            – User123
            Dec 19 '18 at 10:02






          • 2





            @User123 It's a standard way of outputting a string containing variable data. printf takes a format string containing format specifiers (here %s for a string and %d for a decimal number). The variable data is provided as the extra arguments, and printf will insert the data in the appropriate places in the format string. It works pretty much the same way as in most programming languages (C and Perl, for example). See also Why is printf better than echo?

            – Kusalananda
            Dec 19 '18 at 10:04













          • Thanks!! But if i use: echo "The number in $thing is $num" It'll also produce the same output. Is there any downfall in using like this?

            – User123
            Dec 19 '18 at 10:08








          • 2





            @User123 Not if $thing contains escape sequences like n. I know it's very unlikely that your data may use t, n etc., but other people's data may, and my answer is for anyone who reads it and has a similar problem. Using echo in their code may do unexpected things depending on what their data actually is. printf guarantees more consistent output. See Why is printf better than echo?

            – Kusalananda
            Dec 19 '18 at 10:11













          • @User123 Sorry, I confused you with the person asking the question. Nevertheless...

            – Kusalananda
            Dec 19 '18 at 10:14

















          +1 Nice, Can you please explain this part: printf 'The number in "%s" is %dn' "$thing" "$num" ?

          – User123
          Dec 19 '18 at 10:02





          +1 Nice, Can you please explain this part: printf 'The number in "%s" is %dn' "$thing" "$num" ?

          – User123
          Dec 19 '18 at 10:02




          2




          2





          @User123 It's a standard way of outputting a string containing variable data. printf takes a format string containing format specifiers (here %s for a string and %d for a decimal number). The variable data is provided as the extra arguments, and printf will insert the data in the appropriate places in the format string. It works pretty much the same way as in most programming languages (C and Perl, for example). See also Why is printf better than echo?

          – Kusalananda
          Dec 19 '18 at 10:04







          @User123 It's a standard way of outputting a string containing variable data. printf takes a format string containing format specifiers (here %s for a string and %d for a decimal number). The variable data is provided as the extra arguments, and printf will insert the data in the appropriate places in the format string. It works pretty much the same way as in most programming languages (C and Perl, for example). See also Why is printf better than echo?

          – Kusalananda
          Dec 19 '18 at 10:04















          Thanks!! But if i use: echo "The number in $thing is $num" It'll also produce the same output. Is there any downfall in using like this?

          – User123
          Dec 19 '18 at 10:08







          Thanks!! But if i use: echo "The number in $thing is $num" It'll also produce the same output. Is there any downfall in using like this?

          – User123
          Dec 19 '18 at 10:08






          2




          2





          @User123 Not if $thing contains escape sequences like n. I know it's very unlikely that your data may use t, n etc., but other people's data may, and my answer is for anyone who reads it and has a similar problem. Using echo in their code may do unexpected things depending on what their data actually is. printf guarantees more consistent output. See Why is printf better than echo?

          – Kusalananda
          Dec 19 '18 at 10:11







          @User123 Not if $thing contains escape sequences like n. I know it's very unlikely that your data may use t, n etc., but other people's data may, and my answer is for anyone who reads it and has a similar problem. Using echo in their code may do unexpected things depending on what their data actually is. printf guarantees more consistent output. See Why is printf better than echo?

          – Kusalananda
          Dec 19 '18 at 10:11















          @User123 Sorry, I confused you with the person asking the question. Nevertheless...

          – Kusalananda
          Dec 19 '18 at 10:14





          @User123 Sorry, I confused you with the person asking the question. Nevertheless...

          – Kusalananda
          Dec 19 '18 at 10:14


















          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%2f489868%2fhow-do-i-split-a-string-into-two-strings%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

          Plaza Victoria

          Puebla de Zaragoza

          Musa