Sed substitute pattern with commandReplace data between quotes in a fileRegex and piped commands with...
Is this cheap "air conditioner" able to cool a room?
How to help new students accept function notation
Team goes to lunch frequently, I do intermittent fasting but still want to socialize
In the movie Harry Potter and the Order or the Phoenix, why didn't Mr. Filch succeed to open the Room of Requirement if it's what he needed?
Colleagues speaking another language and it impacts work
Looking for a new job because of relocation - is it okay to tell the real reason?
Replace data between quotes in a file
Why is there a need to prevent a racist, sexist, or otherwise bigoted vendor from discriminating who they sell to?
Why does Intel's Haswell chip allow multiplication to be twice as fast as addition?
Pandas: fill one column with count of # of obs between occurrences in a 2nd column
How does The Fools Guild make its money?
During the Space Shuttle Columbia Disaster of 2003, Why Did The Flight Director Say, "Lock the doors."?
English - Acceptable use of parentheses in an author's name
Why are the inside diameters of some pipe larger than the stated size?
Did WWII Japanese soldiers engage in cannibalism of their enemies?
Does the Shapechange spell allow one to use Innate Spellcasting of the creature they turned into?
How quickly could a country build a tall concrete wall around a city?
How to mark beverage cans in a cooler for a blind person?
Does a code snippet compile? Or does it gets compiled?
Can a character who casts Shapechange and turns into a spellcaster use innate spellcasting to cast spells with a long casting time?
Dropdowns & Chevrons for Right to Left languages
How to write "upright" integrals with automatic sizing
Look mom! I made my own (Base 10) numeral system!
Why did the RAAF procure the F/A-18 despite being purpose-built for carriers?
Sed substitute pattern with command
Replace data between quotes in a fileRegex and piped commands with sedReplace multiline string in filesSed command that would ignore any commented matchIs it possible to pass arguments to a sed script?Change a string with sedsed use from android shellExpand Environment Variable from PIPE (SHELL)Count the number of occurrences of a substring in a stringsubstitute with SED recursively
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
A propo of this question.
Let's say I want to substitute <number with commas> for <number> in string something, "10,000", something.
Number could be 1,000, 10,000... so on.
I was experimenting if it's possible to use a command for the substitution:
echo 'something, "10,000", something' | sed "s/(".*")/$(echo \1 | sed 's/,//')/"
Which doesn't work, nothing happens.
What triggers my question is that if I only use echo
echo 'something, "10,000", something' | sed "s/(".*")/$(echo \1)/"
the parameter \1 is read OK, but it looks like it can't be piped to sed (or grep).
Is this possible in some way?
sed
|
show 1 more comment
A propo of this question.
Let's say I want to substitute <number with commas> for <number> in string something, "10,000", something.
Number could be 1,000, 10,000... so on.
I was experimenting if it's possible to use a command for the substitution:
echo 'something, "10,000", something' | sed "s/(".*")/$(echo \1 | sed 's/,//')/"
Which doesn't work, nothing happens.
What triggers my question is that if I only use echo
echo 'something, "10,000", something' | sed "s/(".*")/$(echo \1)/"
the parameter \1 is read OK, but it looks like it can't be piped to sed (or grep).
Is this possible in some way?
sed
3
The command substitution in the replacement part of yoursedcommand is executed before thesedcommand runs.
– Kusalananda♦
7 hours ago
.. which means it will sed the string '1' and not the actual captured string.
– Jonas Berlin
7 hours ago
@JonasBerlin That was my conclusion, what loses me is thatecho \1outputs the captured pattern, not the string "1"
– guillermo chamorro
7 hours ago
yeah the1gets used in the main sed command replacement part as-is and thus the replacement becomes the captured group's contents.
– Jonas Berlin
7 hours ago
1
I'd suggest that you try running your pipelines withset -xactivated.
– Kusalananda♦
6 hours ago
|
show 1 more comment
A propo of this question.
Let's say I want to substitute <number with commas> for <number> in string something, "10,000", something.
Number could be 1,000, 10,000... so on.
I was experimenting if it's possible to use a command for the substitution:
echo 'something, "10,000", something' | sed "s/(".*")/$(echo \1 | sed 's/,//')/"
Which doesn't work, nothing happens.
What triggers my question is that if I only use echo
echo 'something, "10,000", something' | sed "s/(".*")/$(echo \1)/"
the parameter \1 is read OK, but it looks like it can't be piped to sed (or grep).
Is this possible in some way?
sed
A propo of this question.
Let's say I want to substitute <number with commas> for <number> in string something, "10,000", something.
Number could be 1,000, 10,000... so on.
I was experimenting if it's possible to use a command for the substitution:
echo 'something, "10,000", something' | sed "s/(".*")/$(echo \1 | sed 's/,//')/"
Which doesn't work, nothing happens.
What triggers my question is that if I only use echo
echo 'something, "10,000", something' | sed "s/(".*")/$(echo \1)/"
the parameter \1 is read OK, but it looks like it can't be piped to sed (or grep).
Is this possible in some way?
sed
sed
edited 25 mins ago
mosvy
15.6k2 gold badges18 silver badges51 bronze badges
15.6k2 gold badges18 silver badges51 bronze badges
asked 7 hours ago
guillermo chamorroguillermo chamorro
38412 bronze badges
38412 bronze badges
3
The command substitution in the replacement part of yoursedcommand is executed before thesedcommand runs.
– Kusalananda♦
7 hours ago
.. which means it will sed the string '1' and not the actual captured string.
– Jonas Berlin
7 hours ago
@JonasBerlin That was my conclusion, what loses me is thatecho \1outputs the captured pattern, not the string "1"
– guillermo chamorro
7 hours ago
yeah the1gets used in the main sed command replacement part as-is and thus the replacement becomes the captured group's contents.
– Jonas Berlin
7 hours ago
1
I'd suggest that you try running your pipelines withset -xactivated.
– Kusalananda♦
6 hours ago
|
show 1 more comment
3
The command substitution in the replacement part of yoursedcommand is executed before thesedcommand runs.
– Kusalananda♦
7 hours ago
.. which means it will sed the string '1' and not the actual captured string.
– Jonas Berlin
7 hours ago
@JonasBerlin That was my conclusion, what loses me is thatecho \1outputs the captured pattern, not the string "1"
– guillermo chamorro
7 hours ago
yeah the1gets used in the main sed command replacement part as-is and thus the replacement becomes the captured group's contents.
– Jonas Berlin
7 hours ago
1
I'd suggest that you try running your pipelines withset -xactivated.
– Kusalananda♦
6 hours ago
3
3
The command substitution in the replacement part of your
sed command is executed before the sed command runs.– Kusalananda♦
7 hours ago
The command substitution in the replacement part of your
sed command is executed before the sed command runs.– Kusalananda♦
7 hours ago
.. which means it will sed the string '1' and not the actual captured string.
– Jonas Berlin
7 hours ago
.. which means it will sed the string '1' and not the actual captured string.
– Jonas Berlin
7 hours ago
@JonasBerlin That was my conclusion, what loses me is that
echo \1 outputs the captured pattern, not the string "1"– guillermo chamorro
7 hours ago
@JonasBerlin That was my conclusion, what loses me is that
echo \1 outputs the captured pattern, not the string "1"– guillermo chamorro
7 hours ago
yeah the
1 gets used in the main sed command replacement part as-is and thus the replacement becomes the captured group's contents.– Jonas Berlin
7 hours ago
yeah the
1 gets used in the main sed command replacement part as-is and thus the replacement becomes the captured group's contents.– Jonas Berlin
7 hours ago
1
1
I'd suggest that you try running your pipelines with
set -x activated.– Kusalananda♦
6 hours ago
I'd suggest that you try running your pipelines with
set -x activated.– Kusalananda♦
6 hours ago
|
show 1 more comment
2 Answers
2
active
oldest
votes
The expansion of the command substitution that is part of your sed expression happens before the sed command is executed.
This means that the shell, to execute the command
sed "s/(".*")/$(echo \1 | sed 's/,//')/"
first runs
echo \1 | sed 's/,//'
This outputs 1 since there are no commas in the string outputted by echo.
The shell then inserts this string where the command substitution was, yielding
sed "s/(".*")/1/"
This is also clear if we run the pipeline with tracing enabled in the shell:
$ set -x
$ echo 'something, "10,000", something' | sed "s/(".*")/$(echo \1 | sed 's/,//')/"
+ echo 'something, "10,000", something'
+ echo '1'
+ sed s/,//
+ sed 's/(".*")/1/'
something, "10,000", something
In your second pipeline, the same thing happens (minus the execution of sed 's/,//'):
$ set -x
$ echo 'something, "10,000", something' | sed "s/(".*")/$(echo \1)/"
+ echo 'something, "10,000", something'
+ echo '1'
+ sed 's/(".*")/1/'
something, "10,000", something
In conclusion: It is not possible to call another shell command from within sed using command substitutions to process a substring matched by a regular expression, as the command substitutions are processed before sed is called (in order to resolve what the sed editing script is before calling sed).
sed moreover does not provide any mechanism in its language to call out to another command, like some other editing/processing languages do (e.g. the rudimentary r and w commands in the ed editor allows this, as does awk).
With GNUsedone could do e.g.echo 'something, "10,000", something' | sed 's/.*"([[:digit:]]+),?.*/seq 1/e'. Theecan be used as a flag for thesfunction or as a standalone function
– LL3
2 hours ago
add a comment |
sed 's/([0-9]),([0-9])/12/g'
This works only if there are at least two numbers between two commas, so
123,456,789 -> 123456789
12,34,56,78 -> 12345678
but
1,2,3,4,5,6 -> 12,34,56
sed -e :top -e 's/([[:digit:]]),([[:digit:]])/12/g; ttop'
– Kusalananda♦
3 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
});
}
});
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%2f534772%2fsed-substitute-pattern-with-command%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The expansion of the command substitution that is part of your sed expression happens before the sed command is executed.
This means that the shell, to execute the command
sed "s/(".*")/$(echo \1 | sed 's/,//')/"
first runs
echo \1 | sed 's/,//'
This outputs 1 since there are no commas in the string outputted by echo.
The shell then inserts this string where the command substitution was, yielding
sed "s/(".*")/1/"
This is also clear if we run the pipeline with tracing enabled in the shell:
$ set -x
$ echo 'something, "10,000", something' | sed "s/(".*")/$(echo \1 | sed 's/,//')/"
+ echo 'something, "10,000", something'
+ echo '1'
+ sed s/,//
+ sed 's/(".*")/1/'
something, "10,000", something
In your second pipeline, the same thing happens (minus the execution of sed 's/,//'):
$ set -x
$ echo 'something, "10,000", something' | sed "s/(".*")/$(echo \1)/"
+ echo 'something, "10,000", something'
+ echo '1'
+ sed 's/(".*")/1/'
something, "10,000", something
In conclusion: It is not possible to call another shell command from within sed using command substitutions to process a substring matched by a regular expression, as the command substitutions are processed before sed is called (in order to resolve what the sed editing script is before calling sed).
sed moreover does not provide any mechanism in its language to call out to another command, like some other editing/processing languages do (e.g. the rudimentary r and w commands in the ed editor allows this, as does awk).
With GNUsedone could do e.g.echo 'something, "10,000", something' | sed 's/.*"([[:digit:]]+),?.*/seq 1/e'. Theecan be used as a flag for thesfunction or as a standalone function
– LL3
2 hours ago
add a comment |
The expansion of the command substitution that is part of your sed expression happens before the sed command is executed.
This means that the shell, to execute the command
sed "s/(".*")/$(echo \1 | sed 's/,//')/"
first runs
echo \1 | sed 's/,//'
This outputs 1 since there are no commas in the string outputted by echo.
The shell then inserts this string where the command substitution was, yielding
sed "s/(".*")/1/"
This is also clear if we run the pipeline with tracing enabled in the shell:
$ set -x
$ echo 'something, "10,000", something' | sed "s/(".*")/$(echo \1 | sed 's/,//')/"
+ echo 'something, "10,000", something'
+ echo '1'
+ sed s/,//
+ sed 's/(".*")/1/'
something, "10,000", something
In your second pipeline, the same thing happens (minus the execution of sed 's/,//'):
$ set -x
$ echo 'something, "10,000", something' | sed "s/(".*")/$(echo \1)/"
+ echo 'something, "10,000", something'
+ echo '1'
+ sed 's/(".*")/1/'
something, "10,000", something
In conclusion: It is not possible to call another shell command from within sed using command substitutions to process a substring matched by a regular expression, as the command substitutions are processed before sed is called (in order to resolve what the sed editing script is before calling sed).
sed moreover does not provide any mechanism in its language to call out to another command, like some other editing/processing languages do (e.g. the rudimentary r and w commands in the ed editor allows this, as does awk).
With GNUsedone could do e.g.echo 'something, "10,000", something' | sed 's/.*"([[:digit:]]+),?.*/seq 1/e'. Theecan be used as a flag for thesfunction or as a standalone function
– LL3
2 hours ago
add a comment |
The expansion of the command substitution that is part of your sed expression happens before the sed command is executed.
This means that the shell, to execute the command
sed "s/(".*")/$(echo \1 | sed 's/,//')/"
first runs
echo \1 | sed 's/,//'
This outputs 1 since there are no commas in the string outputted by echo.
The shell then inserts this string where the command substitution was, yielding
sed "s/(".*")/1/"
This is also clear if we run the pipeline with tracing enabled in the shell:
$ set -x
$ echo 'something, "10,000", something' | sed "s/(".*")/$(echo \1 | sed 's/,//')/"
+ echo 'something, "10,000", something'
+ echo '1'
+ sed s/,//
+ sed 's/(".*")/1/'
something, "10,000", something
In your second pipeline, the same thing happens (minus the execution of sed 's/,//'):
$ set -x
$ echo 'something, "10,000", something' | sed "s/(".*")/$(echo \1)/"
+ echo 'something, "10,000", something'
+ echo '1'
+ sed 's/(".*")/1/'
something, "10,000", something
In conclusion: It is not possible to call another shell command from within sed using command substitutions to process a substring matched by a regular expression, as the command substitutions are processed before sed is called (in order to resolve what the sed editing script is before calling sed).
sed moreover does not provide any mechanism in its language to call out to another command, like some other editing/processing languages do (e.g. the rudimentary r and w commands in the ed editor allows this, as does awk).
The expansion of the command substitution that is part of your sed expression happens before the sed command is executed.
This means that the shell, to execute the command
sed "s/(".*")/$(echo \1 | sed 's/,//')/"
first runs
echo \1 | sed 's/,//'
This outputs 1 since there are no commas in the string outputted by echo.
The shell then inserts this string where the command substitution was, yielding
sed "s/(".*")/1/"
This is also clear if we run the pipeline with tracing enabled in the shell:
$ set -x
$ echo 'something, "10,000", something' | sed "s/(".*")/$(echo \1 | sed 's/,//')/"
+ echo 'something, "10,000", something'
+ echo '1'
+ sed s/,//
+ sed 's/(".*")/1/'
something, "10,000", something
In your second pipeline, the same thing happens (minus the execution of sed 's/,//'):
$ set -x
$ echo 'something, "10,000", something' | sed "s/(".*")/$(echo \1)/"
+ echo 'something, "10,000", something'
+ echo '1'
+ sed 's/(".*")/1/'
something, "10,000", something
In conclusion: It is not possible to call another shell command from within sed using command substitutions to process a substring matched by a regular expression, as the command substitutions are processed before sed is called (in order to resolve what the sed editing script is before calling sed).
sed moreover does not provide any mechanism in its language to call out to another command, like some other editing/processing languages do (e.g. the rudimentary r and w commands in the ed editor allows this, as does awk).
edited 3 hours ago
answered 3 hours ago
Kusalananda♦Kusalananda
158k18 gold badges313 silver badges499 bronze badges
158k18 gold badges313 silver badges499 bronze badges
With GNUsedone could do e.g.echo 'something, "10,000", something' | sed 's/.*"([[:digit:]]+),?.*/seq 1/e'. Theecan be used as a flag for thesfunction or as a standalone function
– LL3
2 hours ago
add a comment |
With GNUsedone could do e.g.echo 'something, "10,000", something' | sed 's/.*"([[:digit:]]+),?.*/seq 1/e'. Theecan be used as a flag for thesfunction or as a standalone function
– LL3
2 hours ago
With GNU
sed one could do e.g. echo 'something, "10,000", something' | sed 's/.*"([[:digit:]]+),?.*/seq 1/e'. The e can be used as a flag for the s function or as a standalone function– LL3
2 hours ago
With GNU
sed one could do e.g. echo 'something, "10,000", something' | sed 's/.*"([[:digit:]]+),?.*/seq 1/e'. The e can be used as a flag for the s function or as a standalone function– LL3
2 hours ago
add a comment |
sed 's/([0-9]),([0-9])/12/g'
This works only if there are at least two numbers between two commas, so
123,456,789 -> 123456789
12,34,56,78 -> 12345678
but
1,2,3,4,5,6 -> 12,34,56
sed -e :top -e 's/([[:digit:]]),([[:digit:]])/12/g; ttop'
– Kusalananda♦
3 hours ago
add a comment |
sed 's/([0-9]),([0-9])/12/g'
This works only if there are at least two numbers between two commas, so
123,456,789 -> 123456789
12,34,56,78 -> 12345678
but
1,2,3,4,5,6 -> 12,34,56
sed -e :top -e 's/([[:digit:]]),([[:digit:]])/12/g; ttop'
– Kusalananda♦
3 hours ago
add a comment |
sed 's/([0-9]),([0-9])/12/g'
This works only if there are at least two numbers between two commas, so
123,456,789 -> 123456789
12,34,56,78 -> 12345678
but
1,2,3,4,5,6 -> 12,34,56
sed 's/([0-9]),([0-9])/12/g'
This works only if there are at least two numbers between two commas, so
123,456,789 -> 123456789
12,34,56,78 -> 12345678
but
1,2,3,4,5,6 -> 12,34,56
answered 7 hours ago
Jonas BerlinJonas Berlin
6086 silver badges10 bronze badges
6086 silver badges10 bronze badges
sed -e :top -e 's/([[:digit:]]),([[:digit:]])/12/g; ttop'
– Kusalananda♦
3 hours ago
add a comment |
sed -e :top -e 's/([[:digit:]]),([[:digit:]])/12/g; ttop'
– Kusalananda♦
3 hours ago
sed -e :top -e 's/([[:digit:]]),([[:digit:]])/12/g; ttop'– Kusalananda♦
3 hours ago
sed -e :top -e 's/([[:digit:]]),([[:digit:]])/12/g; ttop'– Kusalananda♦
3 hours ago
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%2f534772%2fsed-substitute-pattern-with-command%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
3
The command substitution in the replacement part of your
sedcommand is executed before thesedcommand runs.– Kusalananda♦
7 hours ago
.. which means it will sed the string '1' and not the actual captured string.
– Jonas Berlin
7 hours ago
@JonasBerlin That was my conclusion, what loses me is that
echo \1outputs the captured pattern, not the string "1"– guillermo chamorro
7 hours ago
yeah the
1gets used in the main sed command replacement part as-is and thus the replacement becomes the captured group's contents.– Jonas Berlin
7 hours ago
1
I'd suggest that you try running your pipelines with
set -xactivated.– Kusalananda♦
6 hours ago