0

I want to print dates with their respective days of a particular years month.

Example entered year is 2018 and month is Feb. so output will show

Date.     Day  
1.        thursday
2.        Saturday
.
.
.
.
28.       Wednesday       
  • yes I tried but it shows only this months all details. – Kharabela Bhat Feb 22 '18 at 13:51
2

You're describing a good exercise for a beginner in linux so I won't spoon-feed you your homework but I will answer to help you do it.

Start by learning how to use the date command, which is external to awk. The date command has a feature that can output the day of the week in numeric format, so if you type on the command line date +%u, you will get today's day of the week, starting from Monday. For the day of the week of another day, use the d option, for instance date -d2018-02-22 +%u. There is also format code +%A for your locale's name of the day of the week, eg 'Thursday'. See the man page for details and many other goodies of the date command.

You then have a choice. You could iterate over the date command, printing the result for each day of the month, or you could iterate natively in whatever scripting or programming language you feel comfortable with. For instance, you could use the modulus mathematical operator % to loop over seven days of the week n%7 and use a form of case statement for your print operation, or you could define a seven element array of names of days of the week. As an example, here is how to define and use an array in bash:

 dow=( Sun Mon Tues Wed Thu Fri Sat ) ; printf "%s\n" ${dow[4]}

What's left is to tell your loop when to stop, ie. how many days are in the month you are processing. That's probably easiest with an array of twelve elements, each containing the number of days in that month. You would need an external test to check for the 29-day February of a leap year.

As for using awk, you could use it, but it is unlikely to be the best tool for the job because it is designed as a stream editor, and you aren't processing a stream. However, if your assignment requires awk, then that is what you will be stuck with.

  • Thanx a lot :) . I am trying it now. I will paste the code as soon as I do it. – Kharabela Bhat Feb 23 '18 at 5:36
  • @KharabelaBhat: Why? This is a question and answer site, not a "snippet wiki". Once the question has been answered, you should check-mark it answered and move on. – user1404316 Feb 23 '18 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.