0

I have a file with date and time on a couple thousand lines.

I need to convert:

2019/08/02-01:23:50+0000

To:

02/Aug/2019-01:23:50 +0000   

I so far have

th=`grep -o '\[.*\]' test.txt | sed 's/\"//g' | head -1
echo $th | date -d +'%Y/%b/%d-%T'

date: invalid date ‘+%Y/%b/%d-%T’

How can I read in the date and format it?

Amaresh S M
  • 107
  • 4
Ron Russey
  • 128
  • 1
  • 6

1 Answers1

0

Something like this:

echo '2019/08/02-01:23:50+0000' | awk -F'/|\+|-' -v month=$(echo '2019/08/02-01:23:50+0000' | awk -F'-' '{print $1}'|date +%b -f -) '{printf "%s/%s/%s-%s +%s\n", $3,month,$1,$4,$5}'

In one line or

MONTH=$(echo '2019/08/02-01:23:50+0000' | awk -F'-' '{print $1}'|date +%b -f -)
echo '2019/08/02-01:23:50+0000' | awk -F'/|\+|-' -v month="$MONTH" '{printf "%s/%s/%s-%s +%s\n", $3,month,$1,$4,$5}'

First step is to extract 2019/08/02 and convert 08 to Aug then with multiple separators extract all fields and printf with the format we want. Tricky thing here, passing shell variable to awk (fill free to remove \n in printf format)

vx3r
  • 398
  • 2
  • 9