Issue
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?
Solution
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
Answered By - James Brown Answer Checked By - Senaida (WPSolving Volunteer)