• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1207
  • Last Modified:

Convert Zulu Format in Bash to MM/DD/YYYY

Though I found ID:20928993 which is promising but I need to do a little more than that kb item.

my format of the datastore is 20101022185123Z
where date = Oct 22, 2010 18hrs 51mins 23secs Zulu time
I need to construct a pipe sequence to count characters and rearrange formatting
to where this date will be represented as : MM/DD/YYYY

I know the correct format:: date +'%m/%d/%Y' == MM/DD/YYYY or 10/22/2010
If I could just output that I would but my stdin is 20101022185123Z

How do I convert it ?

I am not sure how to count characters. but once I do its some simple awk sequences I am sure...

logical goal steps:

for timestamp in myfile
        if timestamp!="";
        YYYY = ""
        DD = ""
        MM = ""
     1) count/grep 0-3 L>R stdout characters print $1 assign to YYYY variable
     2) | count/grep 0-1 L>R stdout characters trim print $1 assign to MM variable
     3) | count/grep 0-1 L>R stdout characters trim print $1 assign to DD variable
     ##dump the rest of the string do not need mins nor seconds nor Zulu any more
# I want a blank if there is no timestamp
        YYYY = ""
        DD = ""
        MM = ""
4) echo "$MM/$DD/$YYYY" > myfile.log


thanks all in advance :)

JAaron Anderson
JAaron Anderson
  • 3
2 Solutions
How about this for converting:

date -d "${timestamp:0:8}" +'%m/%d/%Y'


DD=`echo $timestamp | sed -r "s|[0-9]{6}([0-9]{2}).*|\1|"`
MM=`echo $timestamp | sed -r "s|[0-9]{4}([0-9]{2}).*|\1|"`
YYYY=`echo $timestamp | sed -r "s|([0-9]{4}).*|\1|"`

that should do what you want
OK, to make your logic complete:

[[ ! -n $timestamp ]] && echo $(date -d "${timestamp:0:8}" +"%m/%d/%Y") >> myfile.log
.. sorry, typo as always:

[[ -n $timestamp ]] && echo $(date -d "${timestamp:0:8}"  +"%m/%d/%Y") >> myfile.log
JAaron AndersonProgramming Architect @ Widener UniversityAuthor Commented:
freakin awesome you guys rock... I aspire to be a affluent in bash as you both ... thx again woolmilkporc & HeiniHog !

## lol
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now