sunn_bmixer
sunn_bmixer

Reputation: 31

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.

;[0-9]{1,}\.[0-9]{4}

But how can I replace them with a comma with awk or sed?

Thanks in advance!

Upvotes: 3

Views: 1940

Answers (2)

cmbuckley
cmbuckley

Reputation: 42517

sed 's/\(;[0-9]\+\)\.\([0-9]\{4\}\)/\1,\2/g' should do the trick.

Upvotes: 1

Tom Fenech
Tom Fenech

Reputation: 74685

Adding some capture groups to the regex in your question, you can use this 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 's/\(;[0-9]\{1,\}\)\.\([0-9]\{4\}\)/\1,\2/g' file

Upvotes: 4

Related Questions