$ V_Last_Day= cal | awk 'NF != 0 {V_Last = $0 }; END{ print V_Last }' | awk '{print $NF}'
Explanation:
1. cal command returns the calender of the current month as shown below:
November 2015
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Row_Number:1==> November 2015
Row_Number:2==>Su Mo Tu We Th Fr Sa
Row_Number:3==> 1 2 3 4 5 6 7
Row_Number:4==> 8 9 10 11 12 13 14
Row_Number:5==>15 16 17 18 19 20 21
Row_Number:6==>22 23 24 25 26 27 28
Row_Number:7==>29 30
******In End part****
29 30
As we are not storing rows in any variable hence every time when AWK reads a row from cal, its overwrite the V_Last variable hence only row 7 is available in the variable V_Last.
Explanation:
1. cal command returns the calender of the current month as shown below:
November 2015
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
2. Output of cal command is piped to AWK command where it is checking all the rows where Number of Fields are not equals to zero using NR inbuilt function. Please refer the article for more info AWK Inbuilt Variables
3. V_Last holds the last line of the cal command and END part print the same and piped it to another AWK command. See the below output:
$ V_Last_Day= cal | awk 'NF != 0{ V_Last = $0; print "Row_Number:"NR"==>"V_Last; } END{ print "******In End part****\n"; print V_Last }'
$ V_Last_Day= cal | awk 'NF != 0{ V_Last = $0; print "Row_Number:"NR"==>"V_Last; } END{ print "******In End part****\n"; print V_Last }'
Row_Number:1==> November 2015
Row_Number:2==>Su Mo Tu We Th Fr Sa
Row_Number:3==> 1 2 3 4 5 6 7
Row_Number:4==> 8 9 10 11 12 13 14
Row_Number:5==>15 16 17 18 19 20 21
Row_Number:6==>22 23 24 25 26 27 28
Row_Number:7==>29 30
******In End part****
29 30
As we are not storing rows in any variable hence every time when AWK reads a row from cal, its overwrite the V_Last variable hence only row 7 is available in the variable V_Last.
4. Last AWK command print the last field of the row which is nothing but the last day of the month.
No comments:
Post a Comment