split string to two parts using sed or awk or perl or bash

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











up vote
1
down vote

favorite












I have string like this:



Grades ABCDEF-123456


I want to split this string to two sections like this



Grades ABCDEF
Grades 123456


How can I do that in bash?










share|improve this question









New contributor




Sincerity is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1




    Where does that string come from ? Is it stored in a variable or in a file ? Why tagging this bash if you want (per the title) to use sed, perl or awk ?
    – don_crissti
    3 hours ago











  • i have script for my study and i was unable to figure out how to split strings with the dash - which is pulling the letters and numbers together every time i tried to split it,
    – Sincerity
    2 hours ago






  • 1




    whatever you say buddy...
    – don_crissti
    2 hours ago










  • what do you mean?
    – Sincerity
    2 hours ago














up vote
1
down vote

favorite












I have string like this:



Grades ABCDEF-123456


I want to split this string to two sections like this



Grades ABCDEF
Grades 123456


How can I do that in bash?










share|improve this question









New contributor




Sincerity is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1




    Where does that string come from ? Is it stored in a variable or in a file ? Why tagging this bash if you want (per the title) to use sed, perl or awk ?
    – don_crissti
    3 hours ago











  • i have script for my study and i was unable to figure out how to split strings with the dash - which is pulling the letters and numbers together every time i tried to split it,
    – Sincerity
    2 hours ago






  • 1




    whatever you say buddy...
    – don_crissti
    2 hours ago










  • what do you mean?
    – Sincerity
    2 hours ago












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have string like this:



Grades ABCDEF-123456


I want to split this string to two sections like this



Grades ABCDEF
Grades 123456


How can I do that in bash?










share|improve this question









New contributor




Sincerity is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have string like this:



Grades ABCDEF-123456


I want to split this string to two sections like this



Grades ABCDEF
Grades 123456


How can I do that in bash?







bash awk sed perl






share|improve this question









New contributor




Sincerity is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Sincerity is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 hours ago





















New contributor




Sincerity is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 3 hours ago









Sincerity

83




83




New contributor




Sincerity is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Sincerity is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Sincerity is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 1




    Where does that string come from ? Is it stored in a variable or in a file ? Why tagging this bash if you want (per the title) to use sed, perl or awk ?
    – don_crissti
    3 hours ago











  • i have script for my study and i was unable to figure out how to split strings with the dash - which is pulling the letters and numbers together every time i tried to split it,
    – Sincerity
    2 hours ago






  • 1




    whatever you say buddy...
    – don_crissti
    2 hours ago










  • what do you mean?
    – Sincerity
    2 hours ago












  • 1




    Where does that string come from ? Is it stored in a variable or in a file ? Why tagging this bash if you want (per the title) to use sed, perl or awk ?
    – don_crissti
    3 hours ago











  • i have script for my study and i was unable to figure out how to split strings with the dash - which is pulling the letters and numbers together every time i tried to split it,
    – Sincerity
    2 hours ago






  • 1




    whatever you say buddy...
    – don_crissti
    2 hours ago










  • what do you mean?
    – Sincerity
    2 hours ago







1




1




Where does that string come from ? Is it stored in a variable or in a file ? Why tagging this bash if you want (per the title) to use sed, perl or awk ?
– don_crissti
3 hours ago





Where does that string come from ? Is it stored in a variable or in a file ? Why tagging this bash if you want (per the title) to use sed, perl or awk ?
– don_crissti
3 hours ago













i have script for my study and i was unable to figure out how to split strings with the dash - which is pulling the letters and numbers together every time i tried to split it,
– Sincerity
2 hours ago




i have script for my study and i was unable to figure out how to split strings with the dash - which is pulling the letters and numbers together every time i tried to split it,
– Sincerity
2 hours ago




1




1




whatever you say buddy...
– don_crissti
2 hours ago




whatever you say buddy...
– don_crissti
2 hours ago












what do you mean?
– Sincerity
2 hours ago




what do you mean?
– Sincerity
2 hours ago










3 Answers
3






active

oldest

votes

















up vote
2
down vote



accepted










echo Grades "ABCDE-12345" | sed 's/-/ /g' | awk ' print $1" "$2"n"$1" "$3'
Grades ABCDE
Grades 12345


or per @steeldriver



awk -F'[ -]' 'print $1, $2; print $1, $3'





share|improve this answer


















  • 2




    There's really no need for sed here - just set the FS to include - i.e. awk -F'[ -]' 'print $1, $2; print $1, $3'
    – steeldriver
    3 hours ago











  • @ steeldriver thanks!
    – Goro
    2 hours ago










  • thank you for the quick answer
    – Sincerity
    2 hours ago

















up vote
2
down vote













You can do this entirely in your shell, too:



text="Grades ABCDEF-123456"


Split off the leading text. You could capture it if required, but here we'll just discard it:



grades="$text#* "


Now we could extract the two parts as variables but for now we'll just print them:



echo "Grades $grades%-*"
echo "Grades $grades#*-"


You can also crash these together into a single output statement, but I don't think it's as readable (even if printf is safer than echo for certain classes of text):



printf "Grades %snGrades %sn" "$grades%-*" "$grades#*-"





share|improve this answer



























    up vote
    1
    down vote













    You can do it by replacing the dash with a newline followed by the first field:



    perl -alpe 's/-/n$F[0] /' 





    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',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );






      Sincerity is a new contributor. Be nice, and check out our Code of Conduct.









       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f474051%2fsplit-string-to-two-parts-using-sed-or-awk-or-perl-or-bash%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote



      accepted










      echo Grades "ABCDE-12345" | sed 's/-/ /g' | awk ' print $1" "$2"n"$1" "$3'
      Grades ABCDE
      Grades 12345


      or per @steeldriver



      awk -F'[ -]' 'print $1, $2; print $1, $3'





      share|improve this answer


















      • 2




        There's really no need for sed here - just set the FS to include - i.e. awk -F'[ -]' 'print $1, $2; print $1, $3'
        – steeldriver
        3 hours ago











      • @ steeldriver thanks!
        – Goro
        2 hours ago










      • thank you for the quick answer
        – Sincerity
        2 hours ago














      up vote
      2
      down vote



      accepted










      echo Grades "ABCDE-12345" | sed 's/-/ /g' | awk ' print $1" "$2"n"$1" "$3'
      Grades ABCDE
      Grades 12345


      or per @steeldriver



      awk -F'[ -]' 'print $1, $2; print $1, $3'





      share|improve this answer


















      • 2




        There's really no need for sed here - just set the FS to include - i.e. awk -F'[ -]' 'print $1, $2; print $1, $3'
        – steeldriver
        3 hours ago











      • @ steeldriver thanks!
        – Goro
        2 hours ago










      • thank you for the quick answer
        – Sincerity
        2 hours ago












      up vote
      2
      down vote



      accepted







      up vote
      2
      down vote



      accepted






      echo Grades "ABCDE-12345" | sed 's/-/ /g' | awk ' print $1" "$2"n"$1" "$3'
      Grades ABCDE
      Grades 12345


      or per @steeldriver



      awk -F'[ -]' 'print $1, $2; print $1, $3'





      share|improve this answer














      echo Grades "ABCDE-12345" | sed 's/-/ /g' | awk ' print $1" "$2"n"$1" "$3'
      Grades ABCDE
      Grades 12345


      or per @steeldriver



      awk -F'[ -]' 'print $1, $2; print $1, $3'






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited 2 hours ago

























      answered 3 hours ago









      Goro

      7,55753371




      7,55753371







      • 2




        There's really no need for sed here - just set the FS to include - i.e. awk -F'[ -]' 'print $1, $2; print $1, $3'
        – steeldriver
        3 hours ago











      • @ steeldriver thanks!
        – Goro
        2 hours ago










      • thank you for the quick answer
        – Sincerity
        2 hours ago












      • 2




        There's really no need for sed here - just set the FS to include - i.e. awk -F'[ -]' 'print $1, $2; print $1, $3'
        – steeldriver
        3 hours ago











      • @ steeldriver thanks!
        – Goro
        2 hours ago










      • thank you for the quick answer
        – Sincerity
        2 hours ago







      2




      2




      There's really no need for sed here - just set the FS to include - i.e. awk -F'[ -]' 'print $1, $2; print $1, $3'
      – steeldriver
      3 hours ago





      There's really no need for sed here - just set the FS to include - i.e. awk -F'[ -]' 'print $1, $2; print $1, $3'
      – steeldriver
      3 hours ago













      @ steeldriver thanks!
      – Goro
      2 hours ago




      @ steeldriver thanks!
      – Goro
      2 hours ago












      thank you for the quick answer
      – Sincerity
      2 hours ago




      thank you for the quick answer
      – Sincerity
      2 hours ago












      up vote
      2
      down vote













      You can do this entirely in your shell, too:



      text="Grades ABCDEF-123456"


      Split off the leading text. You could capture it if required, but here we'll just discard it:



      grades="$text#* "


      Now we could extract the two parts as variables but for now we'll just print them:



      echo "Grades $grades%-*"
      echo "Grades $grades#*-"


      You can also crash these together into a single output statement, but I don't think it's as readable (even if printf is safer than echo for certain classes of text):



      printf "Grades %snGrades %sn" "$grades%-*" "$grades#*-"





      share|improve this answer
























        up vote
        2
        down vote













        You can do this entirely in your shell, too:



        text="Grades ABCDEF-123456"


        Split off the leading text. You could capture it if required, but here we'll just discard it:



        grades="$text#* "


        Now we could extract the two parts as variables but for now we'll just print them:



        echo "Grades $grades%-*"
        echo "Grades $grades#*-"


        You can also crash these together into a single output statement, but I don't think it's as readable (even if printf is safer than echo for certain classes of text):



        printf "Grades %snGrades %sn" "$grades%-*" "$grades#*-"





        share|improve this answer






















          up vote
          2
          down vote










          up vote
          2
          down vote









          You can do this entirely in your shell, too:



          text="Grades ABCDEF-123456"


          Split off the leading text. You could capture it if required, but here we'll just discard it:



          grades="$text#* "


          Now we could extract the two parts as variables but for now we'll just print them:



          echo "Grades $grades%-*"
          echo "Grades $grades#*-"


          You can also crash these together into a single output statement, but I don't think it's as readable (even if printf is safer than echo for certain classes of text):



          printf "Grades %snGrades %sn" "$grades%-*" "$grades#*-"





          share|improve this answer












          You can do this entirely in your shell, too:



          text="Grades ABCDEF-123456"


          Split off the leading text. You could capture it if required, but here we'll just discard it:



          grades="$text#* "


          Now we could extract the two parts as variables but for now we'll just print them:



          echo "Grades $grades%-*"
          echo "Grades $grades#*-"


          You can also crash these together into a single output statement, but I don't think it's as readable (even if printf is safer than echo for certain classes of text):



          printf "Grades %snGrades %sn" "$grades%-*" "$grades#*-"






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 hours ago









          roaima

          41k547111




          41k547111




















              up vote
              1
              down vote













              You can do it by replacing the dash with a newline followed by the first field:



              perl -alpe 's/-/n$F[0] /' 





              share|improve this answer
























                up vote
                1
                down vote













                You can do it by replacing the dash with a newline followed by the first field:



                perl -alpe 's/-/n$F[0] /' 





                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  You can do it by replacing the dash with a newline followed by the first field:



                  perl -alpe 's/-/n$F[0] /' 





                  share|improve this answer












                  You can do it by replacing the dash with a newline followed by the first field:



                  perl -alpe 's/-/n$F[0] /' 






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 47 mins ago









                  Rakesh Sharma

                  217113




                  217113




















                      Sincerity is a new contributor. Be nice, and check out our Code of Conduct.









                       

                      draft saved


                      draft discarded


















                      Sincerity is a new contributor. Be nice, and check out our Code of Conduct.












                      Sincerity is a new contributor. Be nice, and check out our Code of Conduct.











                      Sincerity is a new contributor. Be nice, and check out our Code of Conduct.













                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f474051%2fsplit-string-to-two-parts-using-sed-or-awk-or-perl-or-bash%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      What does second last employer means? [closed]

                      Installing NextGIS Connect into QGIS 3?

                      One-line joke