Command line argument in awk

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











up vote
1
down vote

favorite












I just want to implement if condition in awk. I created one file name : "simple_if" as below.



BEGIN
num=$1;
if (num%2==0)
printf "%d is Even number.n",num;
else printf "%d is odd Number.n",num



Then i executed the program by passing 10 as argument for $1 as below



awk -f simple_if 10


But it doesn't take input and instead displays 0.
Output:



0 is Even number.


How to get value from user in awk?










share|improve this question

























    up vote
    1
    down vote

    favorite












    I just want to implement if condition in awk. I created one file name : "simple_if" as below.



    BEGIN
    num=$1;
    if (num%2==0)
    printf "%d is Even number.n",num;
    else printf "%d is odd Number.n",num



    Then i executed the program by passing 10 as argument for $1 as below



    awk -f simple_if 10


    But it doesn't take input and instead displays 0.
    Output:



    0 is Even number.


    How to get value from user in awk?










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I just want to implement if condition in awk. I created one file name : "simple_if" as below.



      BEGIN
      num=$1;
      if (num%2==0)
      printf "%d is Even number.n",num;
      else printf "%d is odd Number.n",num



      Then i executed the program by passing 10 as argument for $1 as below



      awk -f simple_if 10


      But it doesn't take input and instead displays 0.
      Output:



      0 is Even number.


      How to get value from user in awk?










      share|improve this question













      I just want to implement if condition in awk. I created one file name : "simple_if" as below.



      BEGIN
      num=$1;
      if (num%2==0)
      printf "%d is Even number.n",num;
      else printf "%d is odd Number.n",num



      Then i executed the program by passing 10 as argument for $1 as below



      awk -f simple_if 10


      But it doesn't take input and instead displays 0.
      Output:



      0 is Even number.


      How to get value from user in awk?







      awk command-line






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 1 hour ago









      Dip

      205127




      205127




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          Arguments given at the end of the command line to awk are generally taken as filenames that the awk script will read from. To set a variable on the command line, use -v variable=value, e.g.



          awk -v num=10 -f script.awk


          This would enable you to use num as a variable in your script. The initial value of the variable will be 10 in the above example.



          You may also read environment variables using ENVIRON["variable"] in your script (for some environment variable named variable), or look at the command line arguments with ARGV[n] where n is some positive integer.




          With $1 in awk, you would refer to the value of the first field in the current record, but since you are using it in a BEGIN block, no data has yet been read from any file.



          The number in your code is being interpreted as zero since it's an empty variable used in an arithmetic context.






          share|improve this answer





























            up vote
            3
            down vote













            $1 is not the first command line argument, but the first field after the line was split with FS (and it will be the empty string in BEGIN, since no line was split yet).



            Command line arguments are in the array ARGV:



            $ awk 'BEGIN for(i = 1; i < ARGC; i++) print ARGV[i] ' 1st 2nd 3rd
            1st
            2nd
            3rd


            ARGV[0] is always the name of the interpreter (awk or gawk, etc).



            In order to let awk ignore a command line argument and not try to open it later as a file you should delete it or set it to the empty string: eg. ARGV[1]="".



            As a side note, any argument of the form var=value will also be interpreted as a variable assignment by awk, and will be eval'ed after the file arguments that precede it have been processed:



            $ echo yes > file
            $ awk ' gsub(/e/, var); print ' var=1 file var=2 file var=3 file
            y1s
            y2s
            y3s


            To use an actual filename of the form key=val with awk, you should pass it as a relative or absolute path eg. awk '...' ./key=val.






            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%2f475008%2fcommand-line-argument-in-awk%23new-answer', 'question_page');

              );

              Post as a guest






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              4
              down vote



              accepted










              Arguments given at the end of the command line to awk are generally taken as filenames that the awk script will read from. To set a variable on the command line, use -v variable=value, e.g.



              awk -v num=10 -f script.awk


              This would enable you to use num as a variable in your script. The initial value of the variable will be 10 in the above example.



              You may also read environment variables using ENVIRON["variable"] in your script (for some environment variable named variable), or look at the command line arguments with ARGV[n] where n is some positive integer.




              With $1 in awk, you would refer to the value of the first field in the current record, but since you are using it in a BEGIN block, no data has yet been read from any file.



              The number in your code is being interpreted as zero since it's an empty variable used in an arithmetic context.






              share|improve this answer


























                up vote
                4
                down vote



                accepted










                Arguments given at the end of the command line to awk are generally taken as filenames that the awk script will read from. To set a variable on the command line, use -v variable=value, e.g.



                awk -v num=10 -f script.awk


                This would enable you to use num as a variable in your script. The initial value of the variable will be 10 in the above example.



                You may also read environment variables using ENVIRON["variable"] in your script (for some environment variable named variable), or look at the command line arguments with ARGV[n] where n is some positive integer.




                With $1 in awk, you would refer to the value of the first field in the current record, but since you are using it in a BEGIN block, no data has yet been read from any file.



                The number in your code is being interpreted as zero since it's an empty variable used in an arithmetic context.






                share|improve this answer
























                  up vote
                  4
                  down vote



                  accepted







                  up vote
                  4
                  down vote



                  accepted






                  Arguments given at the end of the command line to awk are generally taken as filenames that the awk script will read from. To set a variable on the command line, use -v variable=value, e.g.



                  awk -v num=10 -f script.awk


                  This would enable you to use num as a variable in your script. The initial value of the variable will be 10 in the above example.



                  You may also read environment variables using ENVIRON["variable"] in your script (for some environment variable named variable), or look at the command line arguments with ARGV[n] where n is some positive integer.




                  With $1 in awk, you would refer to the value of the first field in the current record, but since you are using it in a BEGIN block, no data has yet been read from any file.



                  The number in your code is being interpreted as zero since it's an empty variable used in an arithmetic context.






                  share|improve this answer














                  Arguments given at the end of the command line to awk are generally taken as filenames that the awk script will read from. To set a variable on the command line, use -v variable=value, e.g.



                  awk -v num=10 -f script.awk


                  This would enable you to use num as a variable in your script. The initial value of the variable will be 10 in the above example.



                  You may also read environment variables using ENVIRON["variable"] in your script (for some environment variable named variable), or look at the command line arguments with ARGV[n] where n is some positive integer.




                  With $1 in awk, you would refer to the value of the first field in the current record, but since you are using it in a BEGIN block, no data has yet been read from any file.



                  The number in your code is being interpreted as zero since it's an empty variable used in an arithmetic context.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 10 mins ago

























                  answered 1 hour ago









                  Kusalananda

                  109k14211334




                  109k14211334






















                      up vote
                      3
                      down vote













                      $1 is not the first command line argument, but the first field after the line was split with FS (and it will be the empty string in BEGIN, since no line was split yet).



                      Command line arguments are in the array ARGV:



                      $ awk 'BEGIN for(i = 1; i < ARGC; i++) print ARGV[i] ' 1st 2nd 3rd
                      1st
                      2nd
                      3rd


                      ARGV[0] is always the name of the interpreter (awk or gawk, etc).



                      In order to let awk ignore a command line argument and not try to open it later as a file you should delete it or set it to the empty string: eg. ARGV[1]="".



                      As a side note, any argument of the form var=value will also be interpreted as a variable assignment by awk, and will be eval'ed after the file arguments that precede it have been processed:



                      $ echo yes > file
                      $ awk ' gsub(/e/, var); print ' var=1 file var=2 file var=3 file
                      y1s
                      y2s
                      y3s


                      To use an actual filename of the form key=val with awk, you should pass it as a relative or absolute path eg. awk '...' ./key=val.






                      share|improve this answer


























                        up vote
                        3
                        down vote













                        $1 is not the first command line argument, but the first field after the line was split with FS (and it will be the empty string in BEGIN, since no line was split yet).



                        Command line arguments are in the array ARGV:



                        $ awk 'BEGIN for(i = 1; i < ARGC; i++) print ARGV[i] ' 1st 2nd 3rd
                        1st
                        2nd
                        3rd


                        ARGV[0] is always the name of the interpreter (awk or gawk, etc).



                        In order to let awk ignore a command line argument and not try to open it later as a file you should delete it or set it to the empty string: eg. ARGV[1]="".



                        As a side note, any argument of the form var=value will also be interpreted as a variable assignment by awk, and will be eval'ed after the file arguments that precede it have been processed:



                        $ echo yes > file
                        $ awk ' gsub(/e/, var); print ' var=1 file var=2 file var=3 file
                        y1s
                        y2s
                        y3s


                        To use an actual filename of the form key=val with awk, you should pass it as a relative or absolute path eg. awk '...' ./key=val.






                        share|improve this answer
























                          up vote
                          3
                          down vote










                          up vote
                          3
                          down vote









                          $1 is not the first command line argument, but the first field after the line was split with FS (and it will be the empty string in BEGIN, since no line was split yet).



                          Command line arguments are in the array ARGV:



                          $ awk 'BEGIN for(i = 1; i < ARGC; i++) print ARGV[i] ' 1st 2nd 3rd
                          1st
                          2nd
                          3rd


                          ARGV[0] is always the name of the interpreter (awk or gawk, etc).



                          In order to let awk ignore a command line argument and not try to open it later as a file you should delete it or set it to the empty string: eg. ARGV[1]="".



                          As a side note, any argument of the form var=value will also be interpreted as a variable assignment by awk, and will be eval'ed after the file arguments that precede it have been processed:



                          $ echo yes > file
                          $ awk ' gsub(/e/, var); print ' var=1 file var=2 file var=3 file
                          y1s
                          y2s
                          y3s


                          To use an actual filename of the form key=val with awk, you should pass it as a relative or absolute path eg. awk '...' ./key=val.






                          share|improve this answer














                          $1 is not the first command line argument, but the first field after the line was split with FS (and it will be the empty string in BEGIN, since no line was split yet).



                          Command line arguments are in the array ARGV:



                          $ awk 'BEGIN for(i = 1; i < ARGC; i++) print ARGV[i] ' 1st 2nd 3rd
                          1st
                          2nd
                          3rd


                          ARGV[0] is always the name of the interpreter (awk or gawk, etc).



                          In order to let awk ignore a command line argument and not try to open it later as a file you should delete it or set it to the empty string: eg. ARGV[1]="".



                          As a side note, any argument of the form var=value will also be interpreted as a variable assignment by awk, and will be eval'ed after the file arguments that precede it have been processed:



                          $ echo yes > file
                          $ awk ' gsub(/e/, var); print ' var=1 file var=2 file var=3 file
                          y1s
                          y2s
                          y3s


                          To use an actual filename of the form key=val with awk, you should pass it as a relative or absolute path eg. awk '...' ./key=val.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 22 mins ago

























                          answered 1 hour ago









                          mosvy

                          2,292114




                          2,292114



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f475008%2fcommand-line-argument-in-awk%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Comments

                              Popular posts from this blog

                              How to decode/decipher Mozilla Firefox proprietary .jsonlz4 format? (sessionstore-backups/recovery.jsonlz4)

                              White Anglo-Saxon Protestant

                              How to be valuable when automation & IT are stealing my job (and maybe my whole career)? [closed]