Help with script

I have a file which is comma delimited. I have around 35 fields in each row. The problem is in the 21st field I have data separated by �;� I need the data in only the 21st field separated by �;� to be replaced by �|�. This is a very big file and I need something in awk that its much faster than regular commands. Can anybody tell exactly how I can only achieve this for 21st field.

awk -F, '{ sub(";","|",$21) } { print }' "$file"

This is not working as only the first ; is converted to |

awk -F, '{ gsub(";","|",$21) } { print }' "$file"

sed doesn't work for you?

sed 's/;/|/g' file

That will change all semicolons, not just those in the 21st field.

tsk tsk i didn't read the problem :rolleyes:

You forgot the the OFS:

awk -F, '{ gsub(";","|",$21) } { print }' OFS="," "$file"

True, but I would use:

awk -F, -v OFS=, '{ gsub(";","|",$21) } { print }' "$file"

Or:

awk -F, 'BEGIN { OFS = "," } { gsub(";","|",$21) } { print }' "$file"

In that case this should be more comprehensible:

awk 'BEGIN { FS = OFS = "," } { gsub(";","|",$21) } { print }' "$file"

putting the variables behind or in front with -v has some differences and usage. Its documented here and here for anyone interested