Parse delimited string using awk and fetch the matched string





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have a delimited string variable as mentioned below. I would like to grep a matched string. I found some possible solutions on the Internet, but sadly they did not give me the result I was expecting. Can you suggest or correct me.



Input: 123,src_12,234,456
1,23,34,src_23,4,56,7
src_14,12
12,3,5,src_5
Output: src_12
src_23
src_14
src_5


Logic: I need to fetch the string which has 'src_'. It's not always the second item in the list. The position may change. Variable length, delimited.










share|improve this question

























  • With GNU grep: grep -Po 'src_.*?b' file?

    – Cyrus
    Jan 3 at 6:21











  • Thanks Cyrus for editing the text and comments. i dont have file. input is an variable.

    – srinath
    Jan 3 at 6:23






  • 1





    grep -Po 'bsrc_.*?b' <<< "$variable"?

    – Cyrus
    Jan 3 at 6:34











  • What part of this question involves bash shell and needs the tag?

    – Inian
    Jan 3 at 6:41













  • Sorry, this is not the way StackOverflow works. Questions of the form "I want to do X, please give me tips and/or sample code" are considered off-topic. Please visit the help center and read How to Ask, and especially read Why is “Can someone help me?” not an actual question?

    – kvantour
    Jan 3 at 10:40


















0















I have a delimited string variable as mentioned below. I would like to grep a matched string. I found some possible solutions on the Internet, but sadly they did not give me the result I was expecting. Can you suggest or correct me.



Input: 123,src_12,234,456
1,23,34,src_23,4,56,7
src_14,12
12,3,5,src_5
Output: src_12
src_23
src_14
src_5


Logic: I need to fetch the string which has 'src_'. It's not always the second item in the list. The position may change. Variable length, delimited.










share|improve this question

























  • With GNU grep: grep -Po 'src_.*?b' file?

    – Cyrus
    Jan 3 at 6:21











  • Thanks Cyrus for editing the text and comments. i dont have file. input is an variable.

    – srinath
    Jan 3 at 6:23






  • 1





    grep -Po 'bsrc_.*?b' <<< "$variable"?

    – Cyrus
    Jan 3 at 6:34











  • What part of this question involves bash shell and needs the tag?

    – Inian
    Jan 3 at 6:41













  • Sorry, this is not the way StackOverflow works. Questions of the form "I want to do X, please give me tips and/or sample code" are considered off-topic. Please visit the help center and read How to Ask, and especially read Why is “Can someone help me?” not an actual question?

    – kvantour
    Jan 3 at 10:40














0












0








0


1






I have a delimited string variable as mentioned below. I would like to grep a matched string. I found some possible solutions on the Internet, but sadly they did not give me the result I was expecting. Can you suggest or correct me.



Input: 123,src_12,234,456
1,23,34,src_23,4,56,7
src_14,12
12,3,5,src_5
Output: src_12
src_23
src_14
src_5


Logic: I need to fetch the string which has 'src_'. It's not always the second item in the list. The position may change. Variable length, delimited.










share|improve this question
















I have a delimited string variable as mentioned below. I would like to grep a matched string. I found some possible solutions on the Internet, but sadly they did not give me the result I was expecting. Can you suggest or correct me.



Input: 123,src_12,234,456
1,23,34,src_23,4,56,7
src_14,12
12,3,5,src_5
Output: src_12
src_23
src_14
src_5


Logic: I need to fetch the string which has 'src_'. It's not always the second item in the list. The position may change. Variable length, delimited.







unix awk sed






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 10:33









kvantour

10.7k41732




10.7k41732










asked Jan 3 at 6:18









srinathsrinath

447




447













  • With GNU grep: grep -Po 'src_.*?b' file?

    – Cyrus
    Jan 3 at 6:21











  • Thanks Cyrus for editing the text and comments. i dont have file. input is an variable.

    – srinath
    Jan 3 at 6:23






  • 1





    grep -Po 'bsrc_.*?b' <<< "$variable"?

    – Cyrus
    Jan 3 at 6:34











  • What part of this question involves bash shell and needs the tag?

    – Inian
    Jan 3 at 6:41













  • Sorry, this is not the way StackOverflow works. Questions of the form "I want to do X, please give me tips and/or sample code" are considered off-topic. Please visit the help center and read How to Ask, and especially read Why is “Can someone help me?” not an actual question?

    – kvantour
    Jan 3 at 10:40



















  • With GNU grep: grep -Po 'src_.*?b' file?

    – Cyrus
    Jan 3 at 6:21











  • Thanks Cyrus for editing the text and comments. i dont have file. input is an variable.

    – srinath
    Jan 3 at 6:23






  • 1





    grep -Po 'bsrc_.*?b' <<< "$variable"?

    – Cyrus
    Jan 3 at 6:34











  • What part of this question involves bash shell and needs the tag?

    – Inian
    Jan 3 at 6:41













  • Sorry, this is not the way StackOverflow works. Questions of the form "I want to do X, please give me tips and/or sample code" are considered off-topic. Please visit the help center and read How to Ask, and especially read Why is “Can someone help me?” not an actual question?

    – kvantour
    Jan 3 at 10:40

















With GNU grep: grep -Po 'src_.*?b' file?

– Cyrus
Jan 3 at 6:21





With GNU grep: grep -Po 'src_.*?b' file?

– Cyrus
Jan 3 at 6:21













Thanks Cyrus for editing the text and comments. i dont have file. input is an variable.

– srinath
Jan 3 at 6:23





Thanks Cyrus for editing the text and comments. i dont have file. input is an variable.

– srinath
Jan 3 at 6:23




1




1





grep -Po 'bsrc_.*?b' <<< "$variable"?

– Cyrus
Jan 3 at 6:34





grep -Po 'bsrc_.*?b' <<< "$variable"?

– Cyrus
Jan 3 at 6:34













What part of this question involves bash shell and needs the tag?

– Inian
Jan 3 at 6:41







What part of this question involves bash shell and needs the tag?

– Inian
Jan 3 at 6:41















Sorry, this is not the way StackOverflow works. Questions of the form "I want to do X, please give me tips and/or sample code" are considered off-topic. Please visit the help center and read How to Ask, and especially read Why is “Can someone help me?” not an actual question?

– kvantour
Jan 3 at 10:40





Sorry, this is not the way StackOverflow works. Questions of the form "I want to do X, please give me tips and/or sample code" are considered off-topic. Please visit the help center and read How to Ask, and especially read Why is “Can someone help me?” not an actual question?

– kvantour
Jan 3 at 10:40












7 Answers
7






active

oldest

votes


















1














With simple awk solution:



awk 'match($0,/src_[0-9]+/){print substr($0,RSTART,RLENGTH)}'  Input_file


or



awk '{sub(/.*src/,"src");sub(/,.*/,"")} 1' Input_file





share|improve this answer





















  • 1





    When no match scenarios, it gives first string. How to avoid it

    – srinath
    Jan 4 at 2:14











  • @srinath, please try to run first command only which should give output when a match is found only.

    – RavinderSingh13
    Jan 4 at 2:30











  • first statement fails when i have src_abc (non numeric) one.

    – srinath
    Jan 4 at 9:11











  • @srinath, try src_[0-9a-zA-Z]+ on place of src_[0-9]+/ once and let know?

    – RavinderSingh13
    Jan 4 at 9:13













  • it worked partial. for src_12_abc i am getting src_12 only. i need data till next delimiter.

    – srinath
    Jan 4 at 9:24





















1














With bash:



while IFS="," read -a array; do
for element in "${array[@]}"; do
[[ $element =~ ^src_ ]] && echo "$element"
done
done <<< "$variable"


Output:




src_12
src_23
src_14
src_5





share|improve this answer































    1














    Using tr



    $ cat srinath.txt2
    123,src_12,234,456
    1,23,34,src_23,4,56,7
    src_14,12
    12,3,5,src_5
    src_6,src_7,16,18

    $ A=$(cat srinath.txt2)

    $ tr ',' 'n' <<< "$A" | grep ^src
    src_12
    src_23
    src_14
    src_5
    src_6
    src_7





    share|improve this answer


























    • You can avoid the cat, but this will also pick up stuff like vsrc_blabla

      – kvantour
      Jan 3 at 10:35











    • I wanted to post about the same answer, only using grep "^src_" avoiding vsrc_blabla

      – Walter A
      Jan 3 at 10:37











    • Thanks @kvantour for the review.. updated the answer

      – stack0114106
      Jan 3 at 11:04











    • @Walter.. thank you.. updated the answer

      – stack0114106
      Jan 3 at 11:04



















    0














    Using Perl



    $ cat srinath.txt
    123,src_12,234,456
    1,23,34,src_23,4,56,7
    src_14,12
    12,3,5,src_5

    $ perl -nle ' /(src_d+)/ and print $1 ' srinath.txt
    src_12
    src_23
    src_14
    src_5


    If you have more than one src_ in the same line, then use below



    $ cat srinath.txt2
    123,src_12,234,456
    1,23,34,src_23,4,56,7
    src_14,12
    12,3,5,src_5
    src_6,src_7,16,18

    $ perl -nle ' while( /(src_d+)/g ) { print $1 } ' srinath.txt2
    src_12
    src_23
    src_14
    src_5
    src_6
    src_7


    If it is in a variable, then



    $ A=$(cat srinath.txt2)

    $ perl -nle ' while( /(src_d+)/g ) { print $1 } ' <<< "$A"
    src_12
    src_23
    src_14
    src_5
    src_6
    src_7


    or



    $ export A="123,src_12,234,456,1,23,34,src_23,4,56,7,src_14,12,12,3,5,src_5,src_6,src_7,16,18"

    $ perl -nle ' while( /(src_d+)/g ) { print $1 } ' <<< "$A"
    src_12
    src_23
    src_14
    src_5
    src_6
    src_7

    $ perl -le ' $_=$ENV{A}; while( /(src_d+)/g ) { print $1 } '
    src_12
    src_23
    src_14
    src_5
    src_6
    src_7





    share|improve this answer


























    • i need in unix/DOS. its in middle of my program.

      – srinath
      Jan 3 at 6:26











    • yes.. you can call perl in middle of the bash program

      – stack0114106
      Jan 3 at 6:28











    • updated the answer for working with variables

      – stack0114106
      Jan 3 at 6:31











    • Thank you for quick answer @stack0114106. I dont want to increase dependency by using perl. let me figure it out with bash. if nothing is working, i will use your solution.

      – srinath
      Jan 3 at 6:34



















    0














    This might work for you (GNU sed):



    sed '/n/!s/src_[^,]*/n&n/g;/^src_/P;D' file


    Surround all candidate strings by newlines and then using the sed commands P and D whittle down each line printing only the candidates with the prefix src_.






    share|improve this answer































      0














      A simple grep returning only (-o) the matched words (-w)



      $ grep -wo 'src_[^,]*' file
      src_12
      src_23
      src_14
      src_5





      share|improve this answer































        0














        Look for ^src_xxx,, ,src_xxx, and ,src_xxx$, and only print the match without the ,.



        sed -rn 's/.*(,|^)(src_[^,]*)(,|$).*/2/p'





        share|improve this answer
























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54017214%2fparse-delimited-string-using-awk-and-fetch-the-matched-string%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          7 Answers
          7






          active

          oldest

          votes








          7 Answers
          7






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          With simple awk solution:



          awk 'match($0,/src_[0-9]+/){print substr($0,RSTART,RLENGTH)}'  Input_file


          or



          awk '{sub(/.*src/,"src");sub(/,.*/,"")} 1' Input_file





          share|improve this answer





















          • 1





            When no match scenarios, it gives first string. How to avoid it

            – srinath
            Jan 4 at 2:14











          • @srinath, please try to run first command only which should give output when a match is found only.

            – RavinderSingh13
            Jan 4 at 2:30











          • first statement fails when i have src_abc (non numeric) one.

            – srinath
            Jan 4 at 9:11











          • @srinath, try src_[0-9a-zA-Z]+ on place of src_[0-9]+/ once and let know?

            – RavinderSingh13
            Jan 4 at 9:13













          • it worked partial. for src_12_abc i am getting src_12 only. i need data till next delimiter.

            – srinath
            Jan 4 at 9:24


















          1














          With simple awk solution:



          awk 'match($0,/src_[0-9]+/){print substr($0,RSTART,RLENGTH)}'  Input_file


          or



          awk '{sub(/.*src/,"src");sub(/,.*/,"")} 1' Input_file





          share|improve this answer





















          • 1





            When no match scenarios, it gives first string. How to avoid it

            – srinath
            Jan 4 at 2:14











          • @srinath, please try to run first command only which should give output when a match is found only.

            – RavinderSingh13
            Jan 4 at 2:30











          • first statement fails when i have src_abc (non numeric) one.

            – srinath
            Jan 4 at 9:11











          • @srinath, try src_[0-9a-zA-Z]+ on place of src_[0-9]+/ once and let know?

            – RavinderSingh13
            Jan 4 at 9:13













          • it worked partial. for src_12_abc i am getting src_12 only. i need data till next delimiter.

            – srinath
            Jan 4 at 9:24
















          1












          1








          1







          With simple awk solution:



          awk 'match($0,/src_[0-9]+/){print substr($0,RSTART,RLENGTH)}'  Input_file


          or



          awk '{sub(/.*src/,"src");sub(/,.*/,"")} 1' Input_file





          share|improve this answer















          With simple awk solution:



          awk 'match($0,/src_[0-9]+/){print substr($0,RSTART,RLENGTH)}'  Input_file


          or



          awk '{sub(/.*src/,"src");sub(/,.*/,"")} 1' Input_file






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 3 at 10:31









          kvantour

          10.7k41732




          10.7k41732










          answered Jan 3 at 6:37









          RavinderSingh13RavinderSingh13

          30.6k41639




          30.6k41639








          • 1





            When no match scenarios, it gives first string. How to avoid it

            – srinath
            Jan 4 at 2:14











          • @srinath, please try to run first command only which should give output when a match is found only.

            – RavinderSingh13
            Jan 4 at 2:30











          • first statement fails when i have src_abc (non numeric) one.

            – srinath
            Jan 4 at 9:11











          • @srinath, try src_[0-9a-zA-Z]+ on place of src_[0-9]+/ once and let know?

            – RavinderSingh13
            Jan 4 at 9:13













          • it worked partial. for src_12_abc i am getting src_12 only. i need data till next delimiter.

            – srinath
            Jan 4 at 9:24
















          • 1





            When no match scenarios, it gives first string. How to avoid it

            – srinath
            Jan 4 at 2:14











          • @srinath, please try to run first command only which should give output when a match is found only.

            – RavinderSingh13
            Jan 4 at 2:30











          • first statement fails when i have src_abc (non numeric) one.

            – srinath
            Jan 4 at 9:11











          • @srinath, try src_[0-9a-zA-Z]+ on place of src_[0-9]+/ once and let know?

            – RavinderSingh13
            Jan 4 at 9:13













          • it worked partial. for src_12_abc i am getting src_12 only. i need data till next delimiter.

            – srinath
            Jan 4 at 9:24










          1




          1





          When no match scenarios, it gives first string. How to avoid it

          – srinath
          Jan 4 at 2:14





          When no match scenarios, it gives first string. How to avoid it

          – srinath
          Jan 4 at 2:14













          @srinath, please try to run first command only which should give output when a match is found only.

          – RavinderSingh13
          Jan 4 at 2:30





          @srinath, please try to run first command only which should give output when a match is found only.

          – RavinderSingh13
          Jan 4 at 2:30













          first statement fails when i have src_abc (non numeric) one.

          – srinath
          Jan 4 at 9:11





          first statement fails when i have src_abc (non numeric) one.

          – srinath
          Jan 4 at 9:11













          @srinath, try src_[0-9a-zA-Z]+ on place of src_[0-9]+/ once and let know?

          – RavinderSingh13
          Jan 4 at 9:13







          @srinath, try src_[0-9a-zA-Z]+ on place of src_[0-9]+/ once and let know?

          – RavinderSingh13
          Jan 4 at 9:13















          it worked partial. for src_12_abc i am getting src_12 only. i need data till next delimiter.

          – srinath
          Jan 4 at 9:24







          it worked partial. for src_12_abc i am getting src_12 only. i need data till next delimiter.

          – srinath
          Jan 4 at 9:24















          1














          With bash:



          while IFS="," read -a array; do
          for element in "${array[@]}"; do
          [[ $element =~ ^src_ ]] && echo "$element"
          done
          done <<< "$variable"


          Output:




          src_12
          src_23
          src_14
          src_5





          share|improve this answer




























            1














            With bash:



            while IFS="," read -a array; do
            for element in "${array[@]}"; do
            [[ $element =~ ^src_ ]] && echo "$element"
            done
            done <<< "$variable"


            Output:




            src_12
            src_23
            src_14
            src_5





            share|improve this answer


























              1












              1








              1







              With bash:



              while IFS="," read -a array; do
              for element in "${array[@]}"; do
              [[ $element =~ ^src_ ]] && echo "$element"
              done
              done <<< "$variable"


              Output:




              src_12
              src_23
              src_14
              src_5





              share|improve this answer













              With bash:



              while IFS="," read -a array; do
              for element in "${array[@]}"; do
              [[ $element =~ ^src_ ]] && echo "$element"
              done
              done <<< "$variable"


              Output:




              src_12
              src_23
              src_14
              src_5






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jan 3 at 6:38









              CyrusCyrus

              47.1k43880




              47.1k43880























                  1














                  Using tr



                  $ cat srinath.txt2
                  123,src_12,234,456
                  1,23,34,src_23,4,56,7
                  src_14,12
                  12,3,5,src_5
                  src_6,src_7,16,18

                  $ A=$(cat srinath.txt2)

                  $ tr ',' 'n' <<< "$A" | grep ^src
                  src_12
                  src_23
                  src_14
                  src_5
                  src_6
                  src_7





                  share|improve this answer


























                  • You can avoid the cat, but this will also pick up stuff like vsrc_blabla

                    – kvantour
                    Jan 3 at 10:35











                  • I wanted to post about the same answer, only using grep "^src_" avoiding vsrc_blabla

                    – Walter A
                    Jan 3 at 10:37











                  • Thanks @kvantour for the review.. updated the answer

                    – stack0114106
                    Jan 3 at 11:04











                  • @Walter.. thank you.. updated the answer

                    – stack0114106
                    Jan 3 at 11:04
















                  1














                  Using tr



                  $ cat srinath.txt2
                  123,src_12,234,456
                  1,23,34,src_23,4,56,7
                  src_14,12
                  12,3,5,src_5
                  src_6,src_7,16,18

                  $ A=$(cat srinath.txt2)

                  $ tr ',' 'n' <<< "$A" | grep ^src
                  src_12
                  src_23
                  src_14
                  src_5
                  src_6
                  src_7





                  share|improve this answer


























                  • You can avoid the cat, but this will also pick up stuff like vsrc_blabla

                    – kvantour
                    Jan 3 at 10:35











                  • I wanted to post about the same answer, only using grep "^src_" avoiding vsrc_blabla

                    – Walter A
                    Jan 3 at 10:37











                  • Thanks @kvantour for the review.. updated the answer

                    – stack0114106
                    Jan 3 at 11:04











                  • @Walter.. thank you.. updated the answer

                    – stack0114106
                    Jan 3 at 11:04














                  1












                  1








                  1







                  Using tr



                  $ cat srinath.txt2
                  123,src_12,234,456
                  1,23,34,src_23,4,56,7
                  src_14,12
                  12,3,5,src_5
                  src_6,src_7,16,18

                  $ A=$(cat srinath.txt2)

                  $ tr ',' 'n' <<< "$A" | grep ^src
                  src_12
                  src_23
                  src_14
                  src_5
                  src_6
                  src_7





                  share|improve this answer















                  Using tr



                  $ cat srinath.txt2
                  123,src_12,234,456
                  1,23,34,src_23,4,56,7
                  src_14,12
                  12,3,5,src_5
                  src_6,src_7,16,18

                  $ A=$(cat srinath.txt2)

                  $ tr ',' 'n' <<< "$A" | grep ^src
                  src_12
                  src_23
                  src_14
                  src_5
                  src_6
                  src_7






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 3 at 11:03

























                  answered Jan 3 at 6:39









                  stack0114106stack0114106

                  4,9832423




                  4,9832423













                  • You can avoid the cat, but this will also pick up stuff like vsrc_blabla

                    – kvantour
                    Jan 3 at 10:35











                  • I wanted to post about the same answer, only using grep "^src_" avoiding vsrc_blabla

                    – Walter A
                    Jan 3 at 10:37











                  • Thanks @kvantour for the review.. updated the answer

                    – stack0114106
                    Jan 3 at 11:04











                  • @Walter.. thank you.. updated the answer

                    – stack0114106
                    Jan 3 at 11:04



















                  • You can avoid the cat, but this will also pick up stuff like vsrc_blabla

                    – kvantour
                    Jan 3 at 10:35











                  • I wanted to post about the same answer, only using grep "^src_" avoiding vsrc_blabla

                    – Walter A
                    Jan 3 at 10:37











                  • Thanks @kvantour for the review.. updated the answer

                    – stack0114106
                    Jan 3 at 11:04











                  • @Walter.. thank you.. updated the answer

                    – stack0114106
                    Jan 3 at 11:04

















                  You can avoid the cat, but this will also pick up stuff like vsrc_blabla

                  – kvantour
                  Jan 3 at 10:35





                  You can avoid the cat, but this will also pick up stuff like vsrc_blabla

                  – kvantour
                  Jan 3 at 10:35













                  I wanted to post about the same answer, only using grep "^src_" avoiding vsrc_blabla

                  – Walter A
                  Jan 3 at 10:37





                  I wanted to post about the same answer, only using grep "^src_" avoiding vsrc_blabla

                  – Walter A
                  Jan 3 at 10:37













                  Thanks @kvantour for the review.. updated the answer

                  – stack0114106
                  Jan 3 at 11:04





                  Thanks @kvantour for the review.. updated the answer

                  – stack0114106
                  Jan 3 at 11:04













                  @Walter.. thank you.. updated the answer

                  – stack0114106
                  Jan 3 at 11:04





                  @Walter.. thank you.. updated the answer

                  – stack0114106
                  Jan 3 at 11:04











                  0














                  Using Perl



                  $ cat srinath.txt
                  123,src_12,234,456
                  1,23,34,src_23,4,56,7
                  src_14,12
                  12,3,5,src_5

                  $ perl -nle ' /(src_d+)/ and print $1 ' srinath.txt
                  src_12
                  src_23
                  src_14
                  src_5


                  If you have more than one src_ in the same line, then use below



                  $ cat srinath.txt2
                  123,src_12,234,456
                  1,23,34,src_23,4,56,7
                  src_14,12
                  12,3,5,src_5
                  src_6,src_7,16,18

                  $ perl -nle ' while( /(src_d+)/g ) { print $1 } ' srinath.txt2
                  src_12
                  src_23
                  src_14
                  src_5
                  src_6
                  src_7


                  If it is in a variable, then



                  $ A=$(cat srinath.txt2)

                  $ perl -nle ' while( /(src_d+)/g ) { print $1 } ' <<< "$A"
                  src_12
                  src_23
                  src_14
                  src_5
                  src_6
                  src_7


                  or



                  $ export A="123,src_12,234,456,1,23,34,src_23,4,56,7,src_14,12,12,3,5,src_5,src_6,src_7,16,18"

                  $ perl -nle ' while( /(src_d+)/g ) { print $1 } ' <<< "$A"
                  src_12
                  src_23
                  src_14
                  src_5
                  src_6
                  src_7

                  $ perl -le ' $_=$ENV{A}; while( /(src_d+)/g ) { print $1 } '
                  src_12
                  src_23
                  src_14
                  src_5
                  src_6
                  src_7





                  share|improve this answer


























                  • i need in unix/DOS. its in middle of my program.

                    – srinath
                    Jan 3 at 6:26











                  • yes.. you can call perl in middle of the bash program

                    – stack0114106
                    Jan 3 at 6:28











                  • updated the answer for working with variables

                    – stack0114106
                    Jan 3 at 6:31











                  • Thank you for quick answer @stack0114106. I dont want to increase dependency by using perl. let me figure it out with bash. if nothing is working, i will use your solution.

                    – srinath
                    Jan 3 at 6:34
















                  0














                  Using Perl



                  $ cat srinath.txt
                  123,src_12,234,456
                  1,23,34,src_23,4,56,7
                  src_14,12
                  12,3,5,src_5

                  $ perl -nle ' /(src_d+)/ and print $1 ' srinath.txt
                  src_12
                  src_23
                  src_14
                  src_5


                  If you have more than one src_ in the same line, then use below



                  $ cat srinath.txt2
                  123,src_12,234,456
                  1,23,34,src_23,4,56,7
                  src_14,12
                  12,3,5,src_5
                  src_6,src_7,16,18

                  $ perl -nle ' while( /(src_d+)/g ) { print $1 } ' srinath.txt2
                  src_12
                  src_23
                  src_14
                  src_5
                  src_6
                  src_7


                  If it is in a variable, then



                  $ A=$(cat srinath.txt2)

                  $ perl -nle ' while( /(src_d+)/g ) { print $1 } ' <<< "$A"
                  src_12
                  src_23
                  src_14
                  src_5
                  src_6
                  src_7


                  or



                  $ export A="123,src_12,234,456,1,23,34,src_23,4,56,7,src_14,12,12,3,5,src_5,src_6,src_7,16,18"

                  $ perl -nle ' while( /(src_d+)/g ) { print $1 } ' <<< "$A"
                  src_12
                  src_23
                  src_14
                  src_5
                  src_6
                  src_7

                  $ perl -le ' $_=$ENV{A}; while( /(src_d+)/g ) { print $1 } '
                  src_12
                  src_23
                  src_14
                  src_5
                  src_6
                  src_7





                  share|improve this answer


























                  • i need in unix/DOS. its in middle of my program.

                    – srinath
                    Jan 3 at 6:26











                  • yes.. you can call perl in middle of the bash program

                    – stack0114106
                    Jan 3 at 6:28











                  • updated the answer for working with variables

                    – stack0114106
                    Jan 3 at 6:31











                  • Thank you for quick answer @stack0114106. I dont want to increase dependency by using perl. let me figure it out with bash. if nothing is working, i will use your solution.

                    – srinath
                    Jan 3 at 6:34














                  0












                  0








                  0







                  Using Perl



                  $ cat srinath.txt
                  123,src_12,234,456
                  1,23,34,src_23,4,56,7
                  src_14,12
                  12,3,5,src_5

                  $ perl -nle ' /(src_d+)/ and print $1 ' srinath.txt
                  src_12
                  src_23
                  src_14
                  src_5


                  If you have more than one src_ in the same line, then use below



                  $ cat srinath.txt2
                  123,src_12,234,456
                  1,23,34,src_23,4,56,7
                  src_14,12
                  12,3,5,src_5
                  src_6,src_7,16,18

                  $ perl -nle ' while( /(src_d+)/g ) { print $1 } ' srinath.txt2
                  src_12
                  src_23
                  src_14
                  src_5
                  src_6
                  src_7


                  If it is in a variable, then



                  $ A=$(cat srinath.txt2)

                  $ perl -nle ' while( /(src_d+)/g ) { print $1 } ' <<< "$A"
                  src_12
                  src_23
                  src_14
                  src_5
                  src_6
                  src_7


                  or



                  $ export A="123,src_12,234,456,1,23,34,src_23,4,56,7,src_14,12,12,3,5,src_5,src_6,src_7,16,18"

                  $ perl -nle ' while( /(src_d+)/g ) { print $1 } ' <<< "$A"
                  src_12
                  src_23
                  src_14
                  src_5
                  src_6
                  src_7

                  $ perl -le ' $_=$ENV{A}; while( /(src_d+)/g ) { print $1 } '
                  src_12
                  src_23
                  src_14
                  src_5
                  src_6
                  src_7





                  share|improve this answer















                  Using Perl



                  $ cat srinath.txt
                  123,src_12,234,456
                  1,23,34,src_23,4,56,7
                  src_14,12
                  12,3,5,src_5

                  $ perl -nle ' /(src_d+)/ and print $1 ' srinath.txt
                  src_12
                  src_23
                  src_14
                  src_5


                  If you have more than one src_ in the same line, then use below



                  $ cat srinath.txt2
                  123,src_12,234,456
                  1,23,34,src_23,4,56,7
                  src_14,12
                  12,3,5,src_5
                  src_6,src_7,16,18

                  $ perl -nle ' while( /(src_d+)/g ) { print $1 } ' srinath.txt2
                  src_12
                  src_23
                  src_14
                  src_5
                  src_6
                  src_7


                  If it is in a variable, then



                  $ A=$(cat srinath.txt2)

                  $ perl -nle ' while( /(src_d+)/g ) { print $1 } ' <<< "$A"
                  src_12
                  src_23
                  src_14
                  src_5
                  src_6
                  src_7


                  or



                  $ export A="123,src_12,234,456,1,23,34,src_23,4,56,7,src_14,12,12,3,5,src_5,src_6,src_7,16,18"

                  $ perl -nle ' while( /(src_d+)/g ) { print $1 } ' <<< "$A"
                  src_12
                  src_23
                  src_14
                  src_5
                  src_6
                  src_7

                  $ perl -le ' $_=$ENV{A}; while( /(src_d+)/g ) { print $1 } '
                  src_12
                  src_23
                  src_14
                  src_5
                  src_6
                  src_7






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 3 at 6:34

























                  answered Jan 3 at 6:25









                  stack0114106stack0114106

                  4,9832423




                  4,9832423













                  • i need in unix/DOS. its in middle of my program.

                    – srinath
                    Jan 3 at 6:26











                  • yes.. you can call perl in middle of the bash program

                    – stack0114106
                    Jan 3 at 6:28











                  • updated the answer for working with variables

                    – stack0114106
                    Jan 3 at 6:31











                  • Thank you for quick answer @stack0114106. I dont want to increase dependency by using perl. let me figure it out with bash. if nothing is working, i will use your solution.

                    – srinath
                    Jan 3 at 6:34



















                  • i need in unix/DOS. its in middle of my program.

                    – srinath
                    Jan 3 at 6:26











                  • yes.. you can call perl in middle of the bash program

                    – stack0114106
                    Jan 3 at 6:28











                  • updated the answer for working with variables

                    – stack0114106
                    Jan 3 at 6:31











                  • Thank you for quick answer @stack0114106. I dont want to increase dependency by using perl. let me figure it out with bash. if nothing is working, i will use your solution.

                    – srinath
                    Jan 3 at 6:34

















                  i need in unix/DOS. its in middle of my program.

                  – srinath
                  Jan 3 at 6:26





                  i need in unix/DOS. its in middle of my program.

                  – srinath
                  Jan 3 at 6:26













                  yes.. you can call perl in middle of the bash program

                  – stack0114106
                  Jan 3 at 6:28





                  yes.. you can call perl in middle of the bash program

                  – stack0114106
                  Jan 3 at 6:28













                  updated the answer for working with variables

                  – stack0114106
                  Jan 3 at 6:31





                  updated the answer for working with variables

                  – stack0114106
                  Jan 3 at 6:31













                  Thank you for quick answer @stack0114106. I dont want to increase dependency by using perl. let me figure it out with bash. if nothing is working, i will use your solution.

                  – srinath
                  Jan 3 at 6:34





                  Thank you for quick answer @stack0114106. I dont want to increase dependency by using perl. let me figure it out with bash. if nothing is working, i will use your solution.

                  – srinath
                  Jan 3 at 6:34











                  0














                  This might work for you (GNU sed):



                  sed '/n/!s/src_[^,]*/n&n/g;/^src_/P;D' file


                  Surround all candidate strings by newlines and then using the sed commands P and D whittle down each line printing only the candidates with the prefix src_.






                  share|improve this answer




























                    0














                    This might work for you (GNU sed):



                    sed '/n/!s/src_[^,]*/n&n/g;/^src_/P;D' file


                    Surround all candidate strings by newlines and then using the sed commands P and D whittle down each line printing only the candidates with the prefix src_.






                    share|improve this answer


























                      0












                      0








                      0







                      This might work for you (GNU sed):



                      sed '/n/!s/src_[^,]*/n&n/g;/^src_/P;D' file


                      Surround all candidate strings by newlines and then using the sed commands P and D whittle down each line printing only the candidates with the prefix src_.






                      share|improve this answer













                      This might work for you (GNU sed):



                      sed '/n/!s/src_[^,]*/n&n/g;/^src_/P;D' file


                      Surround all candidate strings by newlines and then using the sed commands P and D whittle down each line printing only the candidates with the prefix src_.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jan 3 at 10:23









                      potongpotong

                      36.3k43062




                      36.3k43062























                          0














                          A simple grep returning only (-o) the matched words (-w)



                          $ grep -wo 'src_[^,]*' file
                          src_12
                          src_23
                          src_14
                          src_5





                          share|improve this answer




























                            0














                            A simple grep returning only (-o) the matched words (-w)



                            $ grep -wo 'src_[^,]*' file
                            src_12
                            src_23
                            src_14
                            src_5





                            share|improve this answer


























                              0












                              0








                              0







                              A simple grep returning only (-o) the matched words (-w)



                              $ grep -wo 'src_[^,]*' file
                              src_12
                              src_23
                              src_14
                              src_5





                              share|improve this answer













                              A simple grep returning only (-o) the matched words (-w)



                              $ grep -wo 'src_[^,]*' file
                              src_12
                              src_23
                              src_14
                              src_5






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Jan 3 at 10:39









                              kvantourkvantour

                              10.7k41732




                              10.7k41732























                                  0














                                  Look for ^src_xxx,, ,src_xxx, and ,src_xxx$, and only print the match without the ,.



                                  sed -rn 's/.*(,|^)(src_[^,]*)(,|$).*/2/p'





                                  share|improve this answer




























                                    0














                                    Look for ^src_xxx,, ,src_xxx, and ,src_xxx$, and only print the match without the ,.



                                    sed -rn 's/.*(,|^)(src_[^,]*)(,|$).*/2/p'





                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      Look for ^src_xxx,, ,src_xxx, and ,src_xxx$, and only print the match without the ,.



                                      sed -rn 's/.*(,|^)(src_[^,]*)(,|$).*/2/p'





                                      share|improve this answer













                                      Look for ^src_xxx,, ,src_xxx, and ,src_xxx$, and only print the match without the ,.



                                      sed -rn 's/.*(,|^)(src_[^,]*)(,|$).*/2/p'






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Jan 3 at 10:49









                                      Walter AWalter A

                                      11.1k21132




                                      11.1k21132






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • 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%2fstackoverflow.com%2fquestions%2f54017214%2fparse-delimited-string-using-awk-and-fetch-the-matched-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

                                          MongoDB - Not Authorized To Execute Command

                                          How to fix TextFormField cause rebuild widget in Flutter

                                          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith