Replace last two occurences of commas with dots
In each line in I want to replace last two commas with dots
I found such two sed piped method but it does not work with sed on MacOS
$ echo 'abc,def,12379,foo' | sed 's/(.*),/1./'|sed 's/(.*),/1./'
abc,def.12379.foo
Would there be a way which will work with any sed version? Not necessarily with sed only.
sed
add a comment |
In each line in I want to replace last two commas with dots
I found such two sed piped method but it does not work with sed on MacOS
$ echo 'abc,def,12379,foo' | sed 's/(.*),/1./'|sed 's/(.*),/1./'
abc,def.12379.foo
Would there be a way which will work with any sed version? Not necessarily with sed only.
sed
2
How does it fail on MacOS?
– choroba
Jan 11 at 9:23
Can you add-e
to the sed command? So like this:echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./'|sed -e 's/(.*),/1./'
.
– rowan
Jan 11 at 9:29
add a comment |
In each line in I want to replace last two commas with dots
I found such two sed piped method but it does not work with sed on MacOS
$ echo 'abc,def,12379,foo' | sed 's/(.*),/1./'|sed 's/(.*),/1./'
abc,def.12379.foo
Would there be a way which will work with any sed version? Not necessarily with sed only.
sed
In each line in I want to replace last two commas with dots
I found such two sed piped method but it does not work with sed on MacOS
$ echo 'abc,def,12379,foo' | sed 's/(.*),/1./'|sed 's/(.*),/1./'
abc,def.12379.foo
Would there be a way which will work with any sed version? Not necessarily with sed only.
sed
sed
asked Jan 11 at 9:18
ChrisChris
146112
146112
2
How does it fail on MacOS?
– choroba
Jan 11 at 9:23
Can you add-e
to the sed command? So like this:echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./'|sed -e 's/(.*),/1./'
.
– rowan
Jan 11 at 9:29
add a comment |
2
How does it fail on MacOS?
– choroba
Jan 11 at 9:23
Can you add-e
to the sed command? So like this:echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./'|sed -e 's/(.*),/1./'
.
– rowan
Jan 11 at 9:29
2
2
How does it fail on MacOS?
– choroba
Jan 11 at 9:23
How does it fail on MacOS?
– choroba
Jan 11 at 9:23
Can you add
-e
to the sed command? So like this: echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./'|sed -e 's/(.*),/1./'
.– rowan
Jan 11 at 9:29
Can you add
-e
to the sed command? So like this: echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./'|sed -e 's/(.*),/1./'
.– rowan
Jan 11 at 9:29
add a comment |
3 Answers
3
active
oldest
votes
Try this:
$ echo 'abc,def,12379,foo' | sed 's/,([^,]*),([^,]*)$/.1.2/'
abc,def.12379.foo
This will leave lines with a single comma alone (eg foo,bar
). That may or may not be what you want.
(...)
capture groups and 1
, 2
backrerefences in the replacement string should be supported in all versions of sed
; I've tested the above on Unix v7.
PS. Even the double sed from your question should work on MacOS; maybe your problem is that the lines are terminated by CarriageReturns instead of LineFeeds?
add a comment |
echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./' -e 's/(.*),/1./'
or
echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./;s/(.*),/1./'
add a comment |
Using awk
, treating the input as a set of comma-delimited fields, and joining the three last fields with dots,
echo 'abc,def,12379,foo' |
awk 'BEGIN { OFS=FS="," } { print $1, $2 "." $3 "." $4 }'
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
});
}
});
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%2f493903%2freplace-last-two-occurences-of-commas-with-dots%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this:
$ echo 'abc,def,12379,foo' | sed 's/,([^,]*),([^,]*)$/.1.2/'
abc,def.12379.foo
This will leave lines with a single comma alone (eg foo,bar
). That may or may not be what you want.
(...)
capture groups and 1
, 2
backrerefences in the replacement string should be supported in all versions of sed
; I've tested the above on Unix v7.
PS. Even the double sed from your question should work on MacOS; maybe your problem is that the lines are terminated by CarriageReturns instead of LineFeeds?
add a comment |
Try this:
$ echo 'abc,def,12379,foo' | sed 's/,([^,]*),([^,]*)$/.1.2/'
abc,def.12379.foo
This will leave lines with a single comma alone (eg foo,bar
). That may or may not be what you want.
(...)
capture groups and 1
, 2
backrerefences in the replacement string should be supported in all versions of sed
; I've tested the above on Unix v7.
PS. Even the double sed from your question should work on MacOS; maybe your problem is that the lines are terminated by CarriageReturns instead of LineFeeds?
add a comment |
Try this:
$ echo 'abc,def,12379,foo' | sed 's/,([^,]*),([^,]*)$/.1.2/'
abc,def.12379.foo
This will leave lines with a single comma alone (eg foo,bar
). That may or may not be what you want.
(...)
capture groups and 1
, 2
backrerefences in the replacement string should be supported in all versions of sed
; I've tested the above on Unix v7.
PS. Even the double sed from your question should work on MacOS; maybe your problem is that the lines are terminated by CarriageReturns instead of LineFeeds?
Try this:
$ echo 'abc,def,12379,foo' | sed 's/,([^,]*),([^,]*)$/.1.2/'
abc,def.12379.foo
This will leave lines with a single comma alone (eg foo,bar
). That may or may not be what you want.
(...)
capture groups and 1
, 2
backrerefences in the replacement string should be supported in all versions of sed
; I've tested the above on Unix v7.
PS. Even the double sed from your question should work on MacOS; maybe your problem is that the lines are terminated by CarriageReturns instead of LineFeeds?
edited Jan 12 at 18:19
answered Jan 12 at 17:02
mosvymosvy
6,2711425
6,2711425
add a comment |
add a comment |
echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./' -e 's/(.*),/1./'
or
echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./;s/(.*),/1./'
add a comment |
echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./' -e 's/(.*),/1./'
or
echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./;s/(.*),/1./'
add a comment |
echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./' -e 's/(.*),/1./'
or
echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./;s/(.*),/1./'
echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./' -e 's/(.*),/1./'
or
echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./;s/(.*),/1./'
answered Jan 12 at 16:19
Emilio GalarragaEmilio Galarraga
50929
50929
add a comment |
add a comment |
Using awk
, treating the input as a set of comma-delimited fields, and joining the three last fields with dots,
echo 'abc,def,12379,foo' |
awk 'BEGIN { OFS=FS="," } { print $1, $2 "." $3 "." $4 }'
add a comment |
Using awk
, treating the input as a set of comma-delimited fields, and joining the three last fields with dots,
echo 'abc,def,12379,foo' |
awk 'BEGIN { OFS=FS="," } { print $1, $2 "." $3 "." $4 }'
add a comment |
Using awk
, treating the input as a set of comma-delimited fields, and joining the three last fields with dots,
echo 'abc,def,12379,foo' |
awk 'BEGIN { OFS=FS="," } { print $1, $2 "." $3 "." $4 }'
Using awk
, treating the input as a set of comma-delimited fields, and joining the three last fields with dots,
echo 'abc,def,12379,foo' |
awk 'BEGIN { OFS=FS="," } { print $1, $2 "." $3 "." $4 }'
edited Jan 12 at 16:59
answered Jan 12 at 16:47
KusalanandaKusalananda
124k16234385
124k16234385
add a comment |
add a comment |
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%2f493903%2freplace-last-two-occurences-of-commas-with-dots%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
2
How does it fail on MacOS?
– choroba
Jan 11 at 9:23
Can you add
-e
to the sed command? So like this:echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./'|sed -e 's/(.*),/1./'
.– rowan
Jan 11 at 9:29