5

I'm new to awk, and I can't seem to figure this one out. How can I substitute in a single field using a regular expression?

In perl, I could assign the field of interest to a variable, then $myvar =~ s/foo/bar/g. Of course also in perl I have to do my own field management, and that's easier in awk.

For the issue at hand just now, it's European money records and I want to change commas to periods in the amount field. But I need to target only that field, so I don't mangle the other fields where commas could be used as prose punctuation.

Is the solution more difficult than I imagine? Or simpler? Do I have to change the record separator or something tacky like that?

Thank you for your help!

Benoit
  • 76,634
  • 23
  • 210
  • 236
rockriver
  • 51
  • 1
  • 3

1 Answers1

6

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

$ echo '02/08/2011 7,33 Shopping' | awk '{sub(/,/,".",$2)} 1'
02/08/2011 7.33 Shopping
marco
  • 4,455
  • 1
  • 23
  • 20
  • Wow, not sure how I missed that in my O'Reilly. There it is. Must have been the all-nighter. THANK YOU! – rockriver Feb 08 '11 at 13:43