0

I have one shell script which take the arguments in this format:

script_name.sh "6/10/2020 11:46" "6/10/2020 14:40"

which gives me the result as :

Start Date : 6/4/2020 11:46
Custom_Start_date: Jun 04 11
End Date : 6/4/2020 14:40
Custom_End_date: Jun 04 14
Total_Hours : 2

I need to fetch the pattern from a file and would like to increment "Custom Start date" by 1 hour till Total hours:

while Total_Hours
do
grep some_pattern file.txt | grep $Custom_Start_date
done

so first $Custom_Start_date should grep for Jun 04 11 time and then in next iteration Jun 04 12 and then increment should be till "Total_Hours"

5
  • 2
    What looks at first to be a very simple ask becomes increasingly complex the more you think it out. "just increment the hour" is fine, but then you're incrementing 23:41 to 24:41. Okay, so just increment the date. But now, whoops, you're incrementing 5/31/2020 to 5/32/202. Okay, so increment the month. And now 12/31/2020 becomes 13/01/2020. And then there are leap years to account for. You probably want to convert the time to Epoch time, add 3600 seconds, and convert back. You can do this with date.
    – DopeGhoti
    Jun 10, 2020 at 16:28
  • no I just want to increment the hour from Jun 04 11 to Jun 04 12
    – Jeetu
    Jun 10, 2020 at 16:35
  • 1
    You really only want to change "Jun 04 11" to "Jun 04 12" and nothing else?
    – Jeff Schaller
    Jun 10, 2020 at 16:36
  • yes only want to change "Jun 04 11" to "Jun 04 12, "Jun 04 13" like this till the total hours. if total hour is 3, then 11 should change to 12, 13 14. like this actually
    – Jeetu
    Jun 10, 2020 at 16:42
  • 1
    No. I have read that comment 5 times. I see that you say Yes, but then explain No. However I have no idea what you are asking. Note time is hard (see @DopeGhoti's comment, and also consider daylight fudging time: sometimes 11+24=10). Jun 10, 2020 at 21:31

1 Answer 1

0

With date (GNU) version 8.32

mydate=$(date -d "Jun 04 11")

date -d "$mydate +1 hour"   
Thu 04 Jun 2020 12:00:00 PM CEST

date -d "$mydate +1 hour" "+%b %d %H"
Jun 04 12
  • %b short form of month
  • %d day of month
  • %H hour in 24-hour clock format
0

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.