How do I get the last day of the current month in Unix?

How do I get the last day of the current month in Unix?

y=$(date ‘+%Y’) # Get current year m=$(date ‘+%m’) # Get current month ((m–)) # Decrement month [[ ${m} == 0 ]] && ((y–)) # If month zero, decrement year [[ ${m} == 0 ]] && m=12 # If month zero, reset to 12 cal ${m} ${y} | awk ‘NF{A=$NF}END{print A}’ # Run cal, and print the last field of the last line with fields.

How do I get the last day of the month in bash?

Start with the current date ( date ) -> 2017-03-06. Set that date to the 1st day of its month ( -v1d ) -> 2017-03-01. Subtract one day from that ( -v-1d) -> 2017-02-28. Format the date ( +%d%b%Y ) -> 28Feb2017.

How do I get the current date in awk?

There are no built-in functions in standard awk to get a date, but the date can easily be assigned to a variable. gawk , does have built-in time functions , and strftime can be used.

How do I get the first day of the current month in Linux?

GET FIRST AND LAST DAY OF A MONTH IN BASH SHELL

  1. GET FIRST AND LAST DAY OF A MONTH IN BASH SHELL. Linux system comes with a date commend.
  2. First Day, current month: # date -d “-0 month -$(($(date +%d)-1)) days”
  3. First Day, last month:
  4. Last Day, current month:
  5. Last Day, last month:
  6. Last Day, month before last month:

Can You redirect output from the date command to AWK?

This second form will take the output from the command $ (date) and redirect it to awk via STDIN. But using awk here is not needed at all. The date command has its own formatting abilities. You should pipe the output of date to awk:

How to print last field in AWK Stack Overflow?

Hence, defining the field separator to / you can say: as NF refers to the number of fields of the current record, printing $NF means printing the last one. Another option is to use bash parameter substitution. It should be a comment to the basename answer but I haven’t enough point.

How to use a simpler method in AWK?

Or if there is another simpler method in awk it will do job as well. EDIT Some very dirty workaround.. Anyone has simpler/cleaner idea? Just use a format string for the outer invocation of date: I’m using the calendar function to load the “month” variable with all of the days of the month and then picking off the last two characters.

How to get the last day of the last month?

I have the requirement to get the last date of the last month. For today the last months last day is 30 (April). note that, the timestamp was set to 00:00:00.