Using SED to derive the values from a String

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












2














I want to use echo "123|456|789" | sed 's/^(.*|)(.*|).*$/1'



I am trying to break the string and get the individual values (without the pipe |).



  • Scenario 1: I want to get 123

  • Scenario 2: I want to get 456

  • Scenario 3: I want to get 789

  • Scenario 4: I want to get 123 456 789









share|improve this question



















  • 2




    What exactly are you trying to accomplish?
    – DopeGhoti
    Jul 7 '17 at 18:21






  • 1




    Can you show the input, expected output and the options you have tried? There are a lot of people here who can answer this question, but you need to prove you've tried some options before posting a question here.
    – rahul
    Jul 7 '17 at 18:41






  • 1




    are you missing a '/g' at the end, like in echo "123|456|789" | sed 's/^(.*|)(.*|).*$/1/g' ?
    – Jaleks
    Jul 7 '17 at 18:51










  • We can't answer your question without knowing what you're trying to accomplish here and what your expected results are.
    – Toby
    Jul 7 '17 at 19:05















2














I want to use echo "123|456|789" | sed 's/^(.*|)(.*|).*$/1'



I am trying to break the string and get the individual values (without the pipe |).



  • Scenario 1: I want to get 123

  • Scenario 2: I want to get 456

  • Scenario 3: I want to get 789

  • Scenario 4: I want to get 123 456 789









share|improve this question



















  • 2




    What exactly are you trying to accomplish?
    – DopeGhoti
    Jul 7 '17 at 18:21






  • 1




    Can you show the input, expected output and the options you have tried? There are a lot of people here who can answer this question, but you need to prove you've tried some options before posting a question here.
    – rahul
    Jul 7 '17 at 18:41






  • 1




    are you missing a '/g' at the end, like in echo "123|456|789" | sed 's/^(.*|)(.*|).*$/1/g' ?
    – Jaleks
    Jul 7 '17 at 18:51










  • We can't answer your question without knowing what you're trying to accomplish here and what your expected results are.
    – Toby
    Jul 7 '17 at 19:05













2












2








2







I want to use echo "123|456|789" | sed 's/^(.*|)(.*|).*$/1'



I am trying to break the string and get the individual values (without the pipe |).



  • Scenario 1: I want to get 123

  • Scenario 2: I want to get 456

  • Scenario 3: I want to get 789

  • Scenario 4: I want to get 123 456 789









share|improve this question















I want to use echo "123|456|789" | sed 's/^(.*|)(.*|).*$/1'



I am trying to break the string and get the individual values (without the pipe |).



  • Scenario 1: I want to get 123

  • Scenario 2: I want to get 456

  • Scenario 3: I want to get 789

  • Scenario 4: I want to get 123 456 789






linux shell-script sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 15 at 21:52









Rui F Ribeiro

38.9k1479129




38.9k1479129










asked Jul 7 '17 at 18:20









AlluSingh

549




549







  • 2




    What exactly are you trying to accomplish?
    – DopeGhoti
    Jul 7 '17 at 18:21






  • 1




    Can you show the input, expected output and the options you have tried? There are a lot of people here who can answer this question, but you need to prove you've tried some options before posting a question here.
    – rahul
    Jul 7 '17 at 18:41






  • 1




    are you missing a '/g' at the end, like in echo "123|456|789" | sed 's/^(.*|)(.*|).*$/1/g' ?
    – Jaleks
    Jul 7 '17 at 18:51










  • We can't answer your question without knowing what you're trying to accomplish here and what your expected results are.
    – Toby
    Jul 7 '17 at 19:05












  • 2




    What exactly are you trying to accomplish?
    – DopeGhoti
    Jul 7 '17 at 18:21






  • 1




    Can you show the input, expected output and the options you have tried? There are a lot of people here who can answer this question, but you need to prove you've tried some options before posting a question here.
    – rahul
    Jul 7 '17 at 18:41






  • 1




    are you missing a '/g' at the end, like in echo "123|456|789" | sed 's/^(.*|)(.*|).*$/1/g' ?
    – Jaleks
    Jul 7 '17 at 18:51










  • We can't answer your question without knowing what you're trying to accomplish here and what your expected results are.
    – Toby
    Jul 7 '17 at 19:05







2




2




What exactly are you trying to accomplish?
– DopeGhoti
Jul 7 '17 at 18:21




What exactly are you trying to accomplish?
– DopeGhoti
Jul 7 '17 at 18:21




1




1




Can you show the input, expected output and the options you have tried? There are a lot of people here who can answer this question, but you need to prove you've tried some options before posting a question here.
– rahul
Jul 7 '17 at 18:41




Can you show the input, expected output and the options you have tried? There are a lot of people here who can answer this question, but you need to prove you've tried some options before posting a question here.
– rahul
Jul 7 '17 at 18:41




1




1




are you missing a '/g' at the end, like in echo "123|456|789" | sed 's/^(.*|)(.*|).*$/1/g' ?
– Jaleks
Jul 7 '17 at 18:51




are you missing a '/g' at the end, like in echo "123|456|789" | sed 's/^(.*|)(.*|).*$/1/g' ?
– Jaleks
Jul 7 '17 at 18:51












We can't answer your question without knowing what you're trying to accomplish here and what your expected results are.
– Toby
Jul 7 '17 at 19:05




We can't answer your question without knowing what you're trying to accomplish here and what your expected results are.
– Toby
Jul 7 '17 at 19:05










4 Answers
4






active

oldest

votes


















1














Since I am not sure what you want this to do, I will confine my answer to fixing the command. It should be:



echo "123|456|789" | sed 's/^(.|)(.|).*$/1/'



(note the / immediately before the last quote character).



This then gives the output:



123|



Is that what you wanted?






share|improve this answer




























    1














    1. echo '123|456|789' | sed 's/|.*//'

    2. echo '123|456|789' | sed 's/^[0-9]*|//;s/|.*//'

    3. echo '123|456|789' | sed 's/.*|//'

    4. echo '123|456|789' | sed 's/|/ /g'

    Or if you're not precious about using sed



    1. echo '123|456|789' | cut -f1 -d|

    2. echo '123|456|789' | cut -f2 -d|

    3. echo '123|456|789' | cut -f3 -d|

    4. echo '123|456|789' | cut -f1,2,3 --output-delimiter=" " -d|





    share|improve this answer




























      0














      My previous answer just fixed an error. Here is the way I would actually solve it:



      Scenario 4 is the most complicated, so here is the solution:



      echo '123|456|789' | sed 's/([0-9]*)|([0-9]*)|([0-9]*)/1 2 3/'


      The 1, 2 and 3 at the end select the part matched between the 'decorated brackets' ( the ( and ) ). Each set of those is referenced by the next number, so 1 for the first one, etc.



      For scenarios 1, 2 and 3, you could simplify that, but its easier just to cut and paste, and then just have 1, 2 or 3 as required between the last set of //. It won't hurt to leave the rest of the decorated brackets in there.



      This solution can be expanded to any number of fields, although I'd probably rather use cut if possible.






      share|improve this answer




























        0














        With GNU sed you can capture your fields by mentioning their number in shell var $n



        n=2; # to get field number 2
        echo "123|456|789" | sed -n "s/|/n/$n;s/^[^n]*|//;P"


        while for scenario-4 is:



        echo ... | sed -e 'y/|/ /'





        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%2f376054%2fusing-sed-to-derive-the-values-from-a-string%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Since I am not sure what you want this to do, I will confine my answer to fixing the command. It should be:



          echo "123|456|789" | sed 's/^(.|)(.|).*$/1/'



          (note the / immediately before the last quote character).



          This then gives the output:



          123|



          Is that what you wanted?






          share|improve this answer

























            1














            Since I am not sure what you want this to do, I will confine my answer to fixing the command. It should be:



            echo "123|456|789" | sed 's/^(.|)(.|).*$/1/'



            (note the / immediately before the last quote character).



            This then gives the output:



            123|



            Is that what you wanted?






            share|improve this answer























              1












              1








              1






              Since I am not sure what you want this to do, I will confine my answer to fixing the command. It should be:



              echo "123|456|789" | sed 's/^(.|)(.|).*$/1/'



              (note the / immediately before the last quote character).



              This then gives the output:



              123|



              Is that what you wanted?






              share|improve this answer












              Since I am not sure what you want this to do, I will confine my answer to fixing the command. It should be:



              echo "123|456|789" | sed 's/^(.|)(.|).*$/1/'



              (note the / immediately before the last quote character).



              This then gives the output:



              123|



              Is that what you wanted?







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jul 7 '17 at 19:17









              Bob Eager

              1,8861421




              1,8861421























                  1














                  1. echo '123|456|789' | sed 's/|.*//'

                  2. echo '123|456|789' | sed 's/^[0-9]*|//;s/|.*//'

                  3. echo '123|456|789' | sed 's/.*|//'

                  4. echo '123|456|789' | sed 's/|/ /g'

                  Or if you're not precious about using sed



                  1. echo '123|456|789' | cut -f1 -d|

                  2. echo '123|456|789' | cut -f2 -d|

                  3. echo '123|456|789' | cut -f3 -d|

                  4. echo '123|456|789' | cut -f1,2,3 --output-delimiter=" " -d|





                  share|improve this answer

























                    1














                    1. echo '123|456|789' | sed 's/|.*//'

                    2. echo '123|456|789' | sed 's/^[0-9]*|//;s/|.*//'

                    3. echo '123|456|789' | sed 's/.*|//'

                    4. echo '123|456|789' | sed 's/|/ /g'

                    Or if you're not precious about using sed



                    1. echo '123|456|789' | cut -f1 -d|

                    2. echo '123|456|789' | cut -f2 -d|

                    3. echo '123|456|789' | cut -f3 -d|

                    4. echo '123|456|789' | cut -f1,2,3 --output-delimiter=" " -d|





                    share|improve this answer























                      1












                      1








                      1






                      1. echo '123|456|789' | sed 's/|.*//'

                      2. echo '123|456|789' | sed 's/^[0-9]*|//;s/|.*//'

                      3. echo '123|456|789' | sed 's/.*|//'

                      4. echo '123|456|789' | sed 's/|/ /g'

                      Or if you're not precious about using sed



                      1. echo '123|456|789' | cut -f1 -d|

                      2. echo '123|456|789' | cut -f2 -d|

                      3. echo '123|456|789' | cut -f3 -d|

                      4. echo '123|456|789' | cut -f1,2,3 --output-delimiter=" " -d|





                      share|improve this answer












                      1. echo '123|456|789' | sed 's/|.*//'

                      2. echo '123|456|789' | sed 's/^[0-9]*|//;s/|.*//'

                      3. echo '123|456|789' | sed 's/.*|//'

                      4. echo '123|456|789' | sed 's/|/ /g'

                      Or if you're not precious about using sed



                      1. echo '123|456|789' | cut -f1 -d|

                      2. echo '123|456|789' | cut -f2 -d|

                      3. echo '123|456|789' | cut -f3 -d|

                      4. echo '123|456|789' | cut -f1,2,3 --output-delimiter=" " -d|






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jul 7 '17 at 21:09









                      steve

                      13.9k22452




                      13.9k22452





















                          0














                          My previous answer just fixed an error. Here is the way I would actually solve it:



                          Scenario 4 is the most complicated, so here is the solution:



                          echo '123|456|789' | sed 's/([0-9]*)|([0-9]*)|([0-9]*)/1 2 3/'


                          The 1, 2 and 3 at the end select the part matched between the 'decorated brackets' ( the ( and ) ). Each set of those is referenced by the next number, so 1 for the first one, etc.



                          For scenarios 1, 2 and 3, you could simplify that, but its easier just to cut and paste, and then just have 1, 2 or 3 as required between the last set of //. It won't hurt to leave the rest of the decorated brackets in there.



                          This solution can be expanded to any number of fields, although I'd probably rather use cut if possible.






                          share|improve this answer

























                            0














                            My previous answer just fixed an error. Here is the way I would actually solve it:



                            Scenario 4 is the most complicated, so here is the solution:



                            echo '123|456|789' | sed 's/([0-9]*)|([0-9]*)|([0-9]*)/1 2 3/'


                            The 1, 2 and 3 at the end select the part matched between the 'decorated brackets' ( the ( and ) ). Each set of those is referenced by the next number, so 1 for the first one, etc.



                            For scenarios 1, 2 and 3, you could simplify that, but its easier just to cut and paste, and then just have 1, 2 or 3 as required between the last set of //. It won't hurt to leave the rest of the decorated brackets in there.



                            This solution can be expanded to any number of fields, although I'd probably rather use cut if possible.






                            share|improve this answer























                              0












                              0








                              0






                              My previous answer just fixed an error. Here is the way I would actually solve it:



                              Scenario 4 is the most complicated, so here is the solution:



                              echo '123|456|789' | sed 's/([0-9]*)|([0-9]*)|([0-9]*)/1 2 3/'


                              The 1, 2 and 3 at the end select the part matched between the 'decorated brackets' ( the ( and ) ). Each set of those is referenced by the next number, so 1 for the first one, etc.



                              For scenarios 1, 2 and 3, you could simplify that, but its easier just to cut and paste, and then just have 1, 2 or 3 as required between the last set of //. It won't hurt to leave the rest of the decorated brackets in there.



                              This solution can be expanded to any number of fields, although I'd probably rather use cut if possible.






                              share|improve this answer












                              My previous answer just fixed an error. Here is the way I would actually solve it:



                              Scenario 4 is the most complicated, so here is the solution:



                              echo '123|456|789' | sed 's/([0-9]*)|([0-9]*)|([0-9]*)/1 2 3/'


                              The 1, 2 and 3 at the end select the part matched between the 'decorated brackets' ( the ( and ) ). Each set of those is referenced by the next number, so 1 for the first one, etc.



                              For scenarios 1, 2 and 3, you could simplify that, but its easier just to cut and paste, and then just have 1, 2 or 3 as required between the last set of //. It won't hurt to leave the rest of the decorated brackets in there.



                              This solution can be expanded to any number of fields, although I'd probably rather use cut if possible.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Jul 8 '17 at 16:43









                              Bob Eager

                              1,8861421




                              1,8861421





















                                  0














                                  With GNU sed you can capture your fields by mentioning their number in shell var $n



                                  n=2; # to get field number 2
                                  echo "123|456|789" | sed -n "s/|/n/$n;s/^[^n]*|//;P"


                                  while for scenario-4 is:



                                  echo ... | sed -e 'y/|/ /'





                                  share|improve this answer

























                                    0














                                    With GNU sed you can capture your fields by mentioning their number in shell var $n



                                    n=2; # to get field number 2
                                    echo "123|456|789" | sed -n "s/|/n/$n;s/^[^n]*|//;P"


                                    while for scenario-4 is:



                                    echo ... | sed -e 'y/|/ /'





                                    share|improve this answer























                                      0












                                      0








                                      0






                                      With GNU sed you can capture your fields by mentioning their number in shell var $n



                                      n=2; # to get field number 2
                                      echo "123|456|789" | sed -n "s/|/n/$n;s/^[^n]*|//;P"


                                      while for scenario-4 is:



                                      echo ... | sed -e 'y/|/ /'





                                      share|improve this answer












                                      With GNU sed you can capture your fields by mentioning their number in shell var $n



                                      n=2; # to get field number 2
                                      echo "123|456|789" | sed -n "s/|/n/$n;s/^[^n]*|//;P"


                                      while for scenario-4 is:



                                      echo ... | sed -e 'y/|/ /'






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Jul 8 '17 at 17:26







                                      user218374


































                                          draft saved

                                          draft discarded
















































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


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

                                          But avoid


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

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

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





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


                                          Please pay close attention to the following guidance:


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

                                          But avoid


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

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

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




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f376054%2fusing-sed-to-derive-the-values-from-a-string%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

                                          How to check contact read email or not when send email to Individual?

                                          Bahrain

                                          Postfix configuration issue with fips on centos 7; mailgun relay