3

Here Oct 01 has to be printed as Oct 1 i.e without 0. I need two spaces between Oct and 1 i.e like Oct 1.

4

2 Answers 2

9

With GNU date:

date -d "Oct 01 20:00" "+%b %_d %k:%M"

Where:

  • %b: locale's abbreviated month name (e.g., Jan)
  • %_d: day of month (space padded )
  • %k: hour ( 0..23)
  • %M: minute (00..59)

Ouput:

Oct  1 20:00
2
  • 1
    @StéphaneChazelas I know -d is not POSIX, but relatively common. However, that's why I upvoted your answer as the more portable one.
    – chaos
    Oct 6, 2015 at 12:21
  • 3
    @StéphaneChazelas Thanks for POSIXLY_CORRECT I will have a look at this. And instead %_d, %e can be used which is in posix: pubs.opengroup.org/onlinepubs/009604599/utilities/…
    – chaos
    Oct 6, 2015 at 12:41
1

If you want to convert those strings when found in some text, and only when they follow that specific Mmm dd HH:MM pattern, on a GNU or FreeBSD system (and derivatives), you could do:

sed -E "s/($(locale abmon|tr ';' '|')) 0([0-9] [0-2][0-9]:[0-5][0-9])/\1  \2/g"

It takes the list of month name abbreviations from the output of locale abmon so that adapts to the localisation preference of the caller. Use LC_ALL=C locale abmon to force English month name abbreviations.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .