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

Clash Royale CLAN TAG#URR8PPP
up vote
-1
down vote
favorite
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
add a comment |Â
up vote
-1
down vote
favorite
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
2
s/.../.../3means "replace 3rd occurrence" (of the entire pattern), not "replace 3rd subexpression".
â Satà  Katsura
Apr 4 '17 at 11:10
add a comment |Â
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
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
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
sed regular-expression gnu
edited 12 mins ago
Rui F Ribeiro
37.4k1374118
37.4k1374118
asked Apr 4 '17 at 11:03
Max
707
707
2
s/.../.../3means "replace 3rd occurrence" (of the entire pattern), not "replace 3rd subexpression".
â Satà  Katsura
Apr 4 '17 at 11:10
add a comment |Â
2
s/.../.../3means "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
add a comment |Â
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/3you replace the third match ofregex(on the current line) withstring. - With
s/regex/3/, you replace the first match ofregexwith the third capture-group. - With
s/regex/3/3, you replace the third match ofregexwith 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.
To clarify, I was referring to thesed -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
add a comment |Â
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/3you replace the third match ofregex(on the current line) withstring. - With
s/regex/3/, you replace the first match ofregexwith the third capture-group. - With
s/regex/3/3, you replace the third match ofregexwith 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.
To clarify, I was referring to thesed -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
add a comment |Â
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/3you replace the third match ofregex(on the current line) withstring. - With
s/regex/3/, you replace the first match ofregexwith the third capture-group. - With
s/regex/3/3, you replace the third match ofregexwith 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.
To clarify, I was referring to thesed -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
add a comment |Â
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/3you replace the third match ofregex(on the current line) withstring. - With
s/regex/3/, you replace the first match ofregexwith the third capture-group. - With
s/regex/3/3, you replace the third match ofregexwith 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.
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/3you replace the third match ofregex(on the current line) withstring. - With
s/regex/3/, you replace the first match ofregexwith the third capture-group. - With
s/regex/3/3, you replace the third match ofregexwith 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.
edited Apr 4 '17 at 11:22
answered Apr 4 '17 at 11:13
Kusalananda
111k15216342
111k15216342
To clarify, I was referring to thesed -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
add a comment |Â
To clarify, I was referring to thesed -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
add a comment |Â
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
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
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
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
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
2
s/.../.../3means "replace 3rd occurrence" (of the entire pattern), not "replace 3rd subexpression".â Satà  Katsura
Apr 4 '17 at 11:10