Replace four times with sed2019 Community Moderator Electionhow to substitute some lines of one file according to the line's contentsed: Multiple -e or while loop?Have trouble with sed command on LinuxReplace matching parentheses with enclosing contentIs there a another way to display a part of a line using sedsed command to replace a word from one file with entire line of another fileDelete Substring from JSON file (malformed)How to change the line a string is found on with the output of another program to which you pass that string as an argument?What's the purpose of adding a prefix on both sides of a shell variable comparison to a string literal?How to improve this 'sed' search & replace command?
Can a wizard cast a spell during their first turn of combat if they initiated combat by releasing a readied spell?
Turning a hard to access nut?
Does the attack bonus from a Masterwork weapon stack with the attack bonus from Masterwork ammunition?
Have the tides ever turned twice on any open problem?
Relation between independence and correlation of uniform random variables
Do native speakers use "ultima" and "proxima" frequently in spoken English?
Knife as defense against stray dogs
Unfrosted light bulb
Why is indicated airspeed rather than ground speed used during the takeoff roll?
Is there a hypothetical scenario that would make Earth uninhabitable for humans, but not for (the majority of) other animals?
Recruiter wants very extensive technical details about all of my previous work
What does Jesus mean regarding "Raca," and "you fool?" - is he contrasting them?
How is the partial sum of a geometric sequence calculated?
gerund and noun applications
Why didn't Héctor fade away after this character died in the movie Coco?
What is the relationship between relativity and the Doppler effect?
How to generate binary array whose elements with values 1 are randomly drawn
Should I use acronyms in dialogues before telling the readers what it stands for in fiction?
Fewest number of steps to reach 200 using special calculator
Using Past-Perfect interchangeably with the Past Continuous
Comment Box for Substitution Method of Integrals
Calculate the frequency of characters in a string
What does "Four-F." mean?
What is the term when voters “dishonestly” choose something that they do not want to choose?
Replace four times with sed
2019 Community Moderator Electionhow to substitute some lines of one file according to the line's contentsed: Multiple -e or while loop?Have trouble with sed command on LinuxReplace matching parentheses with enclosing contentIs there a another way to display a part of a line using sedsed command to replace a word from one file with entire line of another fileDelete Substring from JSON file (malformed)How to change the line a string is found on with the output of another program to which you pass that string as an argument?What's the purpose of adding a prefix on both sides of a shell variable comparison to a string literal?How to improve this 'sed' search & replace command?
I want to replace some commas in this string
2019-03-17T11:32:28.143343Z;1234.5678;901.234;567.89012;3456.78;192.168.0.1
to this
2019-03-17T11:32:28.143343Z;1234,5678;901,234;567,89012;3456,78;192.168.0.1
In words:
replace the second, third, fourth and fifth dot with a comma. The first and sixth 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 some commas in this string
2019-03-17T11:32:28.143343Z;1234.5678;901.234;567.89012;3456.78;192.168.0.1
to this
2019-03-17T11:32:28.143343Z;1234,5678;901,234;567,89012;3456,78;192.168.0.1
In words:
replace the second, third, fourth and fifth dot with a comma. The first and sixth 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
8 hours ago
add a comment |
I want to replace some commas in this string
2019-03-17T11:32:28.143343Z;1234.5678;901.234;567.89012;3456.78;192.168.0.1
to this
2019-03-17T11:32:28.143343Z;1234,5678;901,234;567,89012;3456,78;192.168.0.1
In words:
replace the second, third, fourth and fifth dot with a comma. The first and sixth 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 some commas in this string
2019-03-17T11:32:28.143343Z;1234.5678;901.234;567.89012;3456.78;192.168.0.1
to this
2019-03-17T11:32:28.143343Z;1234,5678;901,234;567,89012;3456,78;192.168.0.1
In words:
replace the second, third, fourth and fifth dot with a comma. The first and sixth 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 12 hours ago
Kusalananda
136k17256424
136k17256424
New contributor
asked 13 hours ago
fty4fty4
184
184
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
8 hours 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
8 hours 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
8 hours 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
8 hours 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).
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
11 hours 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_
8 hours 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_
8 hours 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
7 hours 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_
7 hours 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
8 hours ago
Not un front of an xterm so couldn't test it. But it assumes 1 to 6 dots in the input.
– Rakesh Sharma
8 hours 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 12 hours ago
answered 13 hours ago
KusalanandaKusalananda
136k17256424
136k17256424
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).
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).
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).
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).
answered 6 hours ago
G-ManG-Man
13.5k93668
13.5k93668
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
11 hours 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_
8 hours 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_
8 hours 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
7 hours 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_
7 hours 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
11 hours 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_
8 hours 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_
8 hours 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
7 hours 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_
7 hours 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 12 hours ago
ctac_ctac_
1,4301210
1,4301210
1
-1 for no explanation and for not answering the question as asked.
– G-Man
11 hours 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_
8 hours 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_
8 hours 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
7 hours 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_
7 hours ago
|
show 1 more comment
1
-1 for no explanation and for not answering the question as asked.
– G-Man
11 hours 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_
8 hours 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_
8 hours 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
7 hours 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_
7 hours ago
1
1
-1 for no explanation and for not answering the question as asked.
– G-Man
11 hours ago
-1 for no explanation and for not answering the question as asked.
– G-Man
11 hours 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_
8 hours 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_
8 hours 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_
8 hours 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_
8 hours 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
7 hours 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
7 hours 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_
7 hours 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_
7 hours 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
8 hours ago
Not un front of an xterm so couldn't test it. But it assumes 1 to 6 dots in the input.
– Rakesh Sharma
8 hours 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
8 hours ago
Not un front of an xterm so couldn't test it. But it assumes 1 to 6 dots in the input.
– Rakesh Sharma
8 hours 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 8 hours 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
8 hours ago
Not un front of an xterm so couldn't test it. But it assumes 1 to 6 dots in the input.
– Rakesh Sharma
8 hours 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
8 hours ago
Not un front of an xterm so couldn't test it. But it assumes 1 to 6 dots in the input.
– Rakesh Sharma
8 hours ago
1
1
That's rather neat, but it does seem to change some dots in the IP number at the end.
– Kusalananda
8 hours ago
That's rather neat, but it does seem to change some dots in the IP number at the end.
– Kusalananda
8 hours ago
Not un front of an xterm so couldn't test it. But it assumes 1 to 6 dots in the input.
– Rakesh Sharma
8 hours ago
Not un front of an xterm so couldn't test it. But it assumes 1 to 6 dots in the input.
– Rakesh Sharma
8 hours 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
8 hours ago