Display Previous Months in Shell - macos

I have extracted the current month using
currentMonth=`date +"%b"`
I want to get all previous five months in a file. Suppose its October now, i want Sep, Aug, Jul, Jun, May in a file. I tried using multiple utilities like cal, ncal, date but could not achieve this.
I want the months in the file as
Sep
Aug
Jul
Jun
May
Any help or suggestion is greatly appreciated.

Simple bash/shell solution (for Unix):
for i in 1 2 3 4 5; do date -d" - $i month" +%b; done
The output:
Sep
Aug
Jul
Jun
May
For MacOS:
for i in {1..5}; do date -v-"$i"m +%b; done

Related

How to perform date operations in bash

Basically, I want to take a time, and day of the week, in UTC+8, and adjust the datetime object to a given UTC offset, within bash, I don't have any code to show because I'm not sure how to start attempting this in the first place honestly
(I'm writing a custom script for a friend who lives in UTC+8 and want to make the input as easy as possible for them, basically they just give it a time in their timezone, and a day of the week, and it'll tell them what date and time that'll be in a different timezone, for an overarching purpose)
For a reference, look at the section 1 of the manual page for "date":
In your shell, just type: man 1 date
or see the online man page:
https://man7.org/linux/man-pages/man1/date.1.html
One way is to parse the date into the number of seconds since epoch (since 1970), and then convert that number of seconds into the format you want:
For example:
$ date +%s --date='2022-12-27 11:30:17 +008'
1672140137
$ date +%c --date='#1672140137'
Tue 27 Dec 2022 06:22:17 AM EST
or you could also convert to ISO format then back to local time
$ date -Iseconds --date='TZ="GMT" 2022-12-22 11:33:44 +08'
2022-12-21T22:33:44-05:00
$ date --date='2022-12-21T22:33:44-05:00'
Wed 21 Dec 2022 10:33:44 PM EST
I hope this helps you get started with some ideas for converting to/from different timezones.
Also, to help with user input, you can show the current month calendar using cal
$ cal
December 2022
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 31
$ date --date='Fri 08:30'
Fri 30 Dec 2022 08:30:00 AM EST
In the above example, I specified "Fri 08:30" which gets set to the next Friday at 08:30 in the morning for my local timezone.

Automating a roster with bash

I have to create a cleaning roster for an appartment building and would like to automate it with GNU bash if possible.
Requirements:
The tenants have to clean the corridor on their floor every week.
The cycle starts on Feb. 11, 2019 and lasts for 30 weeks (10x3).
There are 4 floors to my building.
There are 10 tenants capable of doing the task per floor.
The names of the tenants are in the 3rd column of the file tenants.csv, (sep = |).
The 1st column contains the appartment number which if it starts with a 2, such as in 214 means they are located on Floor number 2.
I would like to generate the dates automatically (maybe from the Date command with the week number %V which is starting on mondays) and merge in the names of the tenants from the csv file. Use of the date command and %V is way more complicated than I am used to. I don't know how to tackle this.
Desired Output (sample taken from the 2018 roster):
Week of Floor 1 Floor 2 Floor 3 Floor 4
Sep 18, Nov 27, Feb 5 Ms.X Mr.Y Ms.XX Mr.YY
Sep 25, Dec 4, Feb 19 Ms.AA Ms.BB Mr.CC Mrs.DD
...
So far, I have only this as the displaying (which i can handle i think) depends how i get the date command to give me the proper dates:
roster_start=$(date -d "20190211") # 11 fev 2019 start of cleaning roster
yr=2019; wk=6
date -d "Feb 6 $yr" +%V
date -d "20190211"
printf "\nWeek of\tFloor 1\t\tFloor 2\t\tFloor 3\t\tFloor 4\n"; \
for wk in 6 16 26 "$yr"; do
printf "%s\t" "$d"
date -d "$wk" +"%b %e"
done
Thank you for any help you can provide.

12 hrs to 24 hrs converter shell script

I want the date format Thu Nov 3 17:21:08 2016 to be converted to Thursday, 3 November 2016 7:48:24 PM AEDT .
Actually, i want to achieve in converting the 12hrs time to 24hrs format using a shell script .
Thanks
I believe below command would help you to go ahead:
date +"%T"

How do i print the date and time of specific lines that contain a certain key word using Ruby?

In other words i need Ruby to read from the attached log file and report the dates and time that has the keyword MAY_DAY. I am able to print out all the information but I don't have the slightest idea on how to print out the specific entries.
I am an uber noob and find ruby extremely difficult to understand. I appreciate all help and respectful criticism. Thanks
test.txt
Oct 15 12:54:01 WHERE IS THE LOVIN MAY_DAY
Oct 16 23:15:44 WHAT THE HECK CAN I DO ABOUT IT HUMP_DAY
Oct 16 14:16:09 I LOVE MY BABY GIRL MAY_DAY
Oct 16 08:25:18 CAN WAIT UNTIL MY BABY RECOVERS CRYSTAL_WIFE
Oct 18 17:48:38 I HOPE HE STOP MESSING WITH THESE FOOLISH CHILDREN TONY_SMITH
Oct 19 05:17:58 GAME TIME GO HEAD AND GET ME MAY_DAY
Oct 20 10:23:33 GAMESTOP IS WHERE ITS AT GAME_DAY
Oct 21 03:54:27 WHAT IS GOING ON WITH MY LUNCH HUNGRY_MAN
RestartMonitor.rb
class RestartMonitor
counter = 1
begin
file = File.new("test.txt", "r")
while (line = file.gets)
puts "#{counter}: #{line}"
counter = counter + 1
end
end
When i run the file i get the following results:
Oct 15 12:54:01 WHERE IS THE LOVIN MAY_DAY
Oct 16 23:15:44 WHAT THE HECK CAN I DO ABOUT IT HUMP_DAY
Oct 16 14:16:09 I LOVE MY BABY GIRL MAY_DAY
Oct 16 08:25:18 CAN WAIT UNTIL MY BABY RECOVERS CRYSTAL_WIFE
Oct 18 17:48:38 I HOPE HE STOP MESSING WITH THESE FOOLISH CHILDREN TONY_SMITH
Oct 19 05:17:58 GAME TIME GO HEAD AND GET ME MAY_DAY
Oct 20 10:23:33 GAMESTOP IS WHERE ITS AT GAME_DAY
Oct 21 03:54:27 WHAT IS GOING ON WITH MY LUNCH HUNGRY_MAN
When i run the code i would like it to only display the date and times that have the keyworld MAY_DAY. so the output should be:
Oct 15 12:54:01
Oct 16 14:16:09
Oct 19 05:17:58
One way to do it would be like so (within a block that's iterating over the lines in the file, obviously):
if line.include?('MAY_DAY')
puts line[0..14]
end
Since the date information (which is what you want output) appears in the same position and is the same length in every line, we don't bother doing any parsing of the text for the output - just spit out the first 15 characters.
I'm tempted to try to compress all of this into a single regular expression, but this ought to work. Obviously, you could do something other than print out the date within the conditional, and if you wanted to work with it as a date, you could pass it to DateTime.parse() (just remember to require 'date' first).

bash date last with week number

To get the date of the last sunday one could use date --date="last sun" for Linux and date -j -v-sun for BSD.
How is it done however if I do have week numbers (1 to 7) instead of strings? I could always use a case-statement to convert the numbers to strings, but I bet there is a better solution for this?!
Thanks for your help.
Use an array:
number=1
days=(zero sun mon tue wed thu fri sat)
date --date="last ${days[number]}"

Resources