Finding start and end date from given year and month in Unix - date

I have a unix script named myScript where two arguments should be passed in YYYYMM format indicating starting month and ending month for the script.
Inside the script, based on the argument, I need to calculate the start date of the month in first argument and the end date of the month in second argument.
I wrote the below code:
startYr=`expr substr $2 1 4`
startMonth=`expr substr $2 5 2`
startDate=`cal $startMonth $startYr | grep . | fmt -1 | tail -1`
endYr=`expr substr $3 1 4`
endMonth=`expr substr $3 5 2`
endDate=`cal $endMonth $endYr | grep . | fmt -1 | tail -1`
export eomStartDate=$startYr-$startMonth-$startDate
export eomEndDate=$endYr-$endMonth-$endDate
echo "START DATE: $eomStartDate"
echo "END DATE: $eomEndDate"
However, on running above script as:
myScript 201401 201412
The result I am getting is:
START DATE: 2014-12-31
END DATE: --31
What am I missing ? I am using Korn Shell.
Thanks for reading!

The parameters should be $1 and $2, not $2 and $3. Also, for start date you should do sed -n 10p to get the 10th line, from where the days start.

Related

awk appears to be miscalculating dates

I have a log file where each line begins with a date; here is an example line:
26/06/2020 00:00:01 Executing daily job...
I am using the following awk command:
awk -v d="$(date -d "1 month ago" "+%d/%m/%Y")" '$1 $2 > d' log > temp-log
The result is supposed to be log entries in the last month but I only get the last day.
So, putting it all together, we get this solution:
cat logfile | awk -v startdate="$(date -d "1 month ago" "+%Y/%m/%d")" '
{sep="/"; split($1,array,sep); $1=array[3] sep array[2] sep array[1];
if ($1 > startdate) print}'

Change the day from where the week starts in bucket in Splunk

I am running this query in splunk which bucketizes the data on a weekly basis , based on the field "impact_start" and gives me the output. But the problem is that the start of the week in the output is Thursday rather than Monday.
Is there any way i can change the start of the week to Monday instead of Thursday?
search index=* impact=1 OR impact=2 product_line=* | eval time = round( strptime(impact_start,"%Y-%m-%d %H:%M:%S"), 0 )| where time >= 1473328728 AND time<=1476352728| bucket time span=7d | stats values(number) as incident_name by time
You're in luck - If you look at the strftime spec, Monday is considered the start of the week
http://strftime.org/
So you could try this: (untested)
index=* impact=1 OR impact=2 product_line=*
| eval time = round( strptime(impact_start,"%Y-%m-%d %H:%M:%S"), 0 )
| where time >= 1473328728 AND time<=1476352728
| eval week = strftime(impact_start,"%Y %w")
| stats values(number) as incident_name by week

Firefox Bookmarks add_date

One question to firefox exported bookmarks.
How to read this date: ADD_DATE="1375088003"? I think it is date when user add new website to bookmarks but how to read this all string? Where is year, day and month?
It's the Epoch time, ie the number of seconds since Jan 1 1970. To convert it to a human-friendly date, you can pass it through the 'date' unix command:
date -r 1375088003
will return
Mon 29 Jul 2013 18:53:23 AEST
(on my trusty Mac here in eastern Australia, hence the AEST)
Here's what I do. I export bookmarks from my browser. Assume the result file is called "bookmarks.html". I have a one-line executable script that takes a file as input, and creates another file with many long fields stripped. The one-liner I call "icon.sed" contains this:
sed 's/ICON=\"[^\"]*\"//' | sed 's/ICON_URI=\"[^\"]*\"//' | \
sed 's/LAST_CHARSET=\"[^\"]*\"//' | sed 's/HREF=\"[^\"]*\"//'| \
tr -s ' '
I use it like this:
~/icon.sed <bookmarks.html >newbook.txt
I have another script that takes a mm/dd/ccyy parameter, and returns the Epoch time for that date. That executable script is called "epoch.sh":
#!/bin/sh
if [ -z "$1" ]; then echo "You must supply a date"
else date -j -f "%m/%d/%Y" "$1" "+%s"; fi
exit 0
Choose a date you're looking for, and send it through "epoch.sh", such as: ~/epoch.sh 10/30/2015 which outputs this: 1446246082 .
You can then grep your newbook.txt file looking for either ADD_DATE or LAST_MODIFIED date which begins with the first four digits: 1446
grep 'LAST_MODIFIED="1446' newbook.txt
Also, use epoch.sh to get a pair of epoch times for start and stop dates so you can bracket your results by doing multiple greps over the range, using just the first four digits of each epoch time.
To convert a Firefox timestamp to a date with BASIC:
If the timestamp is from an sqlite dump of places.sqlite, you need to truncate it first:
stg0 = Left("1670163289502000", 10) results in "1670163289"
Convert the timestamp:
Dim var0 As double, var1 As Date
var0 = stg0
or
var0 = "1670163289" 'BASIC will do the conversion for you
var1 = (var0 / 86400) + 25569
where 86400 is the # of seconds in a day and 25569 is the BASIC daynumber for 1/1/1970

Validating date in Unix

I have a scenario as below:
I will be getting two dates viz. start_dt and end_dt in yyyyMMdd format as arguments to my Unix Script.
I have to loop through each of the dates starting from start_dt till end_dt incrementing by a day each time.
The script I have written is as below:
start_date=$1
end_date=$2
#verify dates
if ! date -d "$start_date" 2>&1 > /dev/null ;
then echo "start date is invalid" ; exit 1
fi
if ! date -d "$end_date" 2>&1 > /dev/null ;
then echo "end date is invalid" ; exit 1
fi
#set current and end date
curr_dt=$(date -d "$start_date")
end_dt=$(date -d "$end_date +1 hours")
#loop over all dates
while [ "$end_dt" != "$curr_dt" ]
do
echo $curr_dt
# increment the date
curr_dt=$(date -d "$curr_dt +1 hours")
done
However, I am getting below error when I am running with input arguments as 20140128 and 20140130:
date: invalid date `20140130 +1 hours'
Tue Jan 28 00:00:00 EST 2014
date: invalid date `Tue Jan 28 00:00:00 EST 2014 +1 hours'

How to match date mm/dd/yyyy

Given the following input
506028500,1820196768,43,0,84552257, ,1,-71.16,42.42,04/26/2012,
I want to filter the date at the end of the row, so I can change its mm/dd/yyyy format into MySQL date format yyyy-mm-dd:
I am trying to match the date at the end of the row having first removed the trailing ',' (comma).
{
s/,$//g
/\([0-9][0-9]\)\/\([0-9][0-9]\)\/\([0-9][0-9][0-9][0-9]\)$/=
}
I'm getting 12847, but am expecting 04262012.
What am I doing wrong, that I can't even match the date that's there?
Thanks.
This might work for you:
echo "506028500,1820196768,43,0,84552257, ,1,-71.16,42.42,04/26/2012," |
sed 's|,\(..\)/\(..\)/\(....\),$|,\3-\2-\1,|'
506028500,1820196768,43,0,84552257, ,1,-71.16,42.42,2012-26-04,
If you just want the date:
echo "506028500,1820196768,43,0,84552257, ,1,-71.16,42.42,04/26/2012," |
sed 's|.*,\(..\)/\(..\)/\(....\),$|\3-\2-\1|'
2012-26-04
This works for me:
sed "s/\([0-9]\{2\}\)\/\([0-9]\{2\}\)\/\([0-9]\{4\}\)$/\3-\1-\2/g" someFile.txt
At least it replaces the date string with one of the desired format.
To only print the date at the end, the following works for me:
sed "s/.*\([0-9]\{2\}\)\/\([0-9]\{2\}\)\/\([0-9]\{4\}\)$/\3-\1-\2/g" someFile.txt