I want to replace dots with commas for some but not all matches:
我想用逗号替换一些但不是所有匹配的点:
hostname_metric (Index: 1) to hostname;metric (avg);22.04.2015 13:40:00;3.0000;22.04.2015 02:05:00;2.0000;22.04.2015 02:00:00;650.7000;2.2594;
The outcome should look like this:
结果应如下所示:
hostname_metric (Index: 1) to hostname;metric (avg);22.04.2015 13:40:00;3,0000;22.04.2015 02:05:00;2,0000;22.04.2015 02:00:00;650,7000;2,2594;
I was able to identify the RegEx which should work to find the correct dots.
我能够识别RegEx,它应该可以找到正确的点。
;[0-9]{1,}\.[0-9]{4}
But how can I replace them with a comma with awk or sed?
但是如何用awk或sed用逗号替换它们呢?
Thanks in advance!
提前致谢!
2 个解决方案
#1
Adding some capture groups to the regex in your question, you can use this sed one-liner:
在你的问题中向正则表达式添加一些捕获组,你可以使用这个sed one-liner:
sed -r 's/(;[0-9]{1,})\.([0-9]{4})/\1,\2/g' file
This matches and captures the part before and after the .
and uses them in the replacement string.
这匹配并捕获了之前和之后的部分。并在替换字符串中使用它们。
On some versions of sed, you may need to use -E
instead of -r
to enable Extended Regular Expressions. If your version of sed doesn't understand either switch, you can use basic regular expressions and add a few escape characters:
在某些版本的sed上,您可能需要使用-E而不是-r来启用扩展正则表达式。如果你的sed版本不理解任何一个开关,你可以使用基本的正则表达式并添加一些转义字符:
sed 's/\(;[0-9]\{1,\}\)\.\([0-9]\{4\}\)/\1,\2/g' file
#2
sed 's/\(;[0-9]\+\)\.\([0-9]\{4\}\)/\1,\2/g'
should do the trick.
sed的/ \(; [0-9] \ + \)\。\([0-9] \ {4 \} \)/ \ 1,\ 2 / g'应该可以解决问题。
#1
Adding some capture groups to the regex in your question, you can use this sed one-liner:
在你的问题中向正则表达式添加一些捕获组,你可以使用这个sed one-liner:
sed -r 's/(;[0-9]{1,})\.([0-9]{4})/\1,\2/g' file
This matches and captures the part before and after the .
and uses them in the replacement string.
这匹配并捕获了之前和之后的部分。并在替换字符串中使用它们。
On some versions of sed, you may need to use -E
instead of -r
to enable Extended Regular Expressions. If your version of sed doesn't understand either switch, you can use basic regular expressions and add a few escape characters:
在某些版本的sed上,您可能需要使用-E而不是-r来启用扩展正则表达式。如果你的sed版本不理解任何一个开关,你可以使用基本的正则表达式并添加一些转义字符:
sed 's/\(;[0-9]\{1,\}\)\.\([0-9]\{4\}\)/\1,\2/g' file
#2
sed 's/\(;[0-9]\+\)\.\([0-9]\{4\}\)/\1,\2/g'
should do the trick.
sed的/ \(; [0-9] \ + \)\。\([0-9] \ {4 \} \)/ \ 1,\ 2 / g'应该可以解决问题。