awk syntax error in bash. Works fine in zsh










0














I have written the following script that extracts a number from an rss file.



#!/bin/sh
wget -O selic https://conteudo.bcb.gov.br/api/feed/pt-br/PAINEL_INDICADORES/juros
line=$(grep 'dailyratevalue' selic)
index=$(awk -v var=$line 'BEGIN print index(var, "dailyratevalue") ')
end=$((index+21))
echo $line | cut -c $index-$end | tail -c 4 | tr ',' '.' > selic


In zsh it works perfectly, but i need it to work in bash, too. I have tried running it on bash but i get the following error



awk: cmd. line:1: <content
awk: cmd. line:1: ^ syntax error


The error pattern <content comes from the line that is being fed as a parameter to awk, which makes no sense to me, since awk is just supposed to get me the position of the pattern i want.



What could this be?










share|improve this question

















  • 1




    Using grep to find a line number to pass to Awk is completely useless. Awk can match regular expressions just fine on its own, and the line number itself doesn't appear to add any intrinsic value here.
    – tripleee
    Nov 12 '18 at 8:01











  • @RCS : Parameter passing works differently in zsh and bash. In your case, you could double-quote "$line", but in general, it does not make much sense to construct a program which will work in zsh AND bash alike - there are too many differences. Note also that if you want to run a script in bash or zsh, it is dangerous to put /bin/sh in the #! line.
    – user1934428
    Nov 12 '18 at 10:26















0














I have written the following script that extracts a number from an rss file.



#!/bin/sh
wget -O selic https://conteudo.bcb.gov.br/api/feed/pt-br/PAINEL_INDICADORES/juros
line=$(grep 'dailyratevalue' selic)
index=$(awk -v var=$line 'BEGIN print index(var, "dailyratevalue") ')
end=$((index+21))
echo $line | cut -c $index-$end | tail -c 4 | tr ',' '.' > selic


In zsh it works perfectly, but i need it to work in bash, too. I have tried running it on bash but i get the following error



awk: cmd. line:1: <content
awk: cmd. line:1: ^ syntax error


The error pattern <content comes from the line that is being fed as a parameter to awk, which makes no sense to me, since awk is just supposed to get me the position of the pattern i want.



What could this be?










share|improve this question

















  • 1




    Using grep to find a line number to pass to Awk is completely useless. Awk can match regular expressions just fine on its own, and the line number itself doesn't appear to add any intrinsic value here.
    – tripleee
    Nov 12 '18 at 8:01











  • @RCS : Parameter passing works differently in zsh and bash. In your case, you could double-quote "$line", but in general, it does not make much sense to construct a program which will work in zsh AND bash alike - there are too many differences. Note also that if you want to run a script in bash or zsh, it is dangerous to put /bin/sh in the #! line.
    – user1934428
    Nov 12 '18 at 10:26













0












0








0







I have written the following script that extracts a number from an rss file.



#!/bin/sh
wget -O selic https://conteudo.bcb.gov.br/api/feed/pt-br/PAINEL_INDICADORES/juros
line=$(grep 'dailyratevalue' selic)
index=$(awk -v var=$line 'BEGIN print index(var, "dailyratevalue") ')
end=$((index+21))
echo $line | cut -c $index-$end | tail -c 4 | tr ',' '.' > selic


In zsh it works perfectly, but i need it to work in bash, too. I have tried running it on bash but i get the following error



awk: cmd. line:1: <content
awk: cmd. line:1: ^ syntax error


The error pattern <content comes from the line that is being fed as a parameter to awk, which makes no sense to me, since awk is just supposed to get me the position of the pattern i want.



What could this be?










share|improve this question













I have written the following script that extracts a number from an rss file.



#!/bin/sh
wget -O selic https://conteudo.bcb.gov.br/api/feed/pt-br/PAINEL_INDICADORES/juros
line=$(grep 'dailyratevalue' selic)
index=$(awk -v var=$line 'BEGIN print index(var, "dailyratevalue") ')
end=$((index+21))
echo $line | cut -c $index-$end | tail -c 4 | tr ',' '.' > selic


In zsh it works perfectly, but i need it to work in bash, too. I have tried running it on bash but i get the following error



awk: cmd. line:1: <content
awk: cmd. line:1: ^ syntax error


The error pattern <content comes from the line that is being fed as a parameter to awk, which makes no sense to me, since awk is just supposed to get me the position of the pattern i want.



What could this be?







bash shell awk zsh






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 1:13









RCS

31




31







  • 1




    Using grep to find a line number to pass to Awk is completely useless. Awk can match regular expressions just fine on its own, and the line number itself doesn't appear to add any intrinsic value here.
    – tripleee
    Nov 12 '18 at 8:01











  • @RCS : Parameter passing works differently in zsh and bash. In your case, you could double-quote "$line", but in general, it does not make much sense to construct a program which will work in zsh AND bash alike - there are too many differences. Note also that if you want to run a script in bash or zsh, it is dangerous to put /bin/sh in the #! line.
    – user1934428
    Nov 12 '18 at 10:26












  • 1




    Using grep to find a line number to pass to Awk is completely useless. Awk can match regular expressions just fine on its own, and the line number itself doesn't appear to add any intrinsic value here.
    – tripleee
    Nov 12 '18 at 8:01











  • @RCS : Parameter passing works differently in zsh and bash. In your case, you could double-quote "$line", but in general, it does not make much sense to construct a program which will work in zsh AND bash alike - there are too many differences. Note also that if you want to run a script in bash or zsh, it is dangerous to put /bin/sh in the #! line.
    – user1934428
    Nov 12 '18 at 10:26







1




1




Using grep to find a line number to pass to Awk is completely useless. Awk can match regular expressions just fine on its own, and the line number itself doesn't appear to add any intrinsic value here.
– tripleee
Nov 12 '18 at 8:01





Using grep to find a line number to pass to Awk is completely useless. Awk can match regular expressions just fine on its own, and the line number itself doesn't appear to add any intrinsic value here.
– tripleee
Nov 12 '18 at 8:01













@RCS : Parameter passing works differently in zsh and bash. In your case, you could double-quote "$line", but in general, it does not make much sense to construct a program which will work in zsh AND bash alike - there are too many differences. Note also that if you want to run a script in bash or zsh, it is dangerous to put /bin/sh in the #! line.
– user1934428
Nov 12 '18 at 10:26




@RCS : Parameter passing works differently in zsh and bash. In your case, you could double-quote "$line", but in general, it does not make much sense to construct a program which will work in zsh AND bash alike - there are too many differences. Note also that if you want to run a script in bash or zsh, it is dangerous to put /bin/sh in the #! line.
– user1934428
Nov 12 '18 at 10:26












3 Answers
3






active

oldest

votes


















1














@DiegoTorresMilano's answer is probably better overall, but if you want to do it in bash, the main thing you need to do is double-quote your variable references. Without double-quotes around them, bash (and most shells other than zsh) splits variables into "words", and also expands anything that looks like a wildcard expression into a list of matching filenames. You almost never want this, so use double-quotes. In your case, there are two places they're needed: around $line here:



index=$(awk -v var="$line" 'BEGIN print index(var, "dailyratevalue") ')


and here:



echo "$line" | cut -c $index-$end | tail -c 4 | tr ',' '.' > selic


Note that you don't need double-quotes around the $( ) expressions, because they're on the right side of an assignment statement, and that isn't subject to word splitting and wildcard expansion. If they occurred elsewhere, you'd probably want double-quotes around them too.



BTW, shellcheck.net is really good at pointing out common mistakes like this, so I recommend running your scripts through it (even when they seem to be working correctly).






share|improve this answer




























    2














    index=$(awk -v var="$line" 'BEGIN print index(var, "dailyratevalue") ')



    should fix it.






    share|improve this answer




















    • That worked! But my output is not the desired one. Somehow the position of characters is different on bash and i ended up with junk instead of the number i need.
      – RCS
      Nov 12 '18 at 1:33










    • I am getting 249 for $index with both bash and zsh. What's yours?
      – Rico Chen
      Nov 12 '18 at 1:43










    • 249, too. The characters outputted to the file are different, tho. I get the 3 chars immediately before the number i need.
      – RCS
      Nov 12 '18 at 1:49


















    2














    awk can do all of the extra steps. You can just



    wget -qO - https://conteudo.bcb.gov.br/api/feed/pt-br/PAINEL_INDICADORES/juros | 
    awk -F '&[gl]t;' '/dailyratevalue/ sub(",", ".", $25); print $25;'


    and obtain the value you want.



    This is setting the FS and getting the field you want for the line that matches dailyratevalue.






    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%2f53254854%2fawk-syntax-error-in-bash-works-fine-in-zsh%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









      1














      @DiegoTorresMilano's answer is probably better overall, but if you want to do it in bash, the main thing you need to do is double-quote your variable references. Without double-quotes around them, bash (and most shells other than zsh) splits variables into "words", and also expands anything that looks like a wildcard expression into a list of matching filenames. You almost never want this, so use double-quotes. In your case, there are two places they're needed: around $line here:



      index=$(awk -v var="$line" 'BEGIN print index(var, "dailyratevalue") ')


      and here:



      echo "$line" | cut -c $index-$end | tail -c 4 | tr ',' '.' > selic


      Note that you don't need double-quotes around the $( ) expressions, because they're on the right side of an assignment statement, and that isn't subject to word splitting and wildcard expansion. If they occurred elsewhere, you'd probably want double-quotes around them too.



      BTW, shellcheck.net is really good at pointing out common mistakes like this, so I recommend running your scripts through it (even when they seem to be working correctly).






      share|improve this answer

























        1














        @DiegoTorresMilano's answer is probably better overall, but if you want to do it in bash, the main thing you need to do is double-quote your variable references. Without double-quotes around them, bash (and most shells other than zsh) splits variables into "words", and also expands anything that looks like a wildcard expression into a list of matching filenames. You almost never want this, so use double-quotes. In your case, there are two places they're needed: around $line here:



        index=$(awk -v var="$line" 'BEGIN print index(var, "dailyratevalue") ')


        and here:



        echo "$line" | cut -c $index-$end | tail -c 4 | tr ',' '.' > selic


        Note that you don't need double-quotes around the $( ) expressions, because they're on the right side of an assignment statement, and that isn't subject to word splitting and wildcard expansion. If they occurred elsewhere, you'd probably want double-quotes around them too.



        BTW, shellcheck.net is really good at pointing out common mistakes like this, so I recommend running your scripts through it (even when they seem to be working correctly).






        share|improve this answer























          1












          1








          1






          @DiegoTorresMilano's answer is probably better overall, but if you want to do it in bash, the main thing you need to do is double-quote your variable references. Without double-quotes around them, bash (and most shells other than zsh) splits variables into "words", and also expands anything that looks like a wildcard expression into a list of matching filenames. You almost never want this, so use double-quotes. In your case, there are two places they're needed: around $line here:



          index=$(awk -v var="$line" 'BEGIN print index(var, "dailyratevalue") ')


          and here:



          echo "$line" | cut -c $index-$end | tail -c 4 | tr ',' '.' > selic


          Note that you don't need double-quotes around the $( ) expressions, because they're on the right side of an assignment statement, and that isn't subject to word splitting and wildcard expansion. If they occurred elsewhere, you'd probably want double-quotes around them too.



          BTW, shellcheck.net is really good at pointing out common mistakes like this, so I recommend running your scripts through it (even when they seem to be working correctly).






          share|improve this answer












          @DiegoTorresMilano's answer is probably better overall, but if you want to do it in bash, the main thing you need to do is double-quote your variable references. Without double-quotes around them, bash (and most shells other than zsh) splits variables into "words", and also expands anything that looks like a wildcard expression into a list of matching filenames. You almost never want this, so use double-quotes. In your case, there are two places they're needed: around $line here:



          index=$(awk -v var="$line" 'BEGIN print index(var, "dailyratevalue") ')


          and here:



          echo "$line" | cut -c $index-$end | tail -c 4 | tr ',' '.' > selic


          Note that you don't need double-quotes around the $( ) expressions, because they're on the right side of an assignment statement, and that isn't subject to word splitting and wildcard expansion. If they occurred elsewhere, you'd probably want double-quotes around them too.



          BTW, shellcheck.net is really good at pointing out common mistakes like this, so I recommend running your scripts through it (even when they seem to be working correctly).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 '18 at 7:10









          Gordon Davisson

          67.7k97793




          67.7k97793























              2














              index=$(awk -v var="$line" 'BEGIN print index(var, "dailyratevalue") ')



              should fix it.






              share|improve this answer




















              • That worked! But my output is not the desired one. Somehow the position of characters is different on bash and i ended up with junk instead of the number i need.
                – RCS
                Nov 12 '18 at 1:33










              • I am getting 249 for $index with both bash and zsh. What's yours?
                – Rico Chen
                Nov 12 '18 at 1:43










              • 249, too. The characters outputted to the file are different, tho. I get the 3 chars immediately before the number i need.
                – RCS
                Nov 12 '18 at 1:49















              2














              index=$(awk -v var="$line" 'BEGIN print index(var, "dailyratevalue") ')



              should fix it.






              share|improve this answer




















              • That worked! But my output is not the desired one. Somehow the position of characters is different on bash and i ended up with junk instead of the number i need.
                – RCS
                Nov 12 '18 at 1:33










              • I am getting 249 for $index with both bash and zsh. What's yours?
                – Rico Chen
                Nov 12 '18 at 1:43










              • 249, too. The characters outputted to the file are different, tho. I get the 3 chars immediately before the number i need.
                – RCS
                Nov 12 '18 at 1:49













              2












              2








              2






              index=$(awk -v var="$line" 'BEGIN print index(var, "dailyratevalue") ')



              should fix it.






              share|improve this answer












              index=$(awk -v var="$line" 'BEGIN print index(var, "dailyratevalue") ')



              should fix it.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 12 '18 at 1:23









              Rico Chen

              69149




              69149











              • That worked! But my output is not the desired one. Somehow the position of characters is different on bash and i ended up with junk instead of the number i need.
                – RCS
                Nov 12 '18 at 1:33










              • I am getting 249 for $index with both bash and zsh. What's yours?
                – Rico Chen
                Nov 12 '18 at 1:43










              • 249, too. The characters outputted to the file are different, tho. I get the 3 chars immediately before the number i need.
                – RCS
                Nov 12 '18 at 1:49
















              • That worked! But my output is not the desired one. Somehow the position of characters is different on bash and i ended up with junk instead of the number i need.
                – RCS
                Nov 12 '18 at 1:33










              • I am getting 249 for $index with both bash and zsh. What's yours?
                – Rico Chen
                Nov 12 '18 at 1:43










              • 249, too. The characters outputted to the file are different, tho. I get the 3 chars immediately before the number i need.
                – RCS
                Nov 12 '18 at 1:49















              That worked! But my output is not the desired one. Somehow the position of characters is different on bash and i ended up with junk instead of the number i need.
              – RCS
              Nov 12 '18 at 1:33




              That worked! But my output is not the desired one. Somehow the position of characters is different on bash and i ended up with junk instead of the number i need.
              – RCS
              Nov 12 '18 at 1:33












              I am getting 249 for $index with both bash and zsh. What's yours?
              – Rico Chen
              Nov 12 '18 at 1:43




              I am getting 249 for $index with both bash and zsh. What's yours?
              – Rico Chen
              Nov 12 '18 at 1:43












              249, too. The characters outputted to the file are different, tho. I get the 3 chars immediately before the number i need.
              – RCS
              Nov 12 '18 at 1:49




              249, too. The characters outputted to the file are different, tho. I get the 3 chars immediately before the number i need.
              – RCS
              Nov 12 '18 at 1:49











              2














              awk can do all of the extra steps. You can just



              wget -qO - https://conteudo.bcb.gov.br/api/feed/pt-br/PAINEL_INDICADORES/juros | 
              awk -F '&[gl]t;' '/dailyratevalue/ sub(",", ".", $25); print $25;'


              and obtain the value you want.



              This is setting the FS and getting the field you want for the line that matches dailyratevalue.






              share|improve this answer

























                2














                awk can do all of the extra steps. You can just



                wget -qO - https://conteudo.bcb.gov.br/api/feed/pt-br/PAINEL_INDICADORES/juros | 
                awk -F '&[gl]t;' '/dailyratevalue/ sub(",", ".", $25); print $25;'


                and obtain the value you want.



                This is setting the FS and getting the field you want for the line that matches dailyratevalue.






                share|improve this answer























                  2












                  2








                  2






                  awk can do all of the extra steps. You can just



                  wget -qO - https://conteudo.bcb.gov.br/api/feed/pt-br/PAINEL_INDICADORES/juros | 
                  awk -F '&[gl]t;' '/dailyratevalue/ sub(",", ".", $25); print $25;'


                  and obtain the value you want.



                  This is setting the FS and getting the field you want for the line that matches dailyratevalue.






                  share|improve this answer












                  awk can do all of the extra steps. You can just



                  wget -qO - https://conteudo.bcb.gov.br/api/feed/pt-br/PAINEL_INDICADORES/juros | 
                  awk -F '&[gl]t;' '/dailyratevalue/ sub(",", ".", $25); print $25;'


                  and obtain the value you want.



                  This is setting the FS and getting the field you want for the line that matches dailyratevalue.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 '18 at 1:43









                  Diego Torres Milano

                  49.8k678107




                  49.8k678107



























                      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.





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


                      Please pay close attention to the following guidance:


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

                      But avoid


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

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

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




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254854%2fawk-syntax-error-in-bash-works-fine-in-zsh%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

                      Use pre created SQLite database for Android project in kotlin

                      Darth Vader #20

                      Ondo