Difference in AWK when using NR with and withou print












1















I am a AWK beginner and after playing around with the built-in variable NR, I do not understand the following
Text:



CREDITS,EXPDATE,USER,GROUPS
99,01 jun 2018,sylvain,team:::admin
52,01 dec 2018,sonia,team
52,01 dec 2018,sonia,team


using



awk 'NR' file 
CREDITS,EXPDATE,USER,GROUPS
99,01 jun 2018,sylvain,team:::admin
52,01 dec 2018,sonia,team
52,01 dec 2018,sonia,team

awk '{print NR}' file
1
2
3
4


Thus, I was expecting to the same results when using NR>2&&NR<5. Here is what I got:



awk 'NR>2&&NR<5' file
52,01 dec 2018,sonia,team
52,01 dec 2018,sonia,team

awk '{print NR>2&&NR<5}' file
Nothing shows up


Could you tell me why isn´t the last line showing a count from the numbers to 3 to 4 as it is displayed when using awk '{print NR}' file? It is not possible to mix a range of NR with the command print?



Thanks in advance!










share|improve this question




















  • 1





    Hint: try awk '{print (NR>2&&NR<5)}' file and awk 'NR>2&&NR<5 {print NR}' file.

    – Gordon Davisson
    Jan 19 at 9:27











  • Hello Gordon. The first attempt gave some strange results, a couple of zeros and ones, whereas the second attempt perfectly printed what I was looking for. Could you explain to me why you have to use NR again when using {print}? In the first example I didn´t have to use it in order to simply get my numbers, so why does it have to use in this case?

    – little_mice
    Jan 19 at 9:36











  • You might want to invest in some white space a parentheses when writing code. It makes your code clearer and less error prone. Try writing print ( (NR>2) && (NR<5) ) instead of print NR>2&&NR<5 and notice the difference in both functionality and clarity (what you currently have should really be flagged as a syntax error but it's also undefined behavior so YMMV).

    – Ed Morton
    Jan 19 at 20:55


















1















I am a AWK beginner and after playing around with the built-in variable NR, I do not understand the following
Text:



CREDITS,EXPDATE,USER,GROUPS
99,01 jun 2018,sylvain,team:::admin
52,01 dec 2018,sonia,team
52,01 dec 2018,sonia,team


using



awk 'NR' file 
CREDITS,EXPDATE,USER,GROUPS
99,01 jun 2018,sylvain,team:::admin
52,01 dec 2018,sonia,team
52,01 dec 2018,sonia,team

awk '{print NR}' file
1
2
3
4


Thus, I was expecting to the same results when using NR>2&&NR<5. Here is what I got:



awk 'NR>2&&NR<5' file
52,01 dec 2018,sonia,team
52,01 dec 2018,sonia,team

awk '{print NR>2&&NR<5}' file
Nothing shows up


Could you tell me why isn´t the last line showing a count from the numbers to 3 to 4 as it is displayed when using awk '{print NR}' file? It is not possible to mix a range of NR with the command print?



Thanks in advance!










share|improve this question




















  • 1





    Hint: try awk '{print (NR>2&&NR<5)}' file and awk 'NR>2&&NR<5 {print NR}' file.

    – Gordon Davisson
    Jan 19 at 9:27











  • Hello Gordon. The first attempt gave some strange results, a couple of zeros and ones, whereas the second attempt perfectly printed what I was looking for. Could you explain to me why you have to use NR again when using {print}? In the first example I didn´t have to use it in order to simply get my numbers, so why does it have to use in this case?

    – little_mice
    Jan 19 at 9:36











  • You might want to invest in some white space a parentheses when writing code. It makes your code clearer and less error prone. Try writing print ( (NR>2) && (NR<5) ) instead of print NR>2&&NR<5 and notice the difference in both functionality and clarity (what you currently have should really be flagged as a syntax error but it's also undefined behavior so YMMV).

    – Ed Morton
    Jan 19 at 20:55
















1












1








1


1






I am a AWK beginner and after playing around with the built-in variable NR, I do not understand the following
Text:



CREDITS,EXPDATE,USER,GROUPS
99,01 jun 2018,sylvain,team:::admin
52,01 dec 2018,sonia,team
52,01 dec 2018,sonia,team


using



awk 'NR' file 
CREDITS,EXPDATE,USER,GROUPS
99,01 jun 2018,sylvain,team:::admin
52,01 dec 2018,sonia,team
52,01 dec 2018,sonia,team

awk '{print NR}' file
1
2
3
4


Thus, I was expecting to the same results when using NR>2&&NR<5. Here is what I got:



awk 'NR>2&&NR<5' file
52,01 dec 2018,sonia,team
52,01 dec 2018,sonia,team

awk '{print NR>2&&NR<5}' file
Nothing shows up


Could you tell me why isn´t the last line showing a count from the numbers to 3 to 4 as it is displayed when using awk '{print NR}' file? It is not possible to mix a range of NR with the command print?



Thanks in advance!










share|improve this question
















I am a AWK beginner and after playing around with the built-in variable NR, I do not understand the following
Text:



CREDITS,EXPDATE,USER,GROUPS
99,01 jun 2018,sylvain,team:::admin
52,01 dec 2018,sonia,team
52,01 dec 2018,sonia,team


using



awk 'NR' file 
CREDITS,EXPDATE,USER,GROUPS
99,01 jun 2018,sylvain,team:::admin
52,01 dec 2018,sonia,team
52,01 dec 2018,sonia,team

awk '{print NR}' file
1
2
3
4


Thus, I was expecting to the same results when using NR>2&&NR<5. Here is what I got:



awk 'NR>2&&NR<5' file
52,01 dec 2018,sonia,team
52,01 dec 2018,sonia,team

awk '{print NR>2&&NR<5}' file
Nothing shows up


Could you tell me why isn´t the last line showing a count from the numbers to 3 to 4 as it is displayed when using awk '{print NR}' file? It is not possible to mix a range of NR with the command print?



Thanks in advance!







awk text-processing text-parsing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 19 at 10:27









Inian

39.3k63971




39.3k63971










asked Jan 19 at 9:21









little_micelittle_mice

163




163








  • 1





    Hint: try awk '{print (NR>2&&NR<5)}' file and awk 'NR>2&&NR<5 {print NR}' file.

    – Gordon Davisson
    Jan 19 at 9:27











  • Hello Gordon. The first attempt gave some strange results, a couple of zeros and ones, whereas the second attempt perfectly printed what I was looking for. Could you explain to me why you have to use NR again when using {print}? In the first example I didn´t have to use it in order to simply get my numbers, so why does it have to use in this case?

    – little_mice
    Jan 19 at 9:36











  • You might want to invest in some white space a parentheses when writing code. It makes your code clearer and less error prone. Try writing print ( (NR>2) && (NR<5) ) instead of print NR>2&&NR<5 and notice the difference in both functionality and clarity (what you currently have should really be flagged as a syntax error but it's also undefined behavior so YMMV).

    – Ed Morton
    Jan 19 at 20:55
















  • 1





    Hint: try awk '{print (NR>2&&NR<5)}' file and awk 'NR>2&&NR<5 {print NR}' file.

    – Gordon Davisson
    Jan 19 at 9:27











  • Hello Gordon. The first attempt gave some strange results, a couple of zeros and ones, whereas the second attempt perfectly printed what I was looking for. Could you explain to me why you have to use NR again when using {print}? In the first example I didn´t have to use it in order to simply get my numbers, so why does it have to use in this case?

    – little_mice
    Jan 19 at 9:36











  • You might want to invest in some white space a parentheses when writing code. It makes your code clearer and less error prone. Try writing print ( (NR>2) && (NR<5) ) instead of print NR>2&&NR<5 and notice the difference in both functionality and clarity (what you currently have should really be flagged as a syntax error but it's also undefined behavior so YMMV).

    – Ed Morton
    Jan 19 at 20:55










1




1





Hint: try awk '{print (NR>2&&NR<5)}' file and awk 'NR>2&&NR<5 {print NR}' file.

– Gordon Davisson
Jan 19 at 9:27





Hint: try awk '{print (NR>2&&NR<5)}' file and awk 'NR>2&&NR<5 {print NR}' file.

– Gordon Davisson
Jan 19 at 9:27













Hello Gordon. The first attempt gave some strange results, a couple of zeros and ones, whereas the second attempt perfectly printed what I was looking for. Could you explain to me why you have to use NR again when using {print}? In the first example I didn´t have to use it in order to simply get my numbers, so why does it have to use in this case?

– little_mice
Jan 19 at 9:36





Hello Gordon. The first attempt gave some strange results, a couple of zeros and ones, whereas the second attempt perfectly printed what I was looking for. Could you explain to me why you have to use NR again when using {print}? In the first example I didn´t have to use it in order to simply get my numbers, so why does it have to use in this case?

– little_mice
Jan 19 at 9:36













You might want to invest in some white space a parentheses when writing code. It makes your code clearer and less error prone. Try writing print ( (NR>2) && (NR<5) ) instead of print NR>2&&NR<5 and notice the difference in both functionality and clarity (what you currently have should really be flagged as a syntax error but it's also undefined behavior so YMMV).

– Ed Morton
Jan 19 at 20:55







You might want to invest in some white space a parentheses when writing code. It makes your code clearer and less error prone. Try writing print ( (NR>2) && (NR<5) ) instead of print NR>2&&NR<5 and notice the difference in both functionality and clarity (what you currently have should really be flagged as a syntax error but it's also undefined behavior so YMMV).

– Ed Morton
Jan 19 at 20:55














1 Answer
1






active

oldest

votes


















2














awk 'NR>2&&NR<5' Input_file is where we are checking condition if line number is greater than 2 and greater than 5 then do default action which is printing current line.



In you code awk '{print NR>2&&NR<5}' Input_file, here you are using print and then mentioning condition which is NOT the way awk works.



awk works on method of:



Condition_check/regexp{action}


if NO action is given then by default print of current line will happen, which is happening in your first code.



More analysis:
To prove point {print NR>2&&NR<5} will NOT behave like default method of awk of regexp/condition_check{action} run this:



awk '{print (NR>2&&NR<5)}' Input_file


See the output what it will provide:



0
0
1
1
0
0
0
0
0
0


See line 3rd and 4th which is 1 means condition for that line is TRUE and 0 means condition for that line is FALSE. So by this we could see it prints condition's STATE in print statement if we use condition in (..) like mentioned above.






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%2f54265651%2fdifference-in-awk-when-using-nr-with-and-withou-print%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









    2














    awk 'NR>2&&NR<5' Input_file is where we are checking condition if line number is greater than 2 and greater than 5 then do default action which is printing current line.



    In you code awk '{print NR>2&&NR<5}' Input_file, here you are using print and then mentioning condition which is NOT the way awk works.



    awk works on method of:



    Condition_check/regexp{action}


    if NO action is given then by default print of current line will happen, which is happening in your first code.



    More analysis:
    To prove point {print NR>2&&NR<5} will NOT behave like default method of awk of regexp/condition_check{action} run this:



    awk '{print (NR>2&&NR<5)}' Input_file


    See the output what it will provide:



    0
    0
    1
    1
    0
    0
    0
    0
    0
    0


    See line 3rd and 4th which is 1 means condition for that line is TRUE and 0 means condition for that line is FALSE. So by this we could see it prints condition's STATE in print statement if we use condition in (..) like mentioned above.






    share|improve this answer






























      2














      awk 'NR>2&&NR<5' Input_file is where we are checking condition if line number is greater than 2 and greater than 5 then do default action which is printing current line.



      In you code awk '{print NR>2&&NR<5}' Input_file, here you are using print and then mentioning condition which is NOT the way awk works.



      awk works on method of:



      Condition_check/regexp{action}


      if NO action is given then by default print of current line will happen, which is happening in your first code.



      More analysis:
      To prove point {print NR>2&&NR<5} will NOT behave like default method of awk of regexp/condition_check{action} run this:



      awk '{print (NR>2&&NR<5)}' Input_file


      See the output what it will provide:



      0
      0
      1
      1
      0
      0
      0
      0
      0
      0


      See line 3rd and 4th which is 1 means condition for that line is TRUE and 0 means condition for that line is FALSE. So by this we could see it prints condition's STATE in print statement if we use condition in (..) like mentioned above.






      share|improve this answer




























        2












        2








        2







        awk 'NR>2&&NR<5' Input_file is where we are checking condition if line number is greater than 2 and greater than 5 then do default action which is printing current line.



        In you code awk '{print NR>2&&NR<5}' Input_file, here you are using print and then mentioning condition which is NOT the way awk works.



        awk works on method of:



        Condition_check/regexp{action}


        if NO action is given then by default print of current line will happen, which is happening in your first code.



        More analysis:
        To prove point {print NR>2&&NR<5} will NOT behave like default method of awk of regexp/condition_check{action} run this:



        awk '{print (NR>2&&NR<5)}' Input_file


        See the output what it will provide:



        0
        0
        1
        1
        0
        0
        0
        0
        0
        0


        See line 3rd and 4th which is 1 means condition for that line is TRUE and 0 means condition for that line is FALSE. So by this we could see it prints condition's STATE in print statement if we use condition in (..) like mentioned above.






        share|improve this answer















        awk 'NR>2&&NR<5' Input_file is where we are checking condition if line number is greater than 2 and greater than 5 then do default action which is printing current line.



        In you code awk '{print NR>2&&NR<5}' Input_file, here you are using print and then mentioning condition which is NOT the way awk works.



        awk works on method of:



        Condition_check/regexp{action}


        if NO action is given then by default print of current line will happen, which is happening in your first code.



        More analysis:
        To prove point {print NR>2&&NR<5} will NOT behave like default method of awk of regexp/condition_check{action} run this:



        awk '{print (NR>2&&NR<5)}' Input_file


        See the output what it will provide:



        0
        0
        1
        1
        0
        0
        0
        0
        0
        0


        See line 3rd and 4th which is 1 means condition for that line is TRUE and 0 means condition for that line is FALSE. So by this we could see it prints condition's STATE in print statement if we use condition in (..) like mentioned above.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 19 at 12:59

























        answered Jan 19 at 9:52









        RavinderSingh13RavinderSingh13

        27.2k41438




        27.2k41438






























            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54265651%2fdifference-in-awk-when-using-nr-with-and-withou-print%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

            Liquibase includeAll doesn't find base path

            How to use setInterval in EJS file?

            Petrus Granier-Deferre