Replace four times with sed
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?
text-processing sed string
New contributor
add a comment |
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?
text-processing sed string
New contributor
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.
– Rakesh Sharma
2 days ago
add a comment |
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?
text-processing sed string
New contributor
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?
text-processing sed string
text-processing sed string
New contributor
New contributor
edited yesterday
G-Man
13.5k93768
13.5k93768
New contributor
asked 2 days ago
fty4fty4
185
185
New contributor
New contributor
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.
– Rakesh Sharma
2 days ago
add a comment |
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.
– Rakesh Sharma
2 days ago
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.
– Rakesh Sharma
2 days ago
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.
– Rakesh Sharma
2 days ago
add a comment |
4 Answers
4
active
oldest
votes
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
- Copy the original line to the hold space.
- Remove the first and last fields in the pattern space.
- 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.
Make (with
G;H;x
) the pattern space contain
- The original string, followed by a newline,
- The modified middle bit, followed by a newline
- The original string again.
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
;
.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;
.Done.
Or you could just use the awk
code.
add a comment |
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.
add a comment |
Another sed with a loop :
sed ':A;s/([^.]*.[^.]*).(.*;[^;]*$)/1,2/;tA' infile
1
-1 for no explanation and for not answering the question as asked.
– G-Man
2 days ago
@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_
2 days ago
@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_
2 days ago
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 thatecho "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 converta.b.c.d.e.f.g
intoa.b,c,d,e,f.g
, and yours doesn’t.
– G-Man
2 days ago
@G-Man hum, ok, interesting and Kusalananda answer handle that ? Rakesh Sharma answer handle that but fail with the data in OP question.
– ctac_
2 days ago
|
show 1 more comment
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.
1
That's rather neat, but it does seem to change some dots in the IP number at the end.
– Kusalananda
2 days ago
Not un front of an xterm so couldn't test it. But it assumes 1 to 6 dots in the input.
– Rakesh Sharma
2 days ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
fty4 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f506807%2freplace-four-times-with-sed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
- Copy the original line to the hold space.
- Remove the first and last fields in the pattern space.
- 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.
Make (with
G;H;x
) the pattern space contain
- The original string, followed by a newline,
- The modified middle bit, followed by a newline
- The original string again.
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
;
.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;
.Done.
Or you could just use the awk
code.
add a comment |
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
- Copy the original line to the hold space.
- Remove the first and last fields in the pattern space.
- 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.
Make (with
G;H;x
) the pattern space contain
- The original string, followed by a newline,
- The modified middle bit, followed by a newline
- The original string again.
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
;
.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;
.Done.
Or you could just use the awk
code.
add a comment |
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
- Copy the original line to the hold space.
- Remove the first and last fields in the pattern space.
- 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.
Make (with
G;H;x
) the pattern space contain
- The original string, followed by a newline,
- The modified middle bit, followed by a newline
- The original string again.
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
;
.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;
.Done.
Or you could just use the awk
code.
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
- Copy the original line to the hold space.
- Remove the first and last fields in the pattern space.
- 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.
Make (with
G;H;x
) the pattern space contain
- The original string, followed by a newline,
- The modified middle bit, followed by a newline
- The original string again.
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
;
.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;
.Done.
Or you could just use the awk
code.
edited 2 days ago
answered 2 days ago
KusalanandaKusalananda
136k17257426
136k17257426
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited yesterday
answered 2 days ago
G-ManG-Man
13.5k93768
13.5k93768
add a comment |
add a comment |
Another sed with a loop :
sed ':A;s/([^.]*.[^.]*).(.*;[^;]*$)/1,2/;tA' infile
1
-1 for no explanation and for not answering the question as asked.
– G-Man
2 days ago
@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_
2 days ago
@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_
2 days ago
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 thatecho "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 converta.b.c.d.e.f.g
intoa.b,c,d,e,f.g
, and yours doesn’t.
– G-Man
2 days ago
@G-Man hum, ok, interesting and Kusalananda answer handle that ? Rakesh Sharma answer handle that but fail with the data in OP question.
– ctac_
2 days ago
|
show 1 more comment
Another sed with a loop :
sed ':A;s/([^.]*.[^.]*).(.*;[^;]*$)/1,2/;tA' infile
1
-1 for no explanation and for not answering the question as asked.
– G-Man
2 days ago
@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_
2 days ago
@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_
2 days ago
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 thatecho "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 converta.b.c.d.e.f.g
intoa.b,c,d,e,f.g
, and yours doesn’t.
– G-Man
2 days ago
@G-Man hum, ok, interesting and Kusalananda answer handle that ? Rakesh Sharma answer handle that but fail with the data in OP question.
– ctac_
2 days ago
|
show 1 more comment
Another sed with a loop :
sed ':A;s/([^.]*.[^.]*).(.*;[^;]*$)/1,2/;tA' infile
Another sed with a loop :
sed ':A;s/([^.]*.[^.]*).(.*;[^;]*$)/1,2/;tA' infile
answered 2 days ago
ctac_ctac_
1,4301211
1,4301211
1
-1 for no explanation and for not answering the question as asked.
– G-Man
2 days ago
@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_
2 days ago
@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_
2 days ago
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 thatecho "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 converta.b.c.d.e.f.g
intoa.b,c,d,e,f.g
, and yours doesn’t.
– G-Man
2 days ago
@G-Man hum, ok, interesting and Kusalananda answer handle that ? Rakesh Sharma answer handle that but fail with the data in OP question.
– ctac_
2 days ago
|
show 1 more comment
1
-1 for no explanation and for not answering the question as asked.
– G-Man
2 days ago
@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_
2 days ago
@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_
2 days ago
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 thatecho "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 converta.b.c.d.e.f.g
intoa.b,c,d,e,f.g
, and yours doesn’t.
– G-Man
2 days ago
@G-Man hum, ok, interesting and Kusalananda answer handle that ? Rakesh Sharma answer handle that but fail with the data in OP question.
– ctac_
2 days ago
1
1
-1 for no explanation and for not answering the question as asked.
– G-Man
2 days ago
-1 for no explanation and for not answering the question as asked.
– G-Man
2 days ago
@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_
2 days ago
@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_
2 days ago
@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_
2 days ago
@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_
2 days ago
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.– G-Man
2 days ago
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.– G-Man
2 days ago
@G-Man hum, ok, interesting and Kusalananda answer handle that ? Rakesh Sharma answer handle that but fail with the data in OP question.
– ctac_
2 days ago
@G-Man hum, ok, interesting and Kusalananda answer handle that ? Rakesh Sharma answer handle that but fail with the data in OP question.
– ctac_
2 days ago
|
show 1 more comment
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.
1
That's rather neat, but it does seem to change some dots in the IP number at the end.
– Kusalananda
2 days ago
Not un front of an xterm so couldn't test it. But it assumes 1 to 6 dots in the input.
– Rakesh Sharma
2 days ago
add a comment |
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.
1
That's rather neat, but it does seem to change some dots in the IP number at the end.
– Kusalananda
2 days ago
Not un front of an xterm so couldn't test it. But it assumes 1 to 6 dots in the input.
– Rakesh Sharma
2 days ago
add a comment |
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.
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.
answered 2 days ago
Rakesh SharmaRakesh Sharma
342115
342115
1
That's rather neat, but it does seem to change some dots in the IP number at the end.
– Kusalananda
2 days ago
Not un front of an xterm so couldn't test it. But it assumes 1 to 6 dots in the input.
– Rakesh Sharma
2 days ago
add a comment |
1
That's rather neat, but it does seem to change some dots in the IP number at the end.
– Kusalananda
2 days ago
Not un front of an xterm so couldn't test it. But it assumes 1 to 6 dots in the input.
– Rakesh Sharma
2 days ago
1
1
That's rather neat, but it does seem to change some dots in the IP number at the end.
– Kusalananda
2 days ago
That's rather neat, but it does seem to change some dots in the IP number at the end.
– Kusalananda
2 days ago
Not un front of an xterm so couldn't test it. But it assumes 1 to 6 dots in the input.
– Rakesh Sharma
2 days ago
Not un front of an xterm so couldn't test it. But it assumes 1 to 6 dots in the input.
– Rakesh Sharma
2 days ago
add a comment |
fty4 is a new contributor. Be nice, and check out our Code of Conduct.
fty4 is a new contributor. Be nice, and check out our Code of Conduct.
fty4 is a new contributor. Be nice, and check out our Code of Conduct.
fty4 is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f506807%2freplace-four-times-with-sed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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.
– Rakesh Sharma
2 days ago