Removing leading zeros from date output

45,327

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.

Share:
45,327

Related videos on Youtube

easl
Author by

easl

Updated on September 18, 2022

Comments

  • easl
    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 and awk.

    The OS I am running is Ubuntu 12.04.2 LTS.

    • user
      user almost 11 years
      There are no trailing zeroes in your example (well, unless you count the 0 in the 10 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
      easl almost 11 years
      yes, the title was wrong, though the post was correct, sorry for confusion.
    • user
      user almost 11 years
      No 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
      mleonard almost 10 years
      Just a remark: Be aware that removing leading zeros makes it harder to sort by those dates, if you ever need to.
    • codeforester
      codeforester about 6 years
  • user13107
    user13107 about 6 years
    Correct syntax for formatting was found in stackoverflow.com/questions/29659069/…
  • Scott - Слава Україні
    Scott - Слава Україні over 3 years
    At two seconds past noon, date will say 2013.06.14.12.00.02, and sed will output 2013.6.14.12..2.  Note that the 00 minutes field disappears completely, leaving two adjacent dots.  This is probably not what the user wants.  To do this correctly in GNU sed, use sed -E -e 's/^0?//' -e 's/\.0?/./g' (to delete zero or one 0).  Or, to do it portably in GNU or POSIX sed, use sed -e 's/^0//' -e 's/\.0/./g' (to delete one 0, or not; it’s OK if the substitution simply fails when there isn’t a leading 0).  … (Cont’d)
  • Scott - Слава Україні
    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 - Слава Україні
    Scott - Слава Україні over 3 years
    What 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.