Is there a simple way to convert Common Log Format (NCSA) to timestamp ?
I found only C++ decision and some perl decisions. And wonder why i can't use standart unix function like date.
> date +"%d/%m/%Y:%H:%M:%S" -d "17/Oct/2013:16:52:28" +"%s"
"17/Oct/2013:16:52:28" -> 1382014348
For example:
Date in iso8601 to timestamp
> date -d "2013-10-17T18:07:39+04:00" +"%S"
1382018859
You can try this ( if you surely want to use date) ,
date -d "$(sed -e 's#/#-#g; s#:# #' <<< '17/Oct/2013:16:52:28')" '+%s'
Related
This is working
date -d '2019-12-13 -5 day' +%d%m%Y
However, the following is throwing an error to me
date -d "13122019 -5 day" +%d%m%Y
From man date
The --date=STRING is a mostly free format human readable date string such as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29 16:21:42" or even "next Thursday". [...] The date string format is more complex than is easily documented here but is fully described in the info documentation.
tl;dr: Seems like you cannot specify the format of the input. Use one of the known formats, for instance
date -d '2019-12-13 -5 day' +%d%m%Y
To automatically convert a date from DDMMYYYY format to YYYY-MM-DD you can use sed ...
date -d "$(sed -E 's/(..)(..)(.*)/\3-\2-\1/' <<< 13122019) -5 day" +%d%m%Y
... or bash ...
d=13122019
date -d "${d:4}-${d:2:2}-${d:0:2} -5 day" +%d%m%Y
How would you go about doing a few lines of code in Bash to accomplish the following. I'm trying to build up my skills in Bash and learn how to handle more small tasks directory from the command line.
Steps:
Specify a start date and an end date. Load all the dates in between including the start and end date into a "list"
Loop over the list creating a file like this each time.
(requires date formatting)
2017-11-10.w
2017-11-11.w
2017-11-12.w
You could convert the input dates to Unix timestamps, then add the number of seconds per day and touch a file named after the result until you are past the end date:
#!/bin/bash
startstamp=$(date -d "$1" +'%s')
endstamp=$(date -d "$2" +'%s')
secs_per_day=$(( 24 * 3600 ))
for (( thedate = startstamp; thedate <= endstamp; thedate += secs_per_day )); do
touch "$(date -d "#$thedate" '+%F.w')"
done
The %s formatting string (a GNU extension) prints the number of seconds since the Unix epoch, and # in the argument to the -d option indicates that the date is in that format. %F is short for %Y-%m-%d, which translates to YYYY-MM-DD.
Example usage:
$ ./dates 2017-11-10 2017-11-15
$ ls -1
2017-11-10.w
2017-11-11.w
2017-11-12.w
2017-11-13.w
2017-11-14.w
2017-11-15.w
dates
I am completely new to shell scripting.
I need to change the format of given date to customized format like i have date in a variable with format as MM/DD/YY HH:MM:SS but i want date in a format as MM/DD/YYY HH:MM:SS which is having four digits of the year.
We can change the sys date format but i need the same in a variable.
My code as below
START_DATE="12/20/14 05:59:01"
yr=`echo $START_DATE | cut -d ' ' -f1 | cut -d '/' -f3`
yr_len=`echo $yr | wc -c`
if [ $yr_len -lt 4 ]
then
tmp_yr="20${yr}";
else
1=1;
fi
ln=`echo $tmp_yr|wc -c`
After this i strucked in reframe the same date in wanted format.
Can some one please help me
Regards,
Sai.
Using GNU date:
date -d'02/16/15 09:16:04' "+%m/%d/%Y %T"
produces
02/16/2015 09:16:04
which is what you want. See man date for details about the formatting, or this question for a number of great examples.
One option may be using the date/time functions inside awk. Here is a oneliner:
echo '02/16/15 09:16:04' | sed 's\[/:]\ \g' | awk '{d0=$3+2000FS$1FS$2FS$4FS$5FS$6; d1=mktime(d0);print strftime("%m/%d/%Y %T", d1) }'
output is:
02/16/2015 09:16:04
You can find more strftime formats in https://www.gnu.org/software/gawk/manual/html_node/Time-Functions.html
I'm trying to make some calculations with date in bash script but can't find out the right syntax. I get a string from a file that I convert to a date. Then I want to get the date for one and two days ahead. Looking on another StackOverflow posts it looked easy adding days to today date. This is what I am doing now:
# Extract date string from file
ctldate=`awk 'NR==8 { print $4 }' a-AC-2015-02-10-120000-g3.ctl`
echo $ctldate
12:00Z10feb2015
# Convert string to date
ctldate2=`date +'%d/%m/%Y' -d $ctldate`
echo $ctldate2
10/02/2015
# Try to add a day, should be 11/02
data1=$(date +'%d/%m/%Y' -d "$ctldate" --date='1 day')
echo $data1
12/02/2015
# Also tried
data1=$(date +'%d/%m/%Y' -d "$ctldate2" --date='1 day')
echo $data1
12/02/2015
# And
data1=`date +'%d/%m/%Y' -d $ctldate --date='1 day'`
echo $data1
12/02/2015
It seems that I'm not properly passing $ctldate var to command and that the base date for calculation is today.
Thanks in advance
When you pass several -d or --date, date uses the last one. So when you do:
date -d "$ctldate" --date='1 day'
date will take into account --date='1 day' as it's the last -d/--date argument, and will happily show you tomorrow's date.
Instead you should use:
date -d "$ctldate +1 day"
I have a date in format 'YYYYMMDDHHMMSS' and I need to convert it to Unix timestamp.
I tried to date -d '20140826225834' but I get 'invalid date' error. I asume that I would have to convert what I have ( 20140826225834 ) to accepted date and then convert it to timestamp?
Edit: I have sed this date from 2014-08-21_23.03.07 - maybe it would be easier to convert this date type
You should probably change the format of the date you get, so that date can handle it. I change it to a YYYY/MM/DD HH:MM:SS format with sed.
$ date -d"$(sed -r 's#(.{4})(.{2})(.{2})(.{2})(.{2})#\1/\2/\3 \4:\5:#' <<< "20140826225834")" "+%s"
1409086714
By pieces:
$ sed -r 's#(.{4})(.{2})(.{2})(.{2})(.{2})#\1/\2/\3 \4:\5:#' <<< "20140826225834"
2014/08/26 22:58:34
$ date -d"2014/08/26 22:58:34"
Tue Aug 26 22:58:34 CEST 2014
$ date -d"2014/08/26 22:58:34" "+%s"
1409086714
You could use PHP, since PHP's strtotime() can parse your input format:
#!/bin/bash
input="20140826225834"
output=$(php -r 'echo strtotime("'"$input"'");')
echo "$output" # 1409086714