简体   繁体   English

大于/小于所有行的awk sed过滤器值

[英]awk sed filter values in all lines greater/smaller than

is there a way to construct a filter in awk (or something similar) that for a given file, say: 有没有一种方法可以在awk(或类似方式)中为给定文件构造过滤器,例如:

0.99,0.98,1.1,0.85,0.92
0.76,1.4,0.99,0.99,0.82
1.0,1.45,0.78,0.91,0.95

would replace any record in a line that is greater than 1.0 with 1.0? 将大于1.0的行中的任何记录替换为1.0?

Here is something you can do with awk 这是您可以使用awk

awk -F, '{for(i=1;i<=NF;i++) if($i>1) {$i="replacement"}}1' OFS=, file

Test: 测试:

$ cat file
0.99,0.98,1.1,0.85,0.92
0.76,1.4,0.99,0.99,0.82
1.0,1.45,0.78,0.91,0.95

$ awk -F, '{for(i=1;i<=NF;i++) if($i>1) {$i="replacement"}}1' OFS=, file
0.99,0.98,replacement,0.85,0.92
0.76,replacement,0.99,0.99,0.82
1.0,replacement,0.78,0.91,0.95

Here's a sed solution: 这是一个sed解决方案:

sed -e 's/[1-9][0-9]*\.[0-9]*/1.0/g' in-file > out-file

The pattern [1-9][0-9]*\\.[0-9]* simply matches any sequence that begins with a digit greater than 0 , followed by zero or more digits, followed by the decimal point, followed by additional digits. 模式[1-9][0-9]*\\.[0-9]*仅匹配以大于0的数字开头,后接零个或多个数字,后接小数点,再加上其他任何序列数字。 If you want an in-place replacement, you can use the -i option: 如果要就地替换,可以使用-i选项:

sed -i -e 's/[1-9][0-9]*\.[0-9]*/1.0/g' in-file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM