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

I need to convert:



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?


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)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.