0

I am getting date in one of file in the format "Fri Feb 25 16:07:17 UTC 2022" Which I want to convert it to "2022-02-25 16:07:17". However I am able to do it using Python but its being long process to call python from shell script and getting that variable back.

Just I am looking for simple option to convert "Fri Feb 25 16:07:17 UTC 2022" it to "2022-02-25 16:07:17" using bash shell script.

3
  • Thanks @Panki I have updated the question Commented Feb 25, 2022 at 18:05
  • 1
    To be pedantic, bash itself cannot do this. You have to some some external command. date or python are both external tools. Commented Feb 25, 2022 at 20:54
  • thanks @glennjackman, I used the below provided answer by Panki, Its working fine for me. Actually i wanted to get rid of using Python inside the shell and it was being overhead just for date conversion. Commented Feb 26, 2022 at 4:21

1 Answer 1

3

You can use date for this task.

mydate="Fri Feb 25 16:07:17 UTC 2022"
date -u -d "$mydate" "+%F %T"

-u tells date to output the result in UTC. See man date for more formatting options.

1
  • Thanks its working perfectly . Commented Feb 25, 2022 at 18:16

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.