3

I want to replace the second, third, fourth and fifth dots in this string

2019-03-17T11:32:28.143343Z;1234.5678;901.234;567.89012;3456.78;192.168.0.1

with a commas, to get this result:

2019-03-17T11:32:28.143343Z;1234,5678;901,234;567,89012;3456,78;192.168.0.1

The first comma and the sixth (and any after that) should stay the same.

I found this command, which I could execute multiple times (but maybe not the best practise):

echo "$tmp" | sed 's/\./\,/2'

How can I get this done in one command?

1
  • The question is framed improperly. 2nd to 5th dots to comma, but why 1st and 6th mentioned when there are other dots as well which don't change. Mar 17, 2019 at 18:40

4 Answers 4

8

Your data consists of six ;-delimited fields, and you'd like to replace the dots in fields 2 through to 5 (not 1 or 6) with commas.

This is easiest done with awk:

awk -F ';' 'BEGIN { OFS=FS } { for (i=2; i<=5; ++i) gsub("\\.", ",", $i); print }' file

With the example data given, this produces

2019-03-17T11:32:28.143343Z;1234,5678;901,234;567,89012;3456,78;192.168.0.1

The code simply iterates of the ;-delimited fields of each input line and calls gsub() to do a global search and replace (as you would do with s/\./,/g or y/./,/ in sed) on the individual fields that the loop iterates over.

The modified line is then printed.

The -F option sets the input field separator to a semicolon, and we use the BEGIN block to also set the output field separator to the same value (you would otherwise get space-separated fields).


Using sed, you might do something like

sed 's/\./,/2; s/\./,/2; s/\./,/2; s/\./,/2' file

I.e., replace the 2nd dot four times (which one is the 2nd dot will change with each substitution, since you substitute them). This does however assume that the number of values within each field remains static.

To work around this in case you at some point have more than two dot-delimited things in a field, you can do

sed 'h; s/^[^;]*;//; s/;[^;]*$//; y/./,/; G;H;x; s/;[^\n]*\n/;/; s/\n.*;/;/' file

In short, these commands do

  1. Copy the original line to the hold space.
  2. Remove the first and last fields in the pattern space.
  3. Change all dots to commas in the pattern space (that's the y command). All dots that should change into commas have now been changed. Now we must reassemble the line from the middle bit in the pattern space and the original data in the hold space.
  4. Make (with G;H;x) the pattern space contain

    1. The original string, followed by a newline,
    2. The modified middle bit, followed by a newline
    3. The original string again.
  5. So now the pattern space contains three lines. Remove everything but the first field on the first line, and the newline, and replace that removed bit with a ;.

  6. Do a similar thing with the last line, i.e. remove the (now lone) newline and everything up to the last ;, and replace with a ;.

  7. Done.

Or you could just use the awk code.

1

Since the other answers are making assumptions about the input that are not stated in the question (e.g., that it is a bunch of ;-separated values, or that there are exactly six dots), I’ll provide this slightly clunky answer that does what the question asks for:

sed 's/^\([^.]*\.[^.]*\)\.\([^.]*\)\.\([^.]*\)\.\([^.]*\)\./\1,\2,\3,\4,/'

This breaks down each input line as follows:

  • Capture group 1: Starting at the beginning of the line, any number of characters other than ., then one . (the first one in the line), then another arbitrarily long sequence of characters other than .,
  • A . (the second one in the line),
  • Capture group 2: Any number of characters other than .,
  • A . (the third one in the line),
  • Capture group 3: Any number of characters other than .,
  • A . (the fourth one in the line),
  • Capture group 4: Any number of characters other than .,
  • A . (the fifth one in the line),
  • Whatever follows (not matched by the regular expression, but there can be more to the line than the above, because the regex doesn’t end with $).

And replaces it with

  • Capture group 1: Everything up to the second . in the line (including the first one),
  • A , (replacing the second .),
  • Capture group 2: Everything between the second . and the third one,
  • A , (replacing the third .),
  • Capture group 3: Everything between the third . and the fourth one,
  • A , (replacing the fourth .),
  • Capture group 4: Everything between the fourth . and the fifth one,
  • A , (replacing the fifth .),
  • Whatever follows the fifth ..

So it replaces the second, third, fourth and fifth dots with commas.

  • This will make no changes on a line with fewer than five dots.
  • This will leave an arbitrary number of dots after the fifth unchanged.
  • This will replace the second, third, fourth and fifth dots, even if there are only five dots in the line (i.e., there is no sixth one).

Here’s another approach that’s specifically for GNU sed:

sed 's/\./\n/6g; s/\./,/2g; s/\n/./g'
  • s/\./\n/6g replaces all dots starting with the sixth one with newlines.
  • s/\./,/2g  replaces all dots starting with the second one with commas.  But this is really only the second through the fifth, since the first command eliminated all dots past the fifth (if any).
  • s/\n/./g changes all the newlines back to dots.  Of course, the only newlines in the line are the ones that were originally dots, so this just changes them back to what they were.

So, if a line has only three dots, this will change the second and the third (even though the fourth and fifth don’t exist).

Warning:  The behavior of the combination of a number and a g as flags on an s command is not specified by POSIX and may vary between implementations.  This is how it works for GNU SED, as documented in the GNU SED manual.

-1

Another sed with a loop :

sed ':A;s/\([^.]*\.[^.]*\)\.\(.*;[^;]*$\)/\1,\2/;tA' infile
6
  • 1
    -1 for no explanation and for not answering the question as asked. Mar 17, 2019 at 15:01
  • @G-Man : Thanks for downvote : I don't look for reputation. I'm here to learn and try to help other. no explanation ? I say it is a loop, no? but you forgot to tell why not answering the question ! So -1 for my answer, It is a bad answer. Someone come here don't look at that but I get the espected result, no? As you can see, a lot of time there is no feedback. Askers got answer and go away. A lot of time, they are not interested by explanation. If they are, they ask for explanation via comment.
    – ctac_
    Mar 17, 2019 at 18:31
  • @G-Man : An other thing, all here don't speak english very well, like me. It's more difficult for them first to understand the question and second to answer with explanation. Sometime they get downvote because explanation are not clear enough.
    – ctac_
    Mar 17, 2019 at 18:33
  • The question said “I want to replace … the second, third, fourth and fifth dot [in this string] with a comma.”  If you take the question super-literally, you could say that echo "2019-03-17T11:32:28.143343Z;1234,5678;901,234;567,89012;3456,78;192.168.0.1" was an answer — but that would be ridiculous.  But you assumed that the input would be exactly like the example — that it would contain some semicolons — and you posted an answer that depends on that assumption.  Based on what the question says, any answer should convert a.b.c.d.e.f.g into a.b,c,d,e,f.g, and yours doesn’t. Mar 17, 2019 at 19:23
  • @G-Man hum, ok, interesting and Kusalananda answer handle that ? Rakesh Sharma answer handle that but fail with the data in OP question.
    – ctac_
    Mar 17, 2019 at 19:42
-1

You could approach this using sed editor as follows:

$ sed -e '
    y/./\n/
    s/\n\(.*\)\n/.\1./
    y/\n/,/
' input.txt

The premise being that we first turn all dots into newlines, a char guaranteed to not be there in the pattern space. Then we change the last and the first newlines back into dots. The remaining newlines all get converted to commas.

HTH.

2
  • 1
    That's rather neat, but it does seem to change some dots in the IP number at the end.
    – Kusalananda
    Mar 17, 2019 at 18:28
  • Not un front of an xterm so couldn't test it. But it assumes 1 to 6 dots in the input. Mar 17, 2019 at 18:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .