Difference in AWK when using NR with and withou print

68 views Asked by At

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!

1

There are 1 answers

0
RavinderSingh13 On BEST 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.