简体   繁体   English

用RegEx和awk或sed用逗号(,)替换一些点(。)

[英]Replace some dots(.) with commas(,) with RegEx and awk or sed

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! 提前致谢!

Adding some capture groups to the regex in your question, you can use this sed one-liner: 在您的问题中将一些捕获组添加到正则表达式中,您可以使用以下sed单行代码:

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. 在某些版本的sed上,您可能需要使用-E而不是-r来启用扩展正则表达式。 If your version of sed doesn't understand either switch, you can use basic regular expressions and add a few escape characters: 如果您的sed版本不理解任何一个开关,则可以使用基本正则表达式并添加一些转义符:

sed 's/\(;[0-9]\{1,\}\)\.\([0-9]\{4\}\)/\1,\2/g' file

sed 's/\\(;[0-9]\\+\\)\\.\\([0-9]\\{4\\}\\)/\\1,\\2/g'应该可以解决问题。

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

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