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
.
-
5This feels like an XY problem. What are you trying to accomplish?– SobriqueOct 6, 2015 at 12:04
-
1@Sobrique, what is an "XY problem"?– user1717828Oct 6, 2015 at 13:27
-
2It means that perhaps you don't really need to format your date like this, but you think you do.– Dmitry GrigoryevOct 6, 2015 at 13:31
-
meta.stackexchange.com/q/66377/270186– SobriqueOct 6, 2015 at 13:33
Add a comment
|
2 Answers
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
-
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.– chaosOct 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/…– chaosOct 6, 2015 at 12:41
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.