jay jay - 6 months ago 24
Bash Question

How to format following bash/sh/shell date


How to format this "May 18, 2016 at 1:05:30 PM EDT" to +%Y-%m-%d
00:00:00

JayOSX:~ jay$ d=$(defaults read /Library/Preferences/com.someapp.some.plist LastUpdated)


echo"$d "

##returns
May 18, 2016 at 1:05:30 PM EDT


I tried:

date -j -f "%b %d, %Y at %l:%M:S %Z" "$(defaults read /Library/Preferences/com.someapp.some.plist LastUpdated)” + "%Y-%m-%d 00:00:00"



And I got the Following Error:

Failed conversion of ``%d,'' using format ``“%b''
date: illegal time format
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

Answer

here you go ,

create the infile.txt :

echo "May 18, 2016 at 1:05:30 PM EDT" > infile

now we have :

[za]$ cat infile.txt 
May 18, 2016 at 1:05:30 PM EDT

One liner that will do it . FYI, "%s-%s-%s %s %s:%s:%s \n", $4 , $1 , $2 , $3 , $6 , $7 , $8 is what does the reformatting.

 [za]$ awk -F'[ "\t" :,]' '{printf "%s-%s-%s %s  %s:%s:%s  \n", $4 , $1 , $2 , $3  , $6 , $7 , $8  }' infile.txt

Outputs :

 2016-May-18   1:05:30 

Quick test .

echo May 18, 2016 at 1:05:30 PM EDT | awk -F'[ "\t" :,]' '{printf "%s-%s-%s %s  %s:%s:%s  \n", $4 , $1 , $2 , $3  , $6 , $7 , $8  }'

I hope this will help ;).