Any way to extract/cut a common string out of 2 different strings?

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












0















What is Bash or Linux command for extracting the common string out of 2 partly similar, partly different strings



how to get variable



a="How good is it to"


out of



b="How good is it to die in defending fatherland"
c="How good is it to live in dedicating oneself to nation"









share|improve this question
























  • Why is the word to not included in the string that you want to extract? What about the word in?

    – Kusalananda
    Jan 29 at 10:07











  • What about the trailing space?

    – Jeff Schaller
    Jan 29 at 10:44











  • Does the order of the words matter? What should the two strings a b c 1 2 3 and a b 1 2 c 3 result in?

    – Kusalananda
    Jan 29 at 11:01











  • Yes, only the sequence of consecutive words are demanded

    – abdan
    Jan 29 at 11:14











  • Did any of the answers solved your problem? If not, let us know; otherwise, please accept it by clicking the checkmark next to it. Thank you!

    – Jeff Schaller
    Feb 10 at 12:51















0















What is Bash or Linux command for extracting the common string out of 2 partly similar, partly different strings



how to get variable



a="How good is it to"


out of



b="How good is it to die in defending fatherland"
c="How good is it to live in dedicating oneself to nation"









share|improve this question
























  • Why is the word to not included in the string that you want to extract? What about the word in?

    – Kusalananda
    Jan 29 at 10:07











  • What about the trailing space?

    – Jeff Schaller
    Jan 29 at 10:44











  • Does the order of the words matter? What should the two strings a b c 1 2 3 and a b 1 2 c 3 result in?

    – Kusalananda
    Jan 29 at 11:01











  • Yes, only the sequence of consecutive words are demanded

    – abdan
    Jan 29 at 11:14











  • Did any of the answers solved your problem? If not, let us know; otherwise, please accept it by clicking the checkmark next to it. Thank you!

    – Jeff Schaller
    Feb 10 at 12:51













0












0








0


0






What is Bash or Linux command for extracting the common string out of 2 partly similar, partly different strings



how to get variable



a="How good is it to"


out of



b="How good is it to die in defending fatherland"
c="How good is it to live in dedicating oneself to nation"









share|improve this question
















What is Bash or Linux command for extracting the common string out of 2 partly similar, partly different strings



how to get variable



a="How good is it to"


out of



b="How good is it to die in defending fatherland"
c="How good is it to live in dedicating oneself to nation"






linux bash shell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 29 at 10:08







abdan

















asked Jan 29 at 10:05









abdanabdan

314




314












  • Why is the word to not included in the string that you want to extract? What about the word in?

    – Kusalananda
    Jan 29 at 10:07











  • What about the trailing space?

    – Jeff Schaller
    Jan 29 at 10:44











  • Does the order of the words matter? What should the two strings a b c 1 2 3 and a b 1 2 c 3 result in?

    – Kusalananda
    Jan 29 at 11:01











  • Yes, only the sequence of consecutive words are demanded

    – abdan
    Jan 29 at 11:14











  • Did any of the answers solved your problem? If not, let us know; otherwise, please accept it by clicking the checkmark next to it. Thank you!

    – Jeff Schaller
    Feb 10 at 12:51

















  • Why is the word to not included in the string that you want to extract? What about the word in?

    – Kusalananda
    Jan 29 at 10:07











  • What about the trailing space?

    – Jeff Schaller
    Jan 29 at 10:44











  • Does the order of the words matter? What should the two strings a b c 1 2 3 and a b 1 2 c 3 result in?

    – Kusalananda
    Jan 29 at 11:01











  • Yes, only the sequence of consecutive words are demanded

    – abdan
    Jan 29 at 11:14











  • Did any of the answers solved your problem? If not, let us know; otherwise, please accept it by clicking the checkmark next to it. Thank you!

    – Jeff Schaller
    Feb 10 at 12:51
















Why is the word to not included in the string that you want to extract? What about the word in?

– Kusalananda
Jan 29 at 10:07





Why is the word to not included in the string that you want to extract? What about the word in?

– Kusalananda
Jan 29 at 10:07













What about the trailing space?

– Jeff Schaller
Jan 29 at 10:44





What about the trailing space?

– Jeff Schaller
Jan 29 at 10:44













Does the order of the words matter? What should the two strings a b c 1 2 3 and a b 1 2 c 3 result in?

– Kusalananda
Jan 29 at 11:01





Does the order of the words matter? What should the two strings a b c 1 2 3 and a b 1 2 c 3 result in?

– Kusalananda
Jan 29 at 11:01













Yes, only the sequence of consecutive words are demanded

– abdan
Jan 29 at 11:14





Yes, only the sequence of consecutive words are demanded

– abdan
Jan 29 at 11:14













Did any of the answers solved your problem? If not, let us know; otherwise, please accept it by clicking the checkmark next to it. Thank you!

– Jeff Schaller
Feb 10 at 12:51





Did any of the answers solved your problem? If not, let us know; otherwise, please accept it by clicking the checkmark next to it. Thank you!

– Jeff Schaller
Feb 10 at 12:51










3 Answers
3






active

oldest

votes


















2














With a bash loop over the strings:



i=0
a=
while [[ $b:i:1 == $c:i:1 ]]; do a+=$b[i]; ((++i)); done


Or, with less manipulation:



i=0
while [[ $b:i:1 == $c:i:1 ]]; do ((++i)); done
a=$b:0:i


Note that this results in the string:



printf -- '-->%s<--n' "$a"
-->How good is it to <--


... with the trailing space, since that character is common to both source strings.






share|improve this answer






























    0














    Try the following code:



    b="How good is it to die in defending fatherland"
    c="How good is it to live in dedicating oneself to nation"
    a=()
    count=0
    for i in $b[@]
    do
    if [ "`echo "$c[@]" | grep $i`" ]; then
    a[count]=$i
    count=$((count+1))
    fi
    done
    echo $a[@]





    share|improve this answer






























      0














      i have redirected value of variable "a" and "b" to file "file1" and "file2"

      Below is command i have used to fetch the common strings


      Let me know for any suggestions or corrections



      for i in 1..13; do awk -v i="$i" 'NR==FNRa[$i];next($i in a)print $i' file1 file2 ; done|sed '/^$/d'| perl -pne "s/n/ /g"


      output



      How good is it to in





      share|improve this answer























      • the OP did not show the word "in" as sample output...

        – Jeff Schaller
        Jan 29 at 11:39










      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%2f497406%2fany-way-to-extract-cut-a-common-string-out-of-2-different-strings%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      With a bash loop over the strings:



      i=0
      a=
      while [[ $b:i:1 == $c:i:1 ]]; do a+=$b[i]; ((++i)); done


      Or, with less manipulation:



      i=0
      while [[ $b:i:1 == $c:i:1 ]]; do ((++i)); done
      a=$b:0:i


      Note that this results in the string:



      printf -- '-->%s<--n' "$a"
      -->How good is it to <--


      ... with the trailing space, since that character is common to both source strings.






      share|improve this answer



























        2














        With a bash loop over the strings:



        i=0
        a=
        while [[ $b:i:1 == $c:i:1 ]]; do a+=$b[i]; ((++i)); done


        Or, with less manipulation:



        i=0
        while [[ $b:i:1 == $c:i:1 ]]; do ((++i)); done
        a=$b:0:i


        Note that this results in the string:



        printf -- '-->%s<--n' "$a"
        -->How good is it to <--


        ... with the trailing space, since that character is common to both source strings.






        share|improve this answer

























          2












          2








          2







          With a bash loop over the strings:



          i=0
          a=
          while [[ $b:i:1 == $c:i:1 ]]; do a+=$b[i]; ((++i)); done


          Or, with less manipulation:



          i=0
          while [[ $b:i:1 == $c:i:1 ]]; do ((++i)); done
          a=$b:0:i


          Note that this results in the string:



          printf -- '-->%s<--n' "$a"
          -->How good is it to <--


          ... with the trailing space, since that character is common to both source strings.






          share|improve this answer













          With a bash loop over the strings:



          i=0
          a=
          while [[ $b:i:1 == $c:i:1 ]]; do a+=$b[i]; ((++i)); done


          Or, with less manipulation:



          i=0
          while [[ $b:i:1 == $c:i:1 ]]; do ((++i)); done
          a=$b:0:i


          Note that this results in the string:



          printf -- '-->%s<--n' "$a"
          -->How good is it to <--


          ... with the trailing space, since that character is common to both source strings.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 29 at 14:17









          Jeff SchallerJeff Schaller

          41.6k1056132




          41.6k1056132























              0














              Try the following code:



              b="How good is it to die in defending fatherland"
              c="How good is it to live in dedicating oneself to nation"
              a=()
              count=0
              for i in $b[@]
              do
              if [ "`echo "$c[@]" | grep $i`" ]; then
              a[count]=$i
              count=$((count+1))
              fi
              done
              echo $a[@]





              share|improve this answer



























                0














                Try the following code:



                b="How good is it to die in defending fatherland"
                c="How good is it to live in dedicating oneself to nation"
                a=()
                count=0
                for i in $b[@]
                do
                if [ "`echo "$c[@]" | grep $i`" ]; then
                a[count]=$i
                count=$((count+1))
                fi
                done
                echo $a[@]





                share|improve this answer

























                  0












                  0








                  0







                  Try the following code:



                  b="How good is it to die in defending fatherland"
                  c="How good is it to live in dedicating oneself to nation"
                  a=()
                  count=0
                  for i in $b[@]
                  do
                  if [ "`echo "$c[@]" | grep $i`" ]; then
                  a[count]=$i
                  count=$((count+1))
                  fi
                  done
                  echo $a[@]





                  share|improve this answer













                  Try the following code:



                  b="How good is it to die in defending fatherland"
                  c="How good is it to live in dedicating oneself to nation"
                  a=()
                  count=0
                  for i in $b[@]
                  do
                  if [ "`echo "$c[@]" | grep $i`" ]; then
                  a[count]=$i
                  count=$((count+1))
                  fi
                  done
                  echo $a[@]






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 29 at 10:58









                  msp9011msp9011

                  4,33444065




                  4,33444065





















                      0














                      i have redirected value of variable "a" and "b" to file "file1" and "file2"

                      Below is command i have used to fetch the common strings


                      Let me know for any suggestions or corrections



                      for i in 1..13; do awk -v i="$i" 'NR==FNRa[$i];next($i in a)print $i' file1 file2 ; done|sed '/^$/d'| perl -pne "s/n/ /g"


                      output



                      How good is it to in





                      share|improve this answer























                      • the OP did not show the word "in" as sample output...

                        – Jeff Schaller
                        Jan 29 at 11:39















                      0














                      i have redirected value of variable "a" and "b" to file "file1" and "file2"

                      Below is command i have used to fetch the common strings


                      Let me know for any suggestions or corrections



                      for i in 1..13; do awk -v i="$i" 'NR==FNRa[$i];next($i in a)print $i' file1 file2 ; done|sed '/^$/d'| perl -pne "s/n/ /g"


                      output



                      How good is it to in





                      share|improve this answer























                      • the OP did not show the word "in" as sample output...

                        – Jeff Schaller
                        Jan 29 at 11:39













                      0












                      0








                      0







                      i have redirected value of variable "a" and "b" to file "file1" and "file2"

                      Below is command i have used to fetch the common strings


                      Let me know for any suggestions or corrections



                      for i in 1..13; do awk -v i="$i" 'NR==FNRa[$i];next($i in a)print $i' file1 file2 ; done|sed '/^$/d'| perl -pne "s/n/ /g"


                      output



                      How good is it to in





                      share|improve this answer













                      i have redirected value of variable "a" and "b" to file "file1" and "file2"

                      Below is command i have used to fetch the common strings


                      Let me know for any suggestions or corrections



                      for i in 1..13; do awk -v i="$i" 'NR==FNRa[$i];next($i in a)print $i' file1 file2 ; done|sed '/^$/d'| perl -pne "s/n/ /g"


                      output



                      How good is it to in






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jan 29 at 10:59









                      Praveen Kumar BSPraveen Kumar BS

                      1,470138




                      1,470138












                      • the OP did not show the word "in" as sample output...

                        – Jeff Schaller
                        Jan 29 at 11:39

















                      • the OP did not show the word "in" as sample output...

                        – Jeff Schaller
                        Jan 29 at 11:39
















                      the OP did not show the word "in" as sample output...

                      – Jeff Schaller
                      Jan 29 at 11:39





                      the OP did not show the word "in" as sample output...

                      – Jeff Schaller
                      Jan 29 at 11:39

















                      draft saved

                      draft discarded
















































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


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

                      But avoid


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

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

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




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f497406%2fany-way-to-extract-cut-a-common-string-out-of-2-different-strings%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown






                      Popular posts from this blog

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

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?