Is SED trying to pull a fast one on moi(backreference problem)?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
-1
down vote

favorite
1












I'm trying to pull out the third subexpression from this line:



#EXTRA_GROUPS="dialout cdrom floppy audio video plugdev users"


(yep, the adduser.conf file, for those of you who are curious) with:



sed 's/(EXTRA_GROUPS=)(")(.*)(")/3/' adduser.conf


While this does work and produces



#dialout cdrom floppy audio video plugdev users


(I've left the # symbol out of the expression, so please disregard), this



sed 's/(EXTRA_GROUPS=)(")(.*)(")//3' adduser.conf


doesn't and leaves the file as is.



I do realize that the last example is supposed to delete back reference number three, but when I modify the command with /sd/3 ("replace 3rd subexpression with sd") it doesn't really do anything, either.



I have tried running the command with the -n //p options-all to the same result.



I'm using GNU sed version 4.2.2 on a Debian Jessie box.



Have I missed some crucial part of my core utilities "education" or is it that I haven't been out in the sun for while?










share|improve this question



















  • 2




    s/.../.../3 means "replace 3rd occurrence" (of the entire pattern), not "replace 3rd subexpression".
    – Satō Katsura
    Apr 4 '17 at 11:10














up vote
-1
down vote

favorite
1












I'm trying to pull out the third subexpression from this line:



#EXTRA_GROUPS="dialout cdrom floppy audio video plugdev users"


(yep, the adduser.conf file, for those of you who are curious) with:



sed 's/(EXTRA_GROUPS=)(")(.*)(")/3/' adduser.conf


While this does work and produces



#dialout cdrom floppy audio video plugdev users


(I've left the # symbol out of the expression, so please disregard), this



sed 's/(EXTRA_GROUPS=)(")(.*)(")//3' adduser.conf


doesn't and leaves the file as is.



I do realize that the last example is supposed to delete back reference number three, but when I modify the command with /sd/3 ("replace 3rd subexpression with sd") it doesn't really do anything, either.



I have tried running the command with the -n //p options-all to the same result.



I'm using GNU sed version 4.2.2 on a Debian Jessie box.



Have I missed some crucial part of my core utilities "education" or is it that I haven't been out in the sun for while?










share|improve this question



















  • 2




    s/.../.../3 means "replace 3rd occurrence" (of the entire pattern), not "replace 3rd subexpression".
    – Satō Katsura
    Apr 4 '17 at 11:10












up vote
-1
down vote

favorite
1









up vote
-1
down vote

favorite
1






1





I'm trying to pull out the third subexpression from this line:



#EXTRA_GROUPS="dialout cdrom floppy audio video plugdev users"


(yep, the adduser.conf file, for those of you who are curious) with:



sed 's/(EXTRA_GROUPS=)(")(.*)(")/3/' adduser.conf


While this does work and produces



#dialout cdrom floppy audio video plugdev users


(I've left the # symbol out of the expression, so please disregard), this



sed 's/(EXTRA_GROUPS=)(")(.*)(")//3' adduser.conf


doesn't and leaves the file as is.



I do realize that the last example is supposed to delete back reference number three, but when I modify the command with /sd/3 ("replace 3rd subexpression with sd") it doesn't really do anything, either.



I have tried running the command with the -n //p options-all to the same result.



I'm using GNU sed version 4.2.2 on a Debian Jessie box.



Have I missed some crucial part of my core utilities "education" or is it that I haven't been out in the sun for while?










share|improve this question















I'm trying to pull out the third subexpression from this line:



#EXTRA_GROUPS="dialout cdrom floppy audio video plugdev users"


(yep, the adduser.conf file, for those of you who are curious) with:



sed 's/(EXTRA_GROUPS=)(")(.*)(")/3/' adduser.conf


While this does work and produces



#dialout cdrom floppy audio video plugdev users


(I've left the # symbol out of the expression, so please disregard), this



sed 's/(EXTRA_GROUPS=)(")(.*)(")//3' adduser.conf


doesn't and leaves the file as is.



I do realize that the last example is supposed to delete back reference number three, but when I modify the command with /sd/3 ("replace 3rd subexpression with sd") it doesn't really do anything, either.



I have tried running the command with the -n //p options-all to the same result.



I'm using GNU sed version 4.2.2 on a Debian Jessie box.



Have I missed some crucial part of my core utilities "education" or is it that I haven't been out in the sun for while?







sed regular-expression gnu






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 12 mins ago









Rui F Ribeiro

37.4k1374118




37.4k1374118










asked Apr 4 '17 at 11:03









Max

707




707







  • 2




    s/.../.../3 means "replace 3rd occurrence" (of the entire pattern), not "replace 3rd subexpression".
    – Satō Katsura
    Apr 4 '17 at 11:10












  • 2




    s/.../.../3 means "replace 3rd occurrence" (of the entire pattern), not "replace 3rd subexpression".
    – Satō Katsura
    Apr 4 '17 at 11:10







2




2




s/.../.../3 means "replace 3rd occurrence" (of the entire pattern), not "replace 3rd subexpression".
– Satō Katsura
Apr 4 '17 at 11:10




s/.../.../3 means "replace 3rd occurrence" (of the entire pattern), not "replace 3rd subexpression".
– Satō Katsura
Apr 4 '17 at 11:10










1 Answer
1






active

oldest

votes

















up vote
4
down vote



accepted










The /n flag to the substitute command s in sed does not have anything to do with the back-reference n.



  • With, e.g., s/regex/string/3 you replace the third match of regex (on the current line) with string.

  • With s/regex/3/, you replace the first match of regex with the third capture-group.

  • With s/regex/3/3, you replace the third match of regex with the third capture-group.

In this case, I would probably go with something simpler, like



sed 's/^.*EXTRA_GROUPS="([^"]*)".*$/1/'


There's no use in capturing bits of the string if you're not using it.






share|improve this answer






















  • To clarify, I was referring to the sed -n 's///p' filename(print only the modified line) command. But in any case-killer! That answers my question! I was also curious if I can replace the third regex with some new string, in this case everything inside the double quotes "dialout, cdrom, floppy..." with, say "foobar"?
    – Max
    Apr 4 '17 at 11:22











  • @Kusalananda: Since you have .* at the beginning and at the end of the pattern, you can leave out the ^ and $, because greedy * will eat up everything to the bounds of the line anyhow. As well, you can replace [^"] with ., as long as behaviour for additional double quotes is undefined.
    – Philippos
    Apr 4 '17 at 11:40










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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f355832%2fis-sed-trying-to-pull-a-fast-one-on-moibackreference-problem%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote



accepted










The /n flag to the substitute command s in sed does not have anything to do with the back-reference n.



  • With, e.g., s/regex/string/3 you replace the third match of regex (on the current line) with string.

  • With s/regex/3/, you replace the first match of regex with the third capture-group.

  • With s/regex/3/3, you replace the third match of regex with the third capture-group.

In this case, I would probably go with something simpler, like



sed 's/^.*EXTRA_GROUPS="([^"]*)".*$/1/'


There's no use in capturing bits of the string if you're not using it.






share|improve this answer






















  • To clarify, I was referring to the sed -n 's///p' filename(print only the modified line) command. But in any case-killer! That answers my question! I was also curious if I can replace the third regex with some new string, in this case everything inside the double quotes "dialout, cdrom, floppy..." with, say "foobar"?
    – Max
    Apr 4 '17 at 11:22











  • @Kusalananda: Since you have .* at the beginning and at the end of the pattern, you can leave out the ^ and $, because greedy * will eat up everything to the bounds of the line anyhow. As well, you can replace [^"] with ., as long as behaviour for additional double quotes is undefined.
    – Philippos
    Apr 4 '17 at 11:40














up vote
4
down vote



accepted










The /n flag to the substitute command s in sed does not have anything to do with the back-reference n.



  • With, e.g., s/regex/string/3 you replace the third match of regex (on the current line) with string.

  • With s/regex/3/, you replace the first match of regex with the third capture-group.

  • With s/regex/3/3, you replace the third match of regex with the third capture-group.

In this case, I would probably go with something simpler, like



sed 's/^.*EXTRA_GROUPS="([^"]*)".*$/1/'


There's no use in capturing bits of the string if you're not using it.






share|improve this answer






















  • To clarify, I was referring to the sed -n 's///p' filename(print only the modified line) command. But in any case-killer! That answers my question! I was also curious if I can replace the third regex with some new string, in this case everything inside the double quotes "dialout, cdrom, floppy..." with, say "foobar"?
    – Max
    Apr 4 '17 at 11:22











  • @Kusalananda: Since you have .* at the beginning and at the end of the pattern, you can leave out the ^ and $, because greedy * will eat up everything to the bounds of the line anyhow. As well, you can replace [^"] with ., as long as behaviour for additional double quotes is undefined.
    – Philippos
    Apr 4 '17 at 11:40












up vote
4
down vote



accepted







up vote
4
down vote



accepted






The /n flag to the substitute command s in sed does not have anything to do with the back-reference n.



  • With, e.g., s/regex/string/3 you replace the third match of regex (on the current line) with string.

  • With s/regex/3/, you replace the first match of regex with the third capture-group.

  • With s/regex/3/3, you replace the third match of regex with the third capture-group.

In this case, I would probably go with something simpler, like



sed 's/^.*EXTRA_GROUPS="([^"]*)".*$/1/'


There's no use in capturing bits of the string if you're not using it.






share|improve this answer














The /n flag to the substitute command s in sed does not have anything to do with the back-reference n.



  • With, e.g., s/regex/string/3 you replace the third match of regex (on the current line) with string.

  • With s/regex/3/, you replace the first match of regex with the third capture-group.

  • With s/regex/3/3, you replace the third match of regex with the third capture-group.

In this case, I would probably go with something simpler, like



sed 's/^.*EXTRA_GROUPS="([^"]*)".*$/1/'


There's no use in capturing bits of the string if you're not using it.







share|improve this answer














share|improve this answer



share|improve this answer








edited Apr 4 '17 at 11:22

























answered Apr 4 '17 at 11:13









Kusalananda

111k15216342




111k15216342











  • To clarify, I was referring to the sed -n 's///p' filename(print only the modified line) command. But in any case-killer! That answers my question! I was also curious if I can replace the third regex with some new string, in this case everything inside the double quotes "dialout, cdrom, floppy..." with, say "foobar"?
    – Max
    Apr 4 '17 at 11:22











  • @Kusalananda: Since you have .* at the beginning and at the end of the pattern, you can leave out the ^ and $, because greedy * will eat up everything to the bounds of the line anyhow. As well, you can replace [^"] with ., as long as behaviour for additional double quotes is undefined.
    – Philippos
    Apr 4 '17 at 11:40
















  • To clarify, I was referring to the sed -n 's///p' filename(print only the modified line) command. But in any case-killer! That answers my question! I was also curious if I can replace the third regex with some new string, in this case everything inside the double quotes "dialout, cdrom, floppy..." with, say "foobar"?
    – Max
    Apr 4 '17 at 11:22











  • @Kusalananda: Since you have .* at the beginning and at the end of the pattern, you can leave out the ^ and $, because greedy * will eat up everything to the bounds of the line anyhow. As well, you can replace [^"] with ., as long as behaviour for additional double quotes is undefined.
    – Philippos
    Apr 4 '17 at 11:40















To clarify, I was referring to the sed -n 's///p' filename(print only the modified line) command. But in any case-killer! That answers my question! I was also curious if I can replace the third regex with some new string, in this case everything inside the double quotes "dialout, cdrom, floppy..." with, say "foobar"?
– Max
Apr 4 '17 at 11:22





To clarify, I was referring to the sed -n 's///p' filename(print only the modified line) command. But in any case-killer! That answers my question! I was also curious if I can replace the third regex with some new string, in this case everything inside the double quotes "dialout, cdrom, floppy..." with, say "foobar"?
– Max
Apr 4 '17 at 11:22













@Kusalananda: Since you have .* at the beginning and at the end of the pattern, you can leave out the ^ and $, because greedy * will eat up everything to the bounds of the line anyhow. As well, you can replace [^"] with ., as long as behaviour for additional double quotes is undefined.
– Philippos
Apr 4 '17 at 11:40




@Kusalananda: Since you have .* at the beginning and at the end of the pattern, you can leave out the ^ and $, because greedy * will eat up everything to the bounds of the line anyhow. As well, you can replace [^"] with ., as long as behaviour for additional double quotes is undefined.
– Philippos
Apr 4 '17 at 11:40

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f355832%2fis-sed-trying-to-pull-a-fast-one-on-moibackreference-problem%23new-answer', 'question_page');

);

Post as a guest