Code for linux date YYY-M-D - linux

In the linux date command, I use this code:
TODAY=$(date +"%Y-%m-%d")
sample output YYYY-MM-DD, such as 2016-09-05.
If I want output date YYYY-M-D, such as 2016-9-5, what should I do with this code?

Do it:
TODAY=$(date +"%Y-%-m-%-d")
Explaining, the - will remove the leading zeros...
Example:
September month...
%m returns 09
%-m returns 9

I am not user you can do that date, but you fix it afterwards:
TODAY=$(date +"%Y-%m-%d")
TODAY="${TODAY//-0/-}"

Related

change format from %Y%m%d%H%M% to another format

how can I change date format from "%Y%m%d%H%M" to "%Y-%m-%d" "%H:%M" in shell scripting?
there is a log file (logfile.txt) with date format as "%Y%m%d%H%M%S" in first column. what is needed is print first column like "%Y-%m-%d" "%H:%M".
e.g: from 201804041323 to 2018-04-04 13:23
Thanks in advance :)
perl -i.bak -pe 's/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})/$1-$2-$3 $4:$5/' file
sed -r 's/^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/\1-\2-\3 \4:\5/g' logfile.txt > newlogfile.txt
You can use below command for getting output in format of "2018-04-18 22:14"
date +%Y-%m-%d" "%H:%M

BASH/Linux - Finding the next date from a variable

In my Unix shell programming, I am trying to get the next date (tomorrow's date) over a reference date defined as "a". Here is the code:
a=2016-01-02
Which operator would I use in my code so that Unix will automatically define a as tomorrow's date as in below
a=2016-01-03
date has a -d option that is very useful in this situation.
To get the next day, add a space after the date then add 1 day
date +%Y-%m-%d -d "$a 1 day"
It's important to add the format specifier because without it, you would get the following output
=>"Sun Jan 3 00:00:00 UTC 2016"
To update the a variable, you could do something like this
a=$(date +%Y-%m-%d -d "$a 1 day")
Remember to wrap the command inside of parentheses with a $ sign in front of it.

Replace Text with Formatted Text on Linux

I'm looking for a way to replace the text between positions 10 and 17 in a file with a Linux command or script. For example, I'd like to replace the date text 20140101 with 01/01/2014.
I'm hoping this is something I can in a single command from the command line with maybe sed or awk?
Using sed, you can capture the first 9 chars in a capture group that would be placed as is. The remaining would be broken in 3 capture groups and re-arranged as you desire.
Something like:
sed -r 's#(.{9})(.{4})(.{2})(.{2})#\1\3/\4/\2#' file
If you are on a system that does not have GNU sed escape ( ) { } with \.
If all your strings are dates then the best is to use date command:
$ date -d 20140101 +%m/%d/%Y
01/01/2014
$ date -d 20140923 +"%m-%d-%Y %a %b"
09-23-2014 Tue Sep
It is especially great tool to translate number of seconds from 1970 (unix epoch) used in many log files:
$ date --date='#1411199063' +"%m-%d-%Y %H:%M:%S"
09-20-2014 07:44:23

give the input format when converting a date

I read the date from a file to a variable. The date has the format ddmmyyyy. It has to be converted to yyyy-mm-dd
I already searched this forum and got this far :
date -d '$DATE' +%F
The problem is the input format is not recognised. Is there any way I can specify the input date format?
On an other forum I found : date -d "${OLD_DATE}" -D "%d%m%Y" +%F
where -D should specify the input format but this doesn't work. But -D is unknown.
thanks for the help and sorry for my English.
You could to it like this:
echo "DDMMYYYY" | awk 'BEGIN {OFS="-"} {print substr($1,5,4), substr($1,3,2), substr($1,1,2)}'
Output:
YYYY-MM-DD
Yes, date understands a lot of formats for -d, but when it's just 8 digits in a row, it interprets it as YYYYmmdd. I'm not sure if you can force it to read it differently, but in this case you can use a simple editor such as awk or sed instead:
$ OLD_DATE='08032011'
$ echo $OLD_DATE | sed -r 's/(.{2})(.{2})(.{4})/\3-\2-\1/'
2011-03-08
This will work on GNU sed. Note that it doesn't check the input (for brevity).

bash - extract day of week of variable

What is the syntax to extract the day of the week from a stored date variable?
The dateinfile format is always [alphanum]_YYYYMMDD.
In this pseudocode example, trying to get dayofweek to store Saturday:
#! /bin/bash
dateinfile="P_20090530"
dayofweek="$dateinfile -u +%A"
[me#home]$ date --date=${dateinfile#?_} "+%A"
Saturday
Or, to put it as you've requested:
[me#home]$ dayofweek=$(date --date=${dateinfile#?_} "+%A")
[me#home]$ echo $dayofweek
Saturday
date -d $(echo $dateinfile | cut -f2 -d_) -u +%A
The inner expression separates the 20090530 from P_20090530, and the outer one extracts the day of week from that date
I needed the same output recently and googled upon this, but I had the same problem stating Bad Substition error.
I then read the date manual and made my version up as follows:
#! /bin/sh
dateinfile="P_20090530"
dayofweek=`date --reference $dateinfile +%A`
echo $dayofweek

Resources