Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

looking for an awk gsub|sub|gensub equivalent to sed substitution

Tags:

sed

awk

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?

like image 355
growler11 Avatar asked Nov 01 '25 14:11

growler11


1 Answers

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
like image 54
James Brown Avatar answered Nov 03 '25 18:11

James Brown



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!