I am using the following sed command which I would like to transfer to awk to change the date from 2023-12-15 to 15/12/2023:
echo "c_az_6332,2023-12-15,-24.01,BP_Connect,Particulars,details" \
 | sed 's/\(....\)-\(..\)-\(..\)/\3\/\2\/\1/g'
which results in
c_az_6332,15/12/2023,-24.01,BP_Connect,Particulars,details
How can I do the same using awk gsub|sub|gensub?
I have tried the following:
echo "c_az_6332,2023-12-15,-24.01,BP_Connect,Particulars,details" \
 | awk -F "," '{gsub(\(....\)-\(..\)-\(..\),\3\/\2\/\1,$2) ; print $0}'
and
echo "c_az_6332,2023-12-15,-24.01,BP_Connect,Particulars,details" \
 | awk -F "," '{gsub(/\(....\)-\(..\)-\(..\)/,\3\/\2\/\1,$2) ; print $0}'
both won't work. Can someone please help with this?
Using GNU awk and gensub():
$ gawk '{
      print gensub(/(....)-(..)-(..)/,"\\3/\\2/\\1",1)
}' <<< "c_az_6332,2023-12-15,-24.01,BP_Connect,Particulars,details" 
Output:
c_az_6332,15/12/2023,-24.01,BP_Connect,Particulars,details
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With