I have sample.txt file in my linux system.all the data that is present in my file are in the following date format
Mon Apr 24 16:14:06 PDT 2017 Tue Apr 25 13:31:25 PDT 2017 Mon Apr 03 13:53:03 PDT 2017 Sun Feb 05 16:19:27 PST 2017 Sun Feb 05 17:51:42 PST 2017 Sun Feb 05 18:16:21 PST 2017 Sun Feb 05 19:17:33 PST 2017 Sun Feb 05 21:14:23 PST 2017 Sat Feb 04 10:39:33 PST 2017 Sat Feb 04 10:46:52 PST 2017 Sat Feb 04 14:35:27 PST 2017
I want to convert all this date format in my sample.txt file to YYYY-MM-DD
Format.
Advertisement
Answer
It’s quite simple with date -d
, e.g.
while read -r line; do echo $(date -d "$line" +%Y-%m-%d); done < file.txt
Input File
$ cat file.txt Mon Apr 24 16:14:06 PDT 2017 Tue Apr 25 13:31:25 PDT 2017 Mon Apr 03 13:53:03 PDT 2017 Sun Feb 05 16:19:27 PST 2017 Sun Feb 05 17:51:42 PST 2017 Sun Feb 05 18:16:21 PST 2017 Sun Feb 05 19:17:33 PST 2017 Sun Feb 05 21:14:23 PST 2017 Sat Feb 04 10:39:33 PST 2017 Sat Feb 04 10:46:52 PST 2017 Sat Feb 04 14:35:27 PST 2017
Example Use/Output
$ while read -r line; do echo $(date -d "$line" +%Y-%m-%d); done < file.txt 2017-04-24 2017-04-25 2017-04-03 2017-02-05 2017-02-05 2017-02-05 2017-02-05 2017-02-05 2017-02-04 2017-02-04 2017-02-04