Removing leading zeros from date output
Solution 1
As per the GNU date
manpage:
By default, date pads numeric fields with zeroes. The following
optional flags may follow '%':
- (hyphen) do not pad the field
Therefore you can do
alias date="date '+%Y.%-m.%-d.%-H.%-M.%-S'"
and receive
2013.6.14.3.19.31
Solution 2
Feels silly, but since this question is tagged with /sed
, here is a way to do this with sed, as you had mentioned :)
alias date='date +"%Y.%m.%d.%H.%M.%S" | sed "s/^0*//g; s/\.0*/./g"'
Notice the comments below. They have very valid points about removing the *
, and removing the g
option in the first sed expression. I'd rather not modify what I have written above without testing it, and I don't have time right now. Scott's comments below put common sense into this answer.
Related videos on Youtube
easl
Updated on September 18, 2022Comments
-
easl over 1 year
I made an alias of the date command to display date in the following format:
2013.06.14.12.10.02
using this command:
alias date = date +"%Y.%m.%d.%H.%M.%S"
Everything works great, except I want to remove the leading zeroes from the output.
There is no way to make it happen by changing the format. I think it can be done only by piping the output to other commands like
sed
andawk
.The OS I am running is Ubuntu 12.04.2 LTS.
-
user almost 11 yearsThere are no trailing zeroes in your example (well, unless you count the
0
in the10
minutes past the hour, but removing that changes meaning of the timestamp). I take it you mean leading zeroes (zeroes at the beginning of each date component), in which case if you are using a GNU userland @j883376's answer will likely be useful. Otherwise, please specify your environment (userland tools are not the same in all Unix-like OSes, and not even all tools might be available on all such OSes). -
easl almost 11 yearsyes, the title was wrong, though the post was correct, sorry for confusion.
-
user almost 11 yearsNo worries. As a general rule, though, it is always good to specify your environment. OS X is different from OpenBSD is different from AIX is different from GNU. By specifying your environment, you don't risk getting answers which won't be of any use to you (like, say, answers suggesting using Linux's /proc when you are trying to solve a problem on OS X).
-
mleonard almost 10 yearsJust a remark: Be aware that removing leading zeros makes it harder to sort by those dates, if you ever need to.
-
codeforester about 6 yearsSee this post: stackoverflow.com/questions/47880412/…
-
-
user13107 about 6 yearsCorrect syntax for formatting was found in stackoverflow.com/questions/29659069/…
-
Scott - Слава Україні over 3 yearsAt two seconds past noon,
date
will say2013.06.14.12.00.02
, andsed
will output2013.6.14.12..2
. Note that the00
minutes field disappears completely, leaving two adjacent dots. This is probably not what the user wants. To do this correctly in GNUsed
, usesed -E -e 's/^0?//' -e 's/\.0?/./g'
(to delete zero or one0
). Or, to do it portably in GNU or POSIXsed
, usesed -e 's/^0//' -e 's/\.0/./g'
(to delete one0
, or not; it’s OK if the substitution simply fails when there isn’t a leading0
). … (Cont’d) -
Scott - Слава Україні over 3 years(Cont’d) … Note that a
g
option is useless on a substitute that’s anchored with^
or$
, since it cannot happen more than once. -
Scott - Слава Україні over 3 yearsWhat are you saying? “You can do lots of formatting with
printf
; go look up the details yourself.”? This does not produce the YMDHMS format requested by the question, and, after stripping the leading zero from the hour, it puts it back! So this is not an answer to the question.