awk stumper: regex substitution within a field

孤人 提交于 2020-01-13 10:14:14

问题


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!


回答1:


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


来源:https://stackoverflow.com/questions/4931034/awk-stumper-regex-substitution-within-a-field

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!