我有一个包含一些数据的文件,但有一堆恼人的数字小于 1,我想将其更改为 1,而不是手动执行。我想知道你会如何在 Perl 中做到这一点。
我尝试过使用这样的东西
perl -pe 's/\d+/$& < 1 ? $&=1 : $&/g' file
这基本上只是查找所有数字,然后检查它们是否小于 1。如果是,则将其设置为 1,如果不是,则将其保留。不幸的是,它不允许 $&=1 发生,因为它是只读的。 perl中还有其他东西可以达到这种效果吗? 输入示例:
this 1 is a 7 file that
has 0.5 some numbers 4
that are 0.3 less 0.1 than 0.9
one as you see 1.1
输出:
this 1 is a 7 file that
has 1 some numbers 4
that are 1 less 1 than 1
one as you see 1.1
最佳答案
关于regex - 查找文件中的数字并使用 perl 更改其值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30848536/