Awk stumper: replace regular expression inside field

I am new to awk and I seem to be unable to figure this out. How can I replace regular expression in one field?

In perl, I could set the field of interest to the variable value, and then $myvar =~ s/foo/bar/g . Of course, also in perl I have to do my own field control, and it is easier in awk.

For the problem under consideration, these are now European money records, and I want to change the comma for the periods in the amount field. But I need to target only this field, so I don’t manipulate other fields where commas can be used as punctuation of prose.

Is the solution more complex than I imagine? Or is it easier? Do I need to change the record separator or something so sticky?

Thank you for your help!

+5
source share
1 answer

sub () takes a third argument, which is a change to the field (or variable):

 $ echo '02/08/2011 7,33 Shopping' | awk '{sub(/,/,".",$2)} 1' 02/08/2011 7.33 Shopping 
+6
source

All Articles