AWK难倒的:一个领域内的正则表达式替换 [英] awk stumper: regex substitution within a field
问题描述
我是新来的awk,我似乎无法推测这一个。我怎样才能在一个字段中使用常规的前pression替代?
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?
在Perl中,我所感兴趣的领域分配给一个变量,那么 $ MYVAR =〜S /富/酒吧/ G
。当然也可以在Perl我要做我自己的田间管理,这是在awk中更容易。
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?
感谢您的帮助!
推荐答案
子()接受第三个参数,它是字段(或变量)来改变:
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
这篇关于AWK难倒的:一个领域内的正则表达式替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!