0

I need to convert the date of the macbook via CLI because I'm running a script:

Date we have to convert 2019-09-17 15:32:27

Final format Jun 17 15:32:27

I tried it with the following script:

date_raw=`grep "^.*,.*,.*,.*,.*,.*,"$i"," ${SNIFFER_BUFFER_USAGE} | head -1 | awk -F ',' '{ print $2}'`
if [ $OSX -eq 1 ];
then
  dataHoraMinuto=$(date -j -f "%Y-%m-%d %H:%M:%S" "$date_raw" +"%b %d %H:%M:%S")
else
  dataHoraMinuto=`date +"%b %d %H:%M:%S" -d "$date_raw"`
fi
cello
  • 5,356
  • 3
  • 23
  • 28
  • Does this answer your question? [Convert date formats in bash](https://stackoverflow.com/questions/6508819/convert-date-formats-in-bash) – Tomáš Pospíšek Mar 18 '20 at 16:09
  • You need to _format_ the date, not “convert” it. How you do that depends upon what programming language you are using. – matt Mar 18 '20 at 16:09
  • @matt Okay, so a using bash, could you please help me a format this date? Thanks man! – Romulo Soares Mar 18 '20 at 16:15
  • @TomášPospíšek Actually it doesn't help, at least what I tried on my side because it brings all the timestamp and I just need a part! Can you help me? – Romulo Soares Mar 18 '20 at 16:19
  • Please show what you tried (exact commands, input you provided, output you got), that makes it easier to help. – cello Mar 18 '20 at 16:24
  • So `man date` and read about the format string. – matt Mar 18 '20 at 16:28
  • @cello Yes man, no problem! I tried to format but it was strange, so follow the link: https://pastebin.com/raw/apYyANPS Thanks – Romulo Soares Mar 18 '20 at 16:30
  • your command seems to work, at least when I use `date_raw="2019-09-17 15:32:27"` in the script instead of the grep expression. Did you verify that the grep returns the expected part? e.g. add a `echo $date_raw` to your script and check the output. – cello Mar 18 '20 at 16:50

0 Answers0