Is there a way to replace last occurrence of match using a shell variable substitution?
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
% x=abracadabra
% echo $x//a/o
obrocodobro
Hmph...
Is there a way to replace the last occurrence of a pattern using shell substitions (IOW, without rolling out sed
, awk
, perl
, etc.)?
bash scripting wildcards replace
add a comment |Â
up vote
2
down vote
favorite
% x=abracadabra
% echo $x//a/o
obrocodobro
Hmph...
Is there a way to replace the last occurrence of a pattern using shell substitions (IOW, without rolling out sed
, awk
, perl
, etc.)?
bash scripting wildcards replace
Nope there isn't
â llua
Feb 27 at 2:41
@llua: dang ...
â kjo
Feb 27 at 2:44
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
% x=abracadabra
% echo $x//a/o
obrocodobro
Hmph...
Is there a way to replace the last occurrence of a pattern using shell substitions (IOW, without rolling out sed
, awk
, perl
, etc.)?
bash scripting wildcards replace
% x=abracadabra
% echo $x//a/o
obrocodobro
Hmph...
Is there a way to replace the last occurrence of a pattern using shell substitions (IOW, without rolling out sed
, awk
, perl
, etc.)?
bash scripting wildcards replace
asked Feb 27 at 2:39
kjo
3,84873358
3,84873358
Nope there isn't
â llua
Feb 27 at 2:41
@llua: dang ...
â kjo
Feb 27 at 2:44
add a comment |Â
Nope there isn't
â llua
Feb 27 at 2:41
@llua: dang ...
â kjo
Feb 27 at 2:44
Nope there isn't
â llua
Feb 27 at 2:41
Nope there isn't
â llua
Feb 27 at 2:41
@llua: dang ...
â kjo
Feb 27 at 2:44
@llua: dang ...
â kjo
Feb 27 at 2:44
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
Note: this replaces a trailing occurrence - not quite the same as "the last occurrence"
From the Bash reference manual Section 3.5.3 Shell Parameter Expansion
:
$parameter/pattern/string
The pattern is expanded to produce a pattern just as in filename expansion. Parameter is expanded and the
longest match of pattern against its value is replaced with string. If
pattern begins with âÂÂ/âÂÂ, all matches of pattern are replaced with
string. Normally only the first match is replaced. If pattern begins
with âÂÂ#âÂÂ, it must match at the beginning of the expanded value of
parameter. If pattern begins with âÂÂ%âÂÂ, it must match at the end of the
expanded value of parameter. If string is null, matches of pattern are
deleted and the / following pattern may be omitted. If the nocasematch
shell option (see the description of shopt in The Shopt Builtin) is
enabled, the match is performed without regard to the case of
alphabetic characters. If parameter is âÂÂ@â or âÂÂâÂÂ, the substitution
operation is applied to each positional parameter in turn, and the
expansion is the resultant list. If parameter is an array variable
subscripted with âÂÂ@â or âÂÂâÂÂ, the substitution operation is applied to
each member of the array in turn, and the expansion is the resultant
list.
So
$ x=abracadabra
$ echo "$x/%a/o"
abracadabro
add a comment |Â
up vote
1
down vote
You can do this with shell substitutions its just a bit tricky. You basically grab everything before the pattern insert your replacement and than grab everything after the pattern. For your example that would look like this:
x=abracadabra
echo "$x%a*o$x##*a"
EDIT: Or just do what steeldriver suggested in the comments.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Note: this replaces a trailing occurrence - not quite the same as "the last occurrence"
From the Bash reference manual Section 3.5.3 Shell Parameter Expansion
:
$parameter/pattern/string
The pattern is expanded to produce a pattern just as in filename expansion. Parameter is expanded and the
longest match of pattern against its value is replaced with string. If
pattern begins with âÂÂ/âÂÂ, all matches of pattern are replaced with
string. Normally only the first match is replaced. If pattern begins
with âÂÂ#âÂÂ, it must match at the beginning of the expanded value of
parameter. If pattern begins with âÂÂ%âÂÂ, it must match at the end of the
expanded value of parameter. If string is null, matches of pattern are
deleted and the / following pattern may be omitted. If the nocasematch
shell option (see the description of shopt in The Shopt Builtin) is
enabled, the match is performed without regard to the case of
alphabetic characters. If parameter is âÂÂ@â or âÂÂâÂÂ, the substitution
operation is applied to each positional parameter in turn, and the
expansion is the resultant list. If parameter is an array variable
subscripted with âÂÂ@â or âÂÂâÂÂ, the substitution operation is applied to
each member of the array in turn, and the expansion is the resultant
list.
So
$ x=abracadabra
$ echo "$x/%a/o"
abracadabro
add a comment |Â
up vote
3
down vote
accepted
Note: this replaces a trailing occurrence - not quite the same as "the last occurrence"
From the Bash reference manual Section 3.5.3 Shell Parameter Expansion
:
$parameter/pattern/string
The pattern is expanded to produce a pattern just as in filename expansion. Parameter is expanded and the
longest match of pattern against its value is replaced with string. If
pattern begins with âÂÂ/âÂÂ, all matches of pattern are replaced with
string. Normally only the first match is replaced. If pattern begins
with âÂÂ#âÂÂ, it must match at the beginning of the expanded value of
parameter. If pattern begins with âÂÂ%âÂÂ, it must match at the end of the
expanded value of parameter. If string is null, matches of pattern are
deleted and the / following pattern may be omitted. If the nocasematch
shell option (see the description of shopt in The Shopt Builtin) is
enabled, the match is performed without regard to the case of
alphabetic characters. If parameter is âÂÂ@â or âÂÂâÂÂ, the substitution
operation is applied to each positional parameter in turn, and the
expansion is the resultant list. If parameter is an array variable
subscripted with âÂÂ@â or âÂÂâÂÂ, the substitution operation is applied to
each member of the array in turn, and the expansion is the resultant
list.
So
$ x=abracadabra
$ echo "$x/%a/o"
abracadabro
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Note: this replaces a trailing occurrence - not quite the same as "the last occurrence"
From the Bash reference manual Section 3.5.3 Shell Parameter Expansion
:
$parameter/pattern/string
The pattern is expanded to produce a pattern just as in filename expansion. Parameter is expanded and the
longest match of pattern against its value is replaced with string. If
pattern begins with âÂÂ/âÂÂ, all matches of pattern are replaced with
string. Normally only the first match is replaced. If pattern begins
with âÂÂ#âÂÂ, it must match at the beginning of the expanded value of
parameter. If pattern begins with âÂÂ%âÂÂ, it must match at the end of the
expanded value of parameter. If string is null, matches of pattern are
deleted and the / following pattern may be omitted. If the nocasematch
shell option (see the description of shopt in The Shopt Builtin) is
enabled, the match is performed without regard to the case of
alphabetic characters. If parameter is âÂÂ@â or âÂÂâÂÂ, the substitution
operation is applied to each positional parameter in turn, and the
expansion is the resultant list. If parameter is an array variable
subscripted with âÂÂ@â or âÂÂâÂÂ, the substitution operation is applied to
each member of the array in turn, and the expansion is the resultant
list.
So
$ x=abracadabra
$ echo "$x/%a/o"
abracadabro
Note: this replaces a trailing occurrence - not quite the same as "the last occurrence"
From the Bash reference manual Section 3.5.3 Shell Parameter Expansion
:
$parameter/pattern/string
The pattern is expanded to produce a pattern just as in filename expansion. Parameter is expanded and the
longest match of pattern against its value is replaced with string. If
pattern begins with âÂÂ/âÂÂ, all matches of pattern are replaced with
string. Normally only the first match is replaced. If pattern begins
with âÂÂ#âÂÂ, it must match at the beginning of the expanded value of
parameter. If pattern begins with âÂÂ%âÂÂ, it must match at the end of the
expanded value of parameter. If string is null, matches of pattern are
deleted and the / following pattern may be omitted. If the nocasematch
shell option (see the description of shopt in The Shopt Builtin) is
enabled, the match is performed without regard to the case of
alphabetic characters. If parameter is âÂÂ@â or âÂÂâÂÂ, the substitution
operation is applied to each positional parameter in turn, and the
expansion is the resultant list. If parameter is an array variable
subscripted with âÂÂ@â or âÂÂâÂÂ, the substitution operation is applied to
each member of the array in turn, and the expansion is the resultant
list.
So
$ x=abracadabra
$ echo "$x/%a/o"
abracadabro
answered Feb 27 at 2:49
steeldriver
31.5k34978
31.5k34978
add a comment |Â
add a comment |Â
up vote
1
down vote
You can do this with shell substitutions its just a bit tricky. You basically grab everything before the pattern insert your replacement and than grab everything after the pattern. For your example that would look like this:
x=abracadabra
echo "$x%a*o$x##*a"
EDIT: Or just do what steeldriver suggested in the comments.
add a comment |Â
up vote
1
down vote
You can do this with shell substitutions its just a bit tricky. You basically grab everything before the pattern insert your replacement and than grab everything after the pattern. For your example that would look like this:
x=abracadabra
echo "$x%a*o$x##*a"
EDIT: Or just do what steeldriver suggested in the comments.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You can do this with shell substitutions its just a bit tricky. You basically grab everything before the pattern insert your replacement and than grab everything after the pattern. For your example that would look like this:
x=abracadabra
echo "$x%a*o$x##*a"
EDIT: Or just do what steeldriver suggested in the comments.
You can do this with shell substitutions its just a bit tricky. You basically grab everything before the pattern insert your replacement and than grab everything after the pattern. For your example that would look like this:
x=abracadabra
echo "$x%a*o$x##*a"
EDIT: Or just do what steeldriver suggested in the comments.
answered Feb 27 at 2:46
Captain Wobbles
1565
1565
add a comment |Â
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%2f426831%2fis-there-a-way-to-replace-last-occurrence-of-match-using-a-shell-variable-substi%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
Nope there isn't
â llua
Feb 27 at 2:41
@llua: dang ...
â kjo
Feb 27 at 2:44