I'm having the hardest time trying to do the most simple thing. I want to print the current date with Year-month-day-hour-minute-seconds to a .csv file
I want the formatting of the .csv file to look like this:
Year Month Day Hour Minute Second
2017 03 08 17 52 23
And i cannot figure out for the life of me what to do. I have tried figuring it out with printf and using "," as the delimiter but the formatting will not work. It does not end up in the columns like i want.
Could someone point me in the right direction?
It's a simple date formatting problem. Try the following :
echo "Year,Month,Day,Hour,Minute,Second" && date +"%Y,%m,%d,%H,%M,%S" > myfile.csv
Here is a link to date formatting syntax.
Related
I understand how to reformat a date using the date command, and I am fine with that. However, I have a wrinkle in that I am struggling with - the date I want to reformat is the output of another command, so I am storing it in the variable. I am struggling with the syntax of how to specify that I want to take the output of one command, and run it through date -d, and store it in another variable. Here is what I tried:
expdate=`get_expire_date.sh`
echo $expdate
Mon 23 Mar 2022 05:05:05 PM UTC
expdval=`date -d'($expdate)'`
echo $expdval
I get today's date, not the converted expire date from the script output. If I leave the parenthesis out, of course, it treats $expdate as the literal text to translate and gives an error, whereas if I leave the single quote marks off, it uses the spaces in the date string as a delimiter and only grabs the first token.
What am I doing wrong?
First, parameter expansion doesn't occur inside single quotes. You would need to change the single quotes
expdval=`date -d'($expdate)'`
to double quotes
expdval=`date -d"($expdate)"`
Second, the parentheses create an invalid input, which results (for reasons I don't really understand) in an output of midnight of the current day. (You'll get the same result with the trivial invalid date date -d "".)
Drop the parentheses, and you'll get the same date back (because the input format matches the default output format).
$ date -d "$expdate"
Wed Mar 23 13:05:05 EDT 2022
To actually manipulate it, you'll need an explicit output format:
$ date -d "$expdate" +%Y-%m-%d
2022-03-23
or some form of date "arithmetic":
$ date -d "$expdate + 2 days"
Fri Mar 25 13:05:05 EDT 2022
I found I had to use double-quotes instead, like this (and sorry for the old way of doing things, updating to new shell syntax):
expdval=$(date -d"$(get_expire_date.sh)")
Is there any way to use standard tools (not programming scripts) to parse the date in custom, odd format?
I've got a start script (bash) that should handle the output of the program, that contains dates with very odd formatting (like Jan, 4, 2021, 1:20:30 PM - which would be a pattern like "MMM, d, yyyy, h:mm:ss a".
It would be possible to extract the tokens with sed or awk, but processing them is a nightmare, especially month shortcuts (they are in the same language like system, but that language can be installation-specific) or hours (need to check AM/PM token and add 12 if it's PM).
'date' apparently doesn't support that, but maybe some shell extension or toolkit package? Or I'm out of luck and I need to parse this crappy format token by token with sed/cut/awk?
This what I've tried was to do touch -d "Date" after removing the commas (so that I can compare dates with [[ file1 -ot file2 ]], but the problem is, that touch has ignored the TIME part, and ls -lh has shown, that the year was set in place of time, and the result of the comparison was therefore invalid.
Convert date with GNU date and bash:
d="Jan, 4, 2021, 1:20:30 PM"
IFS=',:' read mon day year hour min sec a <<<"$d"
date -d "$mon $day $year $hour:$min:$sec $a" '+%Y-%m-%d %H:%M:%S'
Output:
2021-01-04 13:20:30
I am running awk command to extract data from log file to calculate last 15 minutes log using below command and now i am getting bellow error:
awk '$0>=$from' from=$(`date -u +"####<%d-%b-%Y %H:%M:%S o'clock GMT>"-15min`) test.log
Error:
date: 0551-402 Invalid character in date/time specification.
Usage: date [-u] [+"Field Descriptors"]
awk: 0602-562 Field $() is not correct.
The input line number is 1. The file is test.log.
The source line number is 1.
Can anyone see what the problem is?
I believe you mean
awk '$0 >= from' from=$(date -u -d -15min "+####<%d-%b-%Y %H:%M:%S o'clock GMT>") test.log
$from in awk code does not mean the value of from but the fromth field. This does not make any sense if field is not a number.
the -15min in your date call are part of the format specifier, not the date spec.
Either backticks or $(), not both at the same time. Unless you want to run the output of date as a command and use the output of that.
By the way, I suspect that the lexicographical comparison of date strings formatted this way will not yield the result you want. It will not result in later dates being considered greater than earlier ones in all cases -- particularly at the end/beginning of months. 28-Feb is lexicographically greater than 01-Mar.
Addendum: Ah, I figured this seemed familiar. I left a solution to your previous question that does not have this problem before it was closed as a duplicate of something that doesn't really work for this case.
In my shell, when a date is given with YYYYmmddHHMM format, I would like to add an hour to the given date.
My current code is as below:
DATE=201502252310
NEXT_DATE=$(date +%Y%m%d%H%M -ud "$DATE +1 hour")
echo $NEXT_DATE
When the given date is 201502121010, the result is 2015021210100100.
In fact, I'd like to get 201502121110.
When the given date is 201502122310, the result I'd like to get is 201502130010.
But, it shows date: invalid date '201502122310 +1 hour'.
It seems my code is something wrong.
How can I do to add the specific hour to the given date correctly???
Is there a way to add date in the name of the file... we can add current date in this manner date '+%Y%m%d' but i want to add "filename_date_1-2-2011_thru_31-2-2011.txt" Is it possible to do that??????????
If you have a sufficiently advanced version of the date command and you know a Unix timestamp for the start and end dates, then you can use:
(MacOS X) date -r 1234567890 "+%d-%m-%Y" to obtain 13-02-2009.
(GNU) date -d 2/13/2009 "+%d-%m-%Y" to obtain 13-02-2009 again.
If you don't want the leading zeroes on the day of month, then you need to use '%e` instead of '%d' on Linux (but that puts a space in place of the zero). It is not clear that there's a format specifier for day-of-month without a leading zero on MacOS X; nor is it clear that there's a way to format month of year as a single-digit number for January to September on either platform.
You get the format into your C shell script using back-ticks around the date commands.
Consider reading Csh Programming Considered Harmful and heeding its advice.