Variable Substitution

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











up vote
2
down vote

favorite












I have a variable assigned to a returned string:



ytd_wk=$(cat file.csv | grep $(date +'%Y') | tail -1)



I want to substring last 2 characters:



ytd_wk=$ytd_wk:(-2)



Is there any way to use one-liner to achieve this? I have tried below but got bad substitution error:



ytd_wk=$ tail -1):(-2)







share|improve this question


















  • 2




    How do you define “one-liner”?  You could just put your first two commands together on one line, separated by && or ;.
    – G-Man
    Nov 20 '17 at 21:03















up vote
2
down vote

favorite












I have a variable assigned to a returned string:



ytd_wk=$(cat file.csv | grep $(date +'%Y') | tail -1)



I want to substring last 2 characters:



ytd_wk=$ytd_wk:(-2)



Is there any way to use one-liner to achieve this? I have tried below but got bad substitution error:



ytd_wk=$ tail -1):(-2)







share|improve this question


















  • 2




    How do you define “one-liner”?  You could just put your first two commands together on one line, separated by && or ;.
    – G-Man
    Nov 20 '17 at 21:03













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have a variable assigned to a returned string:



ytd_wk=$(cat file.csv | grep $(date +'%Y') | tail -1)



I want to substring last 2 characters:



ytd_wk=$ytd_wk:(-2)



Is there any way to use one-liner to achieve this? I have tried below but got bad substitution error:



ytd_wk=$ tail -1):(-2)







share|improve this question














I have a variable assigned to a returned string:



ytd_wk=$(cat file.csv | grep $(date +'%Y') | tail -1)



I want to substring last 2 characters:



ytd_wk=$ytd_wk:(-2)



Is there any way to use one-liner to achieve this? I have tried below but got bad substitution error:



ytd_wk=$ tail -1):(-2)









share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '17 at 1:53

























asked Nov 20 '17 at 18:54









lovechillcool

336




336







  • 2




    How do you define “one-liner”?  You could just put your first two commands together on one line, separated by && or ;.
    – G-Man
    Nov 20 '17 at 21:03













  • 2




    How do you define “one-liner”?  You could just put your first two commands together on one line, separated by && or ;.
    – G-Man
    Nov 20 '17 at 21:03








2




2




How do you define “one-liner”?  You could just put your first two commands together on one line, separated by && or ;.
– G-Man
Nov 20 '17 at 21:03





How do you define “one-liner”?  You could just put your first two commands together on one line, separated by && or ;.
– G-Man
Nov 20 '17 at 21:03











3 Answers
3






active

oldest

votes

















up vote
3
down vote



accepted










Try to use:



grep "$(date +'%Y')" file.csv | tail -1 | sed 's/.*(..$)/1/'


You don't need cat because grep can get data both from output of another program or from certain file. The last method more efficient because it uses only one command and this is more fast and consume less system resources.



The full solution:



ytd_wk=$(grep "$(date +'%Y')" file.csv | tail -1 | sed 's/.*(..$)/1/')


You can omit tail with GNU sed '$!d':



grep "$(date +'%Y')" file.csv | sed -r '$!d;s/.*(..$)/1/'


POSIX:



grep "$(date +'%Y')" file.csv | sed -e '$!d' -e 's/.*(..$)/1/'





share|improve this answer


















  • 2




    +1 for not using cat in your answer — although it would have been better it you had explained why cat is unnecessary.
    – G-Man
    Nov 20 '17 at 21:00

















up vote
1
down vote













You could use awk, combining the cat, grep, sed, and tail of other suggestions:



awk -v year=$(date +'%Y') '$0 ~ year line=$0 END print substr(line,length(line)-1,2)' file.csv


Writing this out step-by-step




  • -v year=$(date +'%Y') sets the awk variable year to the current year


  • $0 ~ year line=$0 this is applied to each line of the file in turn. If there is a match to the year in the current line it saves it in the awk variable line


  • END print substr(line,length(line)-1,2) at the end of the file (after the last line has been read and processed) this prints the last two characters of the most recently saved line. It prints a blank line if there was no earlier successful match.





share|improve this answer



























    up vote
    1
    down vote













    This is more efficient, especially if file.csv is large and the desired line is nearer to the end:



    ytd_wk="$(tac file.csv | grep -m 1 $(date +'%Y') | grep -o '..$')"


    How it works: tac outputs file.csv backwards, and grep -m 1 finds the first instance of the pattern, which is fed to grep -o which outputs only the last two chars.






    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%2f405827%2fvariable-substitution%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote



      accepted










      Try to use:



      grep "$(date +'%Y')" file.csv | tail -1 | sed 's/.*(..$)/1/'


      You don't need cat because grep can get data both from output of another program or from certain file. The last method more efficient because it uses only one command and this is more fast and consume less system resources.



      The full solution:



      ytd_wk=$(grep "$(date +'%Y')" file.csv | tail -1 | sed 's/.*(..$)/1/')


      You can omit tail with GNU sed '$!d':



      grep "$(date +'%Y')" file.csv | sed -r '$!d;s/.*(..$)/1/'


      POSIX:



      grep "$(date +'%Y')" file.csv | sed -e '$!d' -e 's/.*(..$)/1/'





      share|improve this answer


















      • 2




        +1 for not using cat in your answer — although it would have been better it you had explained why cat is unnecessary.
        – G-Man
        Nov 20 '17 at 21:00














      up vote
      3
      down vote



      accepted










      Try to use:



      grep "$(date +'%Y')" file.csv | tail -1 | sed 's/.*(..$)/1/'


      You don't need cat because grep can get data both from output of another program or from certain file. The last method more efficient because it uses only one command and this is more fast and consume less system resources.



      The full solution:



      ytd_wk=$(grep "$(date +'%Y')" file.csv | tail -1 | sed 's/.*(..$)/1/')


      You can omit tail with GNU sed '$!d':



      grep "$(date +'%Y')" file.csv | sed -r '$!d;s/.*(..$)/1/'


      POSIX:



      grep "$(date +'%Y')" file.csv | sed -e '$!d' -e 's/.*(..$)/1/'





      share|improve this answer


















      • 2




        +1 for not using cat in your answer — although it would have been better it you had explained why cat is unnecessary.
        – G-Man
        Nov 20 '17 at 21:00












      up vote
      3
      down vote



      accepted







      up vote
      3
      down vote



      accepted






      Try to use:



      grep "$(date +'%Y')" file.csv | tail -1 | sed 's/.*(..$)/1/'


      You don't need cat because grep can get data both from output of another program or from certain file. The last method more efficient because it uses only one command and this is more fast and consume less system resources.



      The full solution:



      ytd_wk=$(grep "$(date +'%Y')" file.csv | tail -1 | sed 's/.*(..$)/1/')


      You can omit tail with GNU sed '$!d':



      grep "$(date +'%Y')" file.csv | sed -r '$!d;s/.*(..$)/1/'


      POSIX:



      grep "$(date +'%Y')" file.csv | sed -e '$!d' -e 's/.*(..$)/1/'





      share|improve this answer














      Try to use:



      grep "$(date +'%Y')" file.csv | tail -1 | sed 's/.*(..$)/1/'


      You don't need cat because grep can get data both from output of another program or from certain file. The last method more efficient because it uses only one command and this is more fast and consume less system resources.



      The full solution:



      ytd_wk=$(grep "$(date +'%Y')" file.csv | tail -1 | sed 's/.*(..$)/1/')


      You can omit tail with GNU sed '$!d':



      grep "$(date +'%Y')" file.csv | sed -r '$!d;s/.*(..$)/1/'


      POSIX:



      grep "$(date +'%Y')" file.csv | sed -e '$!d' -e 's/.*(..$)/1/'






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 21 '17 at 6:59

























      answered Nov 20 '17 at 19:09









      Egor Vasilyev

      1,792129




      1,792129







      • 2




        +1 for not using cat in your answer — although it would have been better it you had explained why cat is unnecessary.
        – G-Man
        Nov 20 '17 at 21:00












      • 2




        +1 for not using cat in your answer — although it would have been better it you had explained why cat is unnecessary.
        – G-Man
        Nov 20 '17 at 21:00







      2




      2




      +1 for not using cat in your answer — although it would have been better it you had explained why cat is unnecessary.
      – G-Man
      Nov 20 '17 at 21:00




      +1 for not using cat in your answer — although it would have been better it you had explained why cat is unnecessary.
      – G-Man
      Nov 20 '17 at 21:00












      up vote
      1
      down vote













      You could use awk, combining the cat, grep, sed, and tail of other suggestions:



      awk -v year=$(date +'%Y') '$0 ~ year line=$0 END print substr(line,length(line)-1,2)' file.csv


      Writing this out step-by-step




      • -v year=$(date +'%Y') sets the awk variable year to the current year


      • $0 ~ year line=$0 this is applied to each line of the file in turn. If there is a match to the year in the current line it saves it in the awk variable line


      • END print substr(line,length(line)-1,2) at the end of the file (after the last line has been read and processed) this prints the last two characters of the most recently saved line. It prints a blank line if there was no earlier successful match.





      share|improve this answer
























        up vote
        1
        down vote













        You could use awk, combining the cat, grep, sed, and tail of other suggestions:



        awk -v year=$(date +'%Y') '$0 ~ year line=$0 END print substr(line,length(line)-1,2)' file.csv


        Writing this out step-by-step




        • -v year=$(date +'%Y') sets the awk variable year to the current year


        • $0 ~ year line=$0 this is applied to each line of the file in turn. If there is a match to the year in the current line it saves it in the awk variable line


        • END print substr(line,length(line)-1,2) at the end of the file (after the last line has been read and processed) this prints the last two characters of the most recently saved line. It prints a blank line if there was no earlier successful match.





        share|improve this answer






















          up vote
          1
          down vote










          up vote
          1
          down vote









          You could use awk, combining the cat, grep, sed, and tail of other suggestions:



          awk -v year=$(date +'%Y') '$0 ~ year line=$0 END print substr(line,length(line)-1,2)' file.csv


          Writing this out step-by-step




          • -v year=$(date +'%Y') sets the awk variable year to the current year


          • $0 ~ year line=$0 this is applied to each line of the file in turn. If there is a match to the year in the current line it saves it in the awk variable line


          • END print substr(line,length(line)-1,2) at the end of the file (after the last line has been read and processed) this prints the last two characters of the most recently saved line. It prints a blank line if there was no earlier successful match.





          share|improve this answer












          You could use awk, combining the cat, grep, sed, and tail of other suggestions:



          awk -v year=$(date +'%Y') '$0 ~ year line=$0 END print substr(line,length(line)-1,2)' file.csv


          Writing this out step-by-step




          • -v year=$(date +'%Y') sets the awk variable year to the current year


          • $0 ~ year line=$0 this is applied to each line of the file in turn. If there is a match to the year in the current line it saves it in the awk variable line


          • END print substr(line,length(line)-1,2) at the end of the file (after the last line has been read and processed) this prints the last two characters of the most recently saved line. It prints a blank line if there was no earlier successful match.






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '17 at 23:47









          roaima

          39.9k546109




          39.9k546109




















              up vote
              1
              down vote













              This is more efficient, especially if file.csv is large and the desired line is nearer to the end:



              ytd_wk="$(tac file.csv | grep -m 1 $(date +'%Y') | grep -o '..$')"


              How it works: tac outputs file.csv backwards, and grep -m 1 finds the first instance of the pattern, which is fed to grep -o which outputs only the last two chars.






              share|improve this answer


























                up vote
                1
                down vote













                This is more efficient, especially if file.csv is large and the desired line is nearer to the end:



                ytd_wk="$(tac file.csv | grep -m 1 $(date +'%Y') | grep -o '..$')"


                How it works: tac outputs file.csv backwards, and grep -m 1 finds the first instance of the pattern, which is fed to grep -o which outputs only the last two chars.






                share|improve this answer
























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  This is more efficient, especially if file.csv is large and the desired line is nearer to the end:



                  ytd_wk="$(tac file.csv | grep -m 1 $(date +'%Y') | grep -o '..$')"


                  How it works: tac outputs file.csv backwards, and grep -m 1 finds the first instance of the pattern, which is fed to grep -o which outputs only the last two chars.






                  share|improve this answer














                  This is more efficient, especially if file.csv is large and the desired line is nearer to the end:



                  ytd_wk="$(tac file.csv | grep -m 1 $(date +'%Y') | grep -o '..$')"


                  How it works: tac outputs file.csv backwards, and grep -m 1 finds the first instance of the pattern, which is fed to grep -o which outputs only the last two chars.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 21 '17 at 9:25

























                  answered Nov 21 '17 at 1:42









                  agc

                  4,1101935




                  4,1101935



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f405827%2fvariable-substitution%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      Peggy Mitchell

                      The Forum (Inglewood, California)

                      Palaiologos