How to prevent printing to stdout from an awk **script** (not from cli)












1















[GNU awk v4.2.1 on Archlinux]



Suppressing awk's default print action to stdout from cli is easy and well documented on UL, e.g. here. Doing so from a script gives me headaches. Here's the toy script:



#!/usr/bin/awk -f
BEGIN {FS=","}
FNR > 1 # skip header
{
if ( $1 == $2 ) {
if ( NR == 4 ) {
printf("*** Print NR=4 ok. n")
} else {
print > "/dev/null" # print nothing
}
} else {
printf("=== Fields 1 and 2 not equal (NR=%s). n",NR)
}
}


and toy data:



col1 col2
1,3
2,2
aa,aa
3.01,-353.01
4.1,4.1
101,101
hello, hello
asd,koi0


along with expected / desired output:



=== Fields 1 and 2 not equal (NR=2). 
*** Print NR=4 ok.
=== Fields 1 and 2 not equal (NR=5).
=== Fields 1 and 2 not equal (NR=8).
=== Fields 1 and 2 not equal (NR=9).


Instead I get:



=== Fields 1 and 2 not equal (NR=1). 
1,3
=== Fields 1 and 2 not equal (NR=2).
2,2
aa,aa
*** Print NR=4 ok.
3.01,-353.01
=== Fields 1 and 2 not equal (NR=5).
4.1,4.1
101,101
hello, hello
=== Fields 1 and 2 not equal (NR=8).
asd,koi0
=== Fields 1 and 2 not equal (NR=9).


To suppress output to stdout, I tried using:
getline, {}, next, printf("") and even the outlandish ORS=""; print ""; ORS="n" instead of print > "/dev/null". I am obviously doing something very wrong in that frigging script and can not find what ...










share|improve this question



























    1















    [GNU awk v4.2.1 on Archlinux]



    Suppressing awk's default print action to stdout from cli is easy and well documented on UL, e.g. here. Doing so from a script gives me headaches. Here's the toy script:



    #!/usr/bin/awk -f
    BEGIN {FS=","}
    FNR > 1 # skip header
    {
    if ( $1 == $2 ) {
    if ( NR == 4 ) {
    printf("*** Print NR=4 ok. n")
    } else {
    print > "/dev/null" # print nothing
    }
    } else {
    printf("=== Fields 1 and 2 not equal (NR=%s). n",NR)
    }
    }


    and toy data:



    col1 col2
    1,3
    2,2
    aa,aa
    3.01,-353.01
    4.1,4.1
    101,101
    hello, hello
    asd,koi0


    along with expected / desired output:



    === Fields 1 and 2 not equal (NR=2). 
    *** Print NR=4 ok.
    === Fields 1 and 2 not equal (NR=5).
    === Fields 1 and 2 not equal (NR=8).
    === Fields 1 and 2 not equal (NR=9).


    Instead I get:



    === Fields 1 and 2 not equal (NR=1). 
    1,3
    === Fields 1 and 2 not equal (NR=2).
    2,2
    aa,aa
    *** Print NR=4 ok.
    3.01,-353.01
    === Fields 1 and 2 not equal (NR=5).
    4.1,4.1
    101,101
    hello, hello
    === Fields 1 and 2 not equal (NR=8).
    asd,koi0
    === Fields 1 and 2 not equal (NR=9).


    To suppress output to stdout, I tried using:
    getline, {}, next, printf("") and even the outlandish ORS=""; print ""; ORS="n" instead of print > "/dev/null". I am obviously doing something very wrong in that frigging script and can not find what ...










    share|improve this question

























      1












      1








      1








      [GNU awk v4.2.1 on Archlinux]



      Suppressing awk's default print action to stdout from cli is easy and well documented on UL, e.g. here. Doing so from a script gives me headaches. Here's the toy script:



      #!/usr/bin/awk -f
      BEGIN {FS=","}
      FNR > 1 # skip header
      {
      if ( $1 == $2 ) {
      if ( NR == 4 ) {
      printf("*** Print NR=4 ok. n")
      } else {
      print > "/dev/null" # print nothing
      }
      } else {
      printf("=== Fields 1 and 2 not equal (NR=%s). n",NR)
      }
      }


      and toy data:



      col1 col2
      1,3
      2,2
      aa,aa
      3.01,-353.01
      4.1,4.1
      101,101
      hello, hello
      asd,koi0


      along with expected / desired output:



      === Fields 1 and 2 not equal (NR=2). 
      *** Print NR=4 ok.
      === Fields 1 and 2 not equal (NR=5).
      === Fields 1 and 2 not equal (NR=8).
      === Fields 1 and 2 not equal (NR=9).


      Instead I get:



      === Fields 1 and 2 not equal (NR=1). 
      1,3
      === Fields 1 and 2 not equal (NR=2).
      2,2
      aa,aa
      *** Print NR=4 ok.
      3.01,-353.01
      === Fields 1 and 2 not equal (NR=5).
      4.1,4.1
      101,101
      hello, hello
      === Fields 1 and 2 not equal (NR=8).
      asd,koi0
      === Fields 1 and 2 not equal (NR=9).


      To suppress output to stdout, I tried using:
      getline, {}, next, printf("") and even the outlandish ORS=""; print ""; ORS="n" instead of print > "/dev/null". I am obviously doing something very wrong in that frigging script and can not find what ...










      share|improve this question














      [GNU awk v4.2.1 on Archlinux]



      Suppressing awk's default print action to stdout from cli is easy and well documented on UL, e.g. here. Doing so from a script gives me headaches. Here's the toy script:



      #!/usr/bin/awk -f
      BEGIN {FS=","}
      FNR > 1 # skip header
      {
      if ( $1 == $2 ) {
      if ( NR == 4 ) {
      printf("*** Print NR=4 ok. n")
      } else {
      print > "/dev/null" # print nothing
      }
      } else {
      printf("=== Fields 1 and 2 not equal (NR=%s). n",NR)
      }
      }


      and toy data:



      col1 col2
      1,3
      2,2
      aa,aa
      3.01,-353.01
      4.1,4.1
      101,101
      hello, hello
      asd,koi0


      along with expected / desired output:



      === Fields 1 and 2 not equal (NR=2). 
      *** Print NR=4 ok.
      === Fields 1 and 2 not equal (NR=5).
      === Fields 1 and 2 not equal (NR=8).
      === Fields 1 and 2 not equal (NR=9).


      Instead I get:



      === Fields 1 and 2 not equal (NR=1). 
      1,3
      === Fields 1 and 2 not equal (NR=2).
      2,2
      aa,aa
      *** Print NR=4 ok.
      3.01,-353.01
      === Fields 1 and 2 not equal (NR=5).
      4.1,4.1
      101,101
      hello, hello
      === Fields 1 and 2 not equal (NR=8).
      asd,koi0
      === Fields 1 and 2 not equal (NR=9).


      To suppress output to stdout, I tried using:
      getline, {}, next, printf("") and even the outlandish ORS=""; print ""; ORS="n" instead of print > "/dev/null". I am obviously doing something very wrong in that frigging script and can not find what ...







      awk scripting stdout






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 3 hours ago









      CbhiheCbhihe

      3691317




      3691317






















          1 Answer
          1






          active

          oldest

          votes


















          1














          The only error is



          FNR > 1          # skip header
          {


          which should be



          FNR > 1 {        # skip header


          A code block with a condition must start on the same line as the condition.



          What your original script actually does is first



          FNR > 1


          This prints all lines from line two onwards (the default action when a condition does not have an associated code block is to print the current record if the condition is true, as if the block had been { print }).



          Then it applies the block following that to each line (since that block does not have an associated condition).



          This is not a peculiarity of GNU awk. All awk implementations should act like this.





          As for the other bits of the script:



              } else {
          print > "/dev/null" # print nothing
          }


          This could be deleted, leaving



          #!/usr/bin/awk -f

          BEGIN { FS = "," }

          FNR > 1 {
          if ( $1 == $2 ) {
          if ( NR == 4 )
          printf("*** Print NR=4 ok.n")
          } else
          printf("=== Fields 1 and 2 not equal (NR=%s).n", NR)
          }


          or,



          #!/usr/bin/awk -f

          BEGIN { FS = "," }

          FNR == 1 { next }

          $1 == $2 && NR == 4 { printf("*** Print NR=4 ok.n") }
          $1 != $2 { printf("=== Fields 1 and 2 not equal (NR=%s).n", NR) }





          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',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f506180%2fhow-to-prevent-printing-to-stdout-from-an-awk-script-not-from-cli%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            The only error is



            FNR > 1          # skip header
            {


            which should be



            FNR > 1 {        # skip header


            A code block with a condition must start on the same line as the condition.



            What your original script actually does is first



            FNR > 1


            This prints all lines from line two onwards (the default action when a condition does not have an associated code block is to print the current record if the condition is true, as if the block had been { print }).



            Then it applies the block following that to each line (since that block does not have an associated condition).



            This is not a peculiarity of GNU awk. All awk implementations should act like this.





            As for the other bits of the script:



                } else {
            print > "/dev/null" # print nothing
            }


            This could be deleted, leaving



            #!/usr/bin/awk -f

            BEGIN { FS = "," }

            FNR > 1 {
            if ( $1 == $2 ) {
            if ( NR == 4 )
            printf("*** Print NR=4 ok.n")
            } else
            printf("=== Fields 1 and 2 not equal (NR=%s).n", NR)
            }


            or,



            #!/usr/bin/awk -f

            BEGIN { FS = "," }

            FNR == 1 { next }

            $1 == $2 && NR == 4 { printf("*** Print NR=4 ok.n") }
            $1 != $2 { printf("=== Fields 1 and 2 not equal (NR=%s).n", NR) }





            share|improve this answer






























              1














              The only error is



              FNR > 1          # skip header
              {


              which should be



              FNR > 1 {        # skip header


              A code block with a condition must start on the same line as the condition.



              What your original script actually does is first



              FNR > 1


              This prints all lines from line two onwards (the default action when a condition does not have an associated code block is to print the current record if the condition is true, as if the block had been { print }).



              Then it applies the block following that to each line (since that block does not have an associated condition).



              This is not a peculiarity of GNU awk. All awk implementations should act like this.





              As for the other bits of the script:



                  } else {
              print > "/dev/null" # print nothing
              }


              This could be deleted, leaving



              #!/usr/bin/awk -f

              BEGIN { FS = "," }

              FNR > 1 {
              if ( $1 == $2 ) {
              if ( NR == 4 )
              printf("*** Print NR=4 ok.n")
              } else
              printf("=== Fields 1 and 2 not equal (NR=%s).n", NR)
              }


              or,



              #!/usr/bin/awk -f

              BEGIN { FS = "," }

              FNR == 1 { next }

              $1 == $2 && NR == 4 { printf("*** Print NR=4 ok.n") }
              $1 != $2 { printf("=== Fields 1 and 2 not equal (NR=%s).n", NR) }





              share|improve this answer




























                1












                1








                1







                The only error is



                FNR > 1          # skip header
                {


                which should be



                FNR > 1 {        # skip header


                A code block with a condition must start on the same line as the condition.



                What your original script actually does is first



                FNR > 1


                This prints all lines from line two onwards (the default action when a condition does not have an associated code block is to print the current record if the condition is true, as if the block had been { print }).



                Then it applies the block following that to each line (since that block does not have an associated condition).



                This is not a peculiarity of GNU awk. All awk implementations should act like this.





                As for the other bits of the script:



                    } else {
                print > "/dev/null" # print nothing
                }


                This could be deleted, leaving



                #!/usr/bin/awk -f

                BEGIN { FS = "," }

                FNR > 1 {
                if ( $1 == $2 ) {
                if ( NR == 4 )
                printf("*** Print NR=4 ok.n")
                } else
                printf("=== Fields 1 and 2 not equal (NR=%s).n", NR)
                }


                or,



                #!/usr/bin/awk -f

                BEGIN { FS = "," }

                FNR == 1 { next }

                $1 == $2 && NR == 4 { printf("*** Print NR=4 ok.n") }
                $1 != $2 { printf("=== Fields 1 and 2 not equal (NR=%s).n", NR) }





                share|improve this answer















                The only error is



                FNR > 1          # skip header
                {


                which should be



                FNR > 1 {        # skip header


                A code block with a condition must start on the same line as the condition.



                What your original script actually does is first



                FNR > 1


                This prints all lines from line two onwards (the default action when a condition does not have an associated code block is to print the current record if the condition is true, as if the block had been { print }).



                Then it applies the block following that to each line (since that block does not have an associated condition).



                This is not a peculiarity of GNU awk. All awk implementations should act like this.





                As for the other bits of the script:



                    } else {
                print > "/dev/null" # print nothing
                }


                This could be deleted, leaving



                #!/usr/bin/awk -f

                BEGIN { FS = "," }

                FNR > 1 {
                if ( $1 == $2 ) {
                if ( NR == 4 )
                printf("*** Print NR=4 ok.n")
                } else
                printf("=== Fields 1 and 2 not equal (NR=%s).n", NR)
                }


                or,



                #!/usr/bin/awk -f

                BEGIN { FS = "," }

                FNR == 1 { next }

                $1 == $2 && NR == 4 { printf("*** Print NR=4 ok.n") }
                $1 != $2 { printf("=== Fields 1 and 2 not equal (NR=%s).n", NR) }






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 3 hours ago

























                answered 3 hours ago









                KusalanandaKusalananda

                135k17255422




                135k17255422






























                    draft saved

                    draft discarded




















































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


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

                    But avoid



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

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


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




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f506180%2fhow-to-prevent-printing-to-stdout-from-an-awk-script-not-from-cli%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

                    Loup dans la culture

                    How to solve the problem of ntp “Unable to contact time server” from KDE?

                    Connection limited (no internet access)