awk Stumper:字段内的正则表达式替换
我是 awk 的新手,我似乎无法弄清楚这一点。如何使用正则表达式替换单个字段?
在 Perl 中,我可以将感兴趣的字段分配给一个变量,然后将 $myvar =~ s/foo/bar/g 分配给变量。当然,在 Perl 中我也必须进行自己的字段管理,这在 awk 中更容易。
对于刚才的问题,这是欧洲货币记录,我想将金额字段中的逗号更改为句点。但我只需要定位该字段,因此我不会破坏逗号可以用作散文标点符号的其他字段。
解决办法是不是比我想象的更难?或者更简单?我是否必须更改记录分隔符或类似的俗气内容?
感谢您的帮助!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
sub() 接受第三个参数,它是要更改的字段(或变量):
sub() accepts a third argument which is the field (or variable) to change: