How do I remove the last characters from a string?

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











up vote
6
down vote

favorite
4












I have a variable set with var='type_cardio_10-11-2017'. I need to remove the last 10 letters from the variable, and append the remaining value to var2.



I tried with the following script, but it doesn't work as expected.



var=type_cardio_10-11-2017
var2=$var | cut -f1 -d "_"
echo $var2


The output I want is type_cardio.







share|improve this question


























    up vote
    6
    down vote

    favorite
    4












    I have a variable set with var='type_cardio_10-11-2017'. I need to remove the last 10 letters from the variable, and append the remaining value to var2.



    I tried with the following script, but it doesn't work as expected.



    var=type_cardio_10-11-2017
    var2=$var | cut -f1 -d "_"
    echo $var2


    The output I want is type_cardio.







    share|improve this question
























      up vote
      6
      down vote

      favorite
      4









      up vote
      6
      down vote

      favorite
      4






      4





      I have a variable set with var='type_cardio_10-11-2017'. I need to remove the last 10 letters from the variable, and append the remaining value to var2.



      I tried with the following script, but it doesn't work as expected.



      var=type_cardio_10-11-2017
      var2=$var | cut -f1 -d "_"
      echo $var2


      The output I want is type_cardio.







      share|improve this question














      I have a variable set with var='type_cardio_10-11-2017'. I need to remove the last 10 letters from the variable, and append the remaining value to var2.



      I tried with the following script, but it doesn't work as expected.



      var=type_cardio_10-11-2017
      var2=$var | cut -f1 -d "_"
      echo $var2


      The output I want is type_cardio.









      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 20 '17 at 17:57









      Jeff Schaller

      32.1k849109




      32.1k849109










      asked Oct 20 '17 at 17:38









      Rak kundra

      141110




      141110




















          5 Answers
          5






          active

          oldest

          votes

















          up vote
          14
          down vote



          accepted










          To remove everything from after the last _ in var and assign the result to var2:



          var2=$var%_*


          The parameter expansion $parameter%word removes the pattern word (_* in this case) from the end of the value of the given variable.



          The POSIX standard calls this a "Remove Smallest Suffix Pattern" parameter expansion.






          share|improve this answer





























            up vote
            9
            down vote













            You actually want to remove the trailing 11 characters from the string; here's another way to do it:



            $ var=type_cardio_10-11-2017
            $ var2=$var%???????????
            $ echo "$var2"
            type_cardio





            share|improve this answer



























              up vote
              5
              down vote













              Another approach in bash:



              echo "$var::-10"


              Or in older versions:



              echo "$var::$#var-10" #or
              echo "$var: : -10"





              share|improve this answer





























                up vote
                4
                down vote













                1) bash solution:



                var1="type_cardio_10-11-2017"
                var2=$var1%_*


                2) cut solution:



                var1="type_cardio_10-11-2017"
                var2=$(cut -d'_' -f1,2 <<<"$var1")
                echo "$var2"


                The output:



                type_cardio





                share|improve this answer






















                • Thank you sir, But the numeric values will be changing because it is a date string .That is the reason i was using cut .This answer is prefect but can i know how cut can be applied ?
                  – Rak kundra
                  Oct 20 '17 at 17:44






                • 1




                  @SanthoshPogaku, that was not clear from your initial question, you have my update
                  – RomanPerekhrest
                  Oct 20 '17 at 17:52










                • Yeah sir, I got what am i looking for . Thank's for your valuable time.
                  – Rak kundra
                  Oct 20 '17 at 17:53

















                up vote
                1
                down vote













                Appending var and var2 is the easy part - you can just join them like new_var="$var$var2". If what you really meant to say is you want to store cropped var into var2,then it's just var2=$(...) and you can put the commands that other users and my answer presented here inside the $() portion.



                The main part is removing those 10 characters. There's number of ways to extract the part you want from var:




                • printf (pretty portable, doesn't rely on specific shell)



                  $ printf "%.11sn" "$var" 
                  type_cardio



                • awk



                  $ var=type_cardio_10-11-2017 
                  $ awk -v awk_var="$var" 'BEGINprint substr(awk_var,0,length(awk_var)-11)'
                  type_cardio


                  or you can use printf trick here as well:



                  $ echo "$var" | awk 'printf "%.11sn",$0' 
                  type_cardio



                • egrep



                  $ echo "$var" | egrep -o '^.0,11' 
                  type_cardio



                • perl:



                  $ echo "$var" | perl -lne 'print substr($_,0,11)' 
                  type_cardio



                • python:



                  $ python -c 'import sys;print sys.argv[1][0:11] ' "$var" 
                  type_cardio






                share|improve this answer






















                  Your Answer







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

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

                  else
                  createEditor();

                  );

                  function createEditor()
                  StackExchange.prepareEditor(
                  heartbeatType: 'answer',
                  convertImagesToLinks: false,
                  noModals: false,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: null,
                  bindNavPrevention: true,
                  postfix: "",
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  );



                  );













                   

                  draft saved


                  draft discarded


















                  StackExchange.ready(
                  function ()
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f399392%2fhow-do-i-remove-the-last-characters-from-a-string%23new-answer', 'question_page');

                  );

                  Post as a guest






























                  5 Answers
                  5






                  active

                  oldest

                  votes








                  5 Answers
                  5






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  14
                  down vote



                  accepted










                  To remove everything from after the last _ in var and assign the result to var2:



                  var2=$var%_*


                  The parameter expansion $parameter%word removes the pattern word (_* in this case) from the end of the value of the given variable.



                  The POSIX standard calls this a "Remove Smallest Suffix Pattern" parameter expansion.






                  share|improve this answer


























                    up vote
                    14
                    down vote



                    accepted










                    To remove everything from after the last _ in var and assign the result to var2:



                    var2=$var%_*


                    The parameter expansion $parameter%word removes the pattern word (_* in this case) from the end of the value of the given variable.



                    The POSIX standard calls this a "Remove Smallest Suffix Pattern" parameter expansion.






                    share|improve this answer
























                      up vote
                      14
                      down vote



                      accepted







                      up vote
                      14
                      down vote



                      accepted






                      To remove everything from after the last _ in var and assign the result to var2:



                      var2=$var%_*


                      The parameter expansion $parameter%word removes the pattern word (_* in this case) from the end of the value of the given variable.



                      The POSIX standard calls this a "Remove Smallest Suffix Pattern" parameter expansion.






                      share|improve this answer














                      To remove everything from after the last _ in var and assign the result to var2:



                      var2=$var%_*


                      The parameter expansion $parameter%word removes the pattern word (_* in this case) from the end of the value of the given variable.



                      The POSIX standard calls this a "Remove Smallest Suffix Pattern" parameter expansion.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Oct 20 '17 at 17:55

























                      answered Oct 20 '17 at 17:49









                      Kusalananda

                      105k14209326




                      105k14209326






















                          up vote
                          9
                          down vote













                          You actually want to remove the trailing 11 characters from the string; here's another way to do it:



                          $ var=type_cardio_10-11-2017
                          $ var2=$var%???????????
                          $ echo "$var2"
                          type_cardio





                          share|improve this answer
























                            up vote
                            9
                            down vote













                            You actually want to remove the trailing 11 characters from the string; here's another way to do it:



                            $ var=type_cardio_10-11-2017
                            $ var2=$var%???????????
                            $ echo "$var2"
                            type_cardio





                            share|improve this answer






















                              up vote
                              9
                              down vote










                              up vote
                              9
                              down vote









                              You actually want to remove the trailing 11 characters from the string; here's another way to do it:



                              $ var=type_cardio_10-11-2017
                              $ var2=$var%???????????
                              $ echo "$var2"
                              type_cardio





                              share|improve this answer












                              You actually want to remove the trailing 11 characters from the string; here's another way to do it:



                              $ var=type_cardio_10-11-2017
                              $ var2=$var%???????????
                              $ echo "$var2"
                              type_cardio






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Oct 20 '17 at 17:56









                              Jeff Schaller

                              32.1k849109




                              32.1k849109




















                                  up vote
                                  5
                                  down vote













                                  Another approach in bash:



                                  echo "$var::-10"


                                  Or in older versions:



                                  echo "$var::$#var-10" #or
                                  echo "$var: : -10"





                                  share|improve this answer


























                                    up vote
                                    5
                                    down vote













                                    Another approach in bash:



                                    echo "$var::-10"


                                    Or in older versions:



                                    echo "$var::$#var-10" #or
                                    echo "$var: : -10"





                                    share|improve this answer
























                                      up vote
                                      5
                                      down vote










                                      up vote
                                      5
                                      down vote









                                      Another approach in bash:



                                      echo "$var::-10"


                                      Or in older versions:



                                      echo "$var::$#var-10" #or
                                      echo "$var: : -10"





                                      share|improve this answer














                                      Another approach in bash:



                                      echo "$var::-10"


                                      Or in older versions:



                                      echo "$var::$#var-10" #or
                                      echo "$var: : -10"






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Oct 20 '17 at 18:07

























                                      answered Oct 20 '17 at 18:00









                                      αғsнιη

                                      15.6k92563




                                      15.6k92563




















                                          up vote
                                          4
                                          down vote













                                          1) bash solution:



                                          var1="type_cardio_10-11-2017"
                                          var2=$var1%_*


                                          2) cut solution:



                                          var1="type_cardio_10-11-2017"
                                          var2=$(cut -d'_' -f1,2 <<<"$var1")
                                          echo "$var2"


                                          The output:



                                          type_cardio





                                          share|improve this answer






















                                          • Thank you sir, But the numeric values will be changing because it is a date string .That is the reason i was using cut .This answer is prefect but can i know how cut can be applied ?
                                            – Rak kundra
                                            Oct 20 '17 at 17:44






                                          • 1




                                            @SanthoshPogaku, that was not clear from your initial question, you have my update
                                            – RomanPerekhrest
                                            Oct 20 '17 at 17:52










                                          • Yeah sir, I got what am i looking for . Thank's for your valuable time.
                                            – Rak kundra
                                            Oct 20 '17 at 17:53














                                          up vote
                                          4
                                          down vote













                                          1) bash solution:



                                          var1="type_cardio_10-11-2017"
                                          var2=$var1%_*


                                          2) cut solution:



                                          var1="type_cardio_10-11-2017"
                                          var2=$(cut -d'_' -f1,2 <<<"$var1")
                                          echo "$var2"


                                          The output:



                                          type_cardio





                                          share|improve this answer






















                                          • Thank you sir, But the numeric values will be changing because it is a date string .That is the reason i was using cut .This answer is prefect but can i know how cut can be applied ?
                                            – Rak kundra
                                            Oct 20 '17 at 17:44






                                          • 1




                                            @SanthoshPogaku, that was not clear from your initial question, you have my update
                                            – RomanPerekhrest
                                            Oct 20 '17 at 17:52










                                          • Yeah sir, I got what am i looking for . Thank's for your valuable time.
                                            – Rak kundra
                                            Oct 20 '17 at 17:53












                                          up vote
                                          4
                                          down vote










                                          up vote
                                          4
                                          down vote









                                          1) bash solution:



                                          var1="type_cardio_10-11-2017"
                                          var2=$var1%_*


                                          2) cut solution:



                                          var1="type_cardio_10-11-2017"
                                          var2=$(cut -d'_' -f1,2 <<<"$var1")
                                          echo "$var2"


                                          The output:



                                          type_cardio





                                          share|improve this answer














                                          1) bash solution:



                                          var1="type_cardio_10-11-2017"
                                          var2=$var1%_*


                                          2) cut solution:



                                          var1="type_cardio_10-11-2017"
                                          var2=$(cut -d'_' -f1,2 <<<"$var1")
                                          echo "$var2"


                                          The output:



                                          type_cardio






                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Oct 20 '17 at 17:54

























                                          answered Oct 20 '17 at 17:42









                                          RomanPerekhrest

                                          22.5k12145




                                          22.5k12145











                                          • Thank you sir, But the numeric values will be changing because it is a date string .That is the reason i was using cut .This answer is prefect but can i know how cut can be applied ?
                                            – Rak kundra
                                            Oct 20 '17 at 17:44






                                          • 1




                                            @SanthoshPogaku, that was not clear from your initial question, you have my update
                                            – RomanPerekhrest
                                            Oct 20 '17 at 17:52










                                          • Yeah sir, I got what am i looking for . Thank's for your valuable time.
                                            – Rak kundra
                                            Oct 20 '17 at 17:53
















                                          • Thank you sir, But the numeric values will be changing because it is a date string .That is the reason i was using cut .This answer is prefect but can i know how cut can be applied ?
                                            – Rak kundra
                                            Oct 20 '17 at 17:44






                                          • 1




                                            @SanthoshPogaku, that was not clear from your initial question, you have my update
                                            – RomanPerekhrest
                                            Oct 20 '17 at 17:52










                                          • Yeah sir, I got what am i looking for . Thank's for your valuable time.
                                            – Rak kundra
                                            Oct 20 '17 at 17:53















                                          Thank you sir, But the numeric values will be changing because it is a date string .That is the reason i was using cut .This answer is prefect but can i know how cut can be applied ?
                                          – Rak kundra
                                          Oct 20 '17 at 17:44




                                          Thank you sir, But the numeric values will be changing because it is a date string .That is the reason i was using cut .This answer is prefect but can i know how cut can be applied ?
                                          – Rak kundra
                                          Oct 20 '17 at 17:44




                                          1




                                          1




                                          @SanthoshPogaku, that was not clear from your initial question, you have my update
                                          – RomanPerekhrest
                                          Oct 20 '17 at 17:52




                                          @SanthoshPogaku, that was not clear from your initial question, you have my update
                                          – RomanPerekhrest
                                          Oct 20 '17 at 17:52












                                          Yeah sir, I got what am i looking for . Thank's for your valuable time.
                                          – Rak kundra
                                          Oct 20 '17 at 17:53




                                          Yeah sir, I got what am i looking for . Thank's for your valuable time.
                                          – Rak kundra
                                          Oct 20 '17 at 17:53










                                          up vote
                                          1
                                          down vote













                                          Appending var and var2 is the easy part - you can just join them like new_var="$var$var2". If what you really meant to say is you want to store cropped var into var2,then it's just var2=$(...) and you can put the commands that other users and my answer presented here inside the $() portion.



                                          The main part is removing those 10 characters. There's number of ways to extract the part you want from var:




                                          • printf (pretty portable, doesn't rely on specific shell)



                                            $ printf "%.11sn" "$var" 
                                            type_cardio



                                          • awk



                                            $ var=type_cardio_10-11-2017 
                                            $ awk -v awk_var="$var" 'BEGINprint substr(awk_var,0,length(awk_var)-11)'
                                            type_cardio


                                            or you can use printf trick here as well:



                                            $ echo "$var" | awk 'printf "%.11sn",$0' 
                                            type_cardio



                                          • egrep



                                            $ echo "$var" | egrep -o '^.0,11' 
                                            type_cardio



                                          • perl:



                                            $ echo "$var" | perl -lne 'print substr($_,0,11)' 
                                            type_cardio



                                          • python:



                                            $ python -c 'import sys;print sys.argv[1][0:11] ' "$var" 
                                            type_cardio






                                          share|improve this answer


























                                            up vote
                                            1
                                            down vote













                                            Appending var and var2 is the easy part - you can just join them like new_var="$var$var2". If what you really meant to say is you want to store cropped var into var2,then it's just var2=$(...) and you can put the commands that other users and my answer presented here inside the $() portion.



                                            The main part is removing those 10 characters. There's number of ways to extract the part you want from var:




                                            • printf (pretty portable, doesn't rely on specific shell)



                                              $ printf "%.11sn" "$var" 
                                              type_cardio



                                            • awk



                                              $ var=type_cardio_10-11-2017 
                                              $ awk -v awk_var="$var" 'BEGINprint substr(awk_var,0,length(awk_var)-11)'
                                              type_cardio


                                              or you can use printf trick here as well:



                                              $ echo "$var" | awk 'printf "%.11sn",$0' 
                                              type_cardio



                                            • egrep



                                              $ echo "$var" | egrep -o '^.0,11' 
                                              type_cardio



                                            • perl:



                                              $ echo "$var" | perl -lne 'print substr($_,0,11)' 
                                              type_cardio



                                            • python:



                                              $ python -c 'import sys;print sys.argv[1][0:11] ' "$var" 
                                              type_cardio






                                            share|improve this answer
























                                              up vote
                                              1
                                              down vote










                                              up vote
                                              1
                                              down vote









                                              Appending var and var2 is the easy part - you can just join them like new_var="$var$var2". If what you really meant to say is you want to store cropped var into var2,then it's just var2=$(...) and you can put the commands that other users and my answer presented here inside the $() portion.



                                              The main part is removing those 10 characters. There's number of ways to extract the part you want from var:




                                              • printf (pretty portable, doesn't rely on specific shell)



                                                $ printf "%.11sn" "$var" 
                                                type_cardio



                                              • awk



                                                $ var=type_cardio_10-11-2017 
                                                $ awk -v awk_var="$var" 'BEGINprint substr(awk_var,0,length(awk_var)-11)'
                                                type_cardio


                                                or you can use printf trick here as well:



                                                $ echo "$var" | awk 'printf "%.11sn",$0' 
                                                type_cardio



                                              • egrep



                                                $ echo "$var" | egrep -o '^.0,11' 
                                                type_cardio



                                              • perl:



                                                $ echo "$var" | perl -lne 'print substr($_,0,11)' 
                                                type_cardio



                                              • python:



                                                $ python -c 'import sys;print sys.argv[1][0:11] ' "$var" 
                                                type_cardio






                                              share|improve this answer














                                              Appending var and var2 is the easy part - you can just join them like new_var="$var$var2". If what you really meant to say is you want to store cropped var into var2,then it's just var2=$(...) and you can put the commands that other users and my answer presented here inside the $() portion.



                                              The main part is removing those 10 characters. There's number of ways to extract the part you want from var:




                                              • printf (pretty portable, doesn't rely on specific shell)



                                                $ printf "%.11sn" "$var" 
                                                type_cardio



                                              • awk



                                                $ var=type_cardio_10-11-2017 
                                                $ awk -v awk_var="$var" 'BEGINprint substr(awk_var,0,length(awk_var)-11)'
                                                type_cardio


                                                or you can use printf trick here as well:



                                                $ echo "$var" | awk 'printf "%.11sn",$0' 
                                                type_cardio



                                              • egrep



                                                $ echo "$var" | egrep -o '^.0,11' 
                                                type_cardio



                                              • perl:



                                                $ echo "$var" | perl -lne 'print substr($_,0,11)' 
                                                type_cardio



                                              • python:



                                                $ python -c 'import sys;print sys.argv[1][0:11] ' "$var" 
                                                type_cardio







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Oct 21 '17 at 17:20

























                                              answered Oct 21 '17 at 17:15









                                              Sergiy Kolodyazhnyy

                                              7,91011548




                                              7,91011548



























                                                   

                                                  draft saved


                                                  draft discarded















































                                                   


                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function ()
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f399392%2fhow-do-i-remove-the-last-characters-from-a-string%23new-answer', 'question_page');

                                                  );

                                                  Post as a guest













































































                                                  Popular posts from this blog

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

                                                  Christian Cage

                                                  How to properly install USB display driver for Fresco Logic FL2000DX on Ubuntu?