Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert to date format (DD MMM YYYY) using the shell?

Tags:

shell

awk

I have data in a file where one column is a date column which has date in the following format:

2021-05-10T18:25:00.000+0100
2021-05-14T18:25:00.000+0100
2021-05-19T18:25:00.000+0100

Expected output is:

10 MAY 2021
14 MAY 2021
19 MAY 2021

My approach which I've tried:

while -r read line
do
    year=`echo $line | awk '{ print $1 }' `
    month=`echo $line | awk '{ print $2 }' `
    dt=`echo $line | awk '{ print $3 }' `

    v=$dt"-"$month"-"$year
    d=date '`$v' | dd-mm-yyyy
    echo $d
done < /f/filename.txt
like image 417
codeholic24 Avatar asked Sep 10 '25 05:09

codeholic24


1 Answers

The GNU coreutils date command supports the input format you have, e.g.:

date -f filename.txt +'%d %b %Y'

Output:

10 May 2021
14 May 2021
19 May 2021

Pipe it through tr a-z A-Z if you want it in all-caps.

Note: tested with version 8.30 of GNU coreutils.

like image 98
Thor Avatar answered Sep 13 '25 07:09

Thor