Search code examples
linuxdatesedformat

sed command to replace the Sting to DD-MMM-YYYY date format in a record


I am trying to reprint the entire record 06/08/2023 06:30:11 06/08/2023 06:30:19 while replacing the string 06/08/2023 which is in mm/dd/yyyy format to DD-MMM-YYYY format i.e, 08-JUN-2023

I have almost got it. but the instead of printing 08-JUN-2023 it's printing the date 01-Feb-0003

echo "06/08/2023 06:30:11 06/08/2023 06:30:19" | sed -E 's#([0-9]{2})/([0-9]{2})/([0-9]{4})#'"$(echo '\2/\1/\3' | xargs -I{} date -d {} +'%d-%^b-%Y')"#g

everything was working when tested individually. but now this command is giving the below result where the date is wrong 01-Feb-0003 06:30:11 01-Feb-0003 06:30:19

i was researching about sed and date commands, could some one throw light on it and provide the correct solution for this?


Solution

  • Using GNU sed

    $ echo "06/08/2023 06:30:11 06/08/2023 06:30:19" | sed -E "s#[0-9]+/[0-9]+/[0-9]+#echo \$(date -d '&' +'%d-%^h-%Y')#g;s/echo //2ge"
    08-JUN-2023 06:30:11 08-JUN-2023 06:30:19