Replace value with variable length between double quotesReplace value within single quotes in a line that contains certain word using sed commandDouble quote value assignments stored in a CSV?sed: replace strings with variable contentFind and replace with awkReplace the comma with vertical bar |, except when inside double quotes, and remove double quotesREGEX search & replace with sed or other commandAdd double quotes to 1st occurrence of = after delimiter | in a filesed: Change a variable-length numerical value for an indeterminate string
Justifying the use of directed energy weapons
Are modern clipless shoes and pedals that much better than toe clips and straps?
Why different interest rates for checking and savings?
Why isn't "I've" a proper response?
What is the hex versus octal timeline?
Architectural feasibility of a tiered circular stone keep
In an emergency, how do I find and share my position?
What magic extends life or grants immortality?
Dealing with an extrovert co-worker
What's the terminology for this alternative minimization algorithm?
Can you feel passing through the sound barrier in an F-16?
What is the difference between true neutral and unaligned?
Is for(( ... )) ... ; a valid shell syntax? In which shells?
Why do all fields in a QFT transform like *irreducible* representations of some group?
How is the list of apps allowed to install another apps populated?
Why were movies shot on film shot at 24 frames per second?
Irish Snap: Variant Rules
Mathematical uses of string theory
Are there account age or level requirements for obtaining special research?
Non-visual Computers - thoughts?
Does travel insurance for short flight delays exist?
Is "The life is beautiful" incorrect or just very non-idiomatic?
What is the best option for High availability on a data warehouse?
See details of old sessions
Replace value with variable length between double quotes
Replace value within single quotes in a line that contains certain word using sed commandDouble quote value assignments stored in a CSV?sed: replace strings with variable contentFind and replace with awkReplace the comma with vertical bar |, except when inside double quotes, and remove double quotesREGEX search & replace with sed or other commandAdd double quotes to 1st occurrence of = after delimiter | in a filesed: Change a variable-length numerical value for an indeterminate string
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a string like:
schedule="0.25"
and I want to replace the 0.25
lets say with 0.50
I could achieve this with:
sed 's/"...."/"0.50"/g'
the problem is I don't know the value between the double quotes, and therefore I do not know the length. It could be any value, but it will be always preceded by schedule=
.
text-processing awk sed
add a comment |
I have a string like:
schedule="0.25"
and I want to replace the 0.25
lets say with 0.50
I could achieve this with:
sed 's/"...."/"0.50"/g'
the problem is I don't know the value between the double quotes, and therefore I do not know the length. It could be any value, but it will be always preceded by schedule=
.
text-processing awk sed
It depends... you might try something likesed 's/"[^"]*"/"0.50"/g'
(this matches 0 or more characters that aren't '"' between '"' for replacement).
– vonbrand
Aug 11 at 20:37
@vonbrand this is pretty much what steeldriver suggested in his answer... (apart from the escapes).
– nath
Aug 11 at 22:31
add a comment |
I have a string like:
schedule="0.25"
and I want to replace the 0.25
lets say with 0.50
I could achieve this with:
sed 's/"...."/"0.50"/g'
the problem is I don't know the value between the double quotes, and therefore I do not know the length. It could be any value, but it will be always preceded by schedule=
.
text-processing awk sed
I have a string like:
schedule="0.25"
and I want to replace the 0.25
lets say with 0.50
I could achieve this with:
sed 's/"...."/"0.50"/g'
the problem is I don't know the value between the double quotes, and therefore I do not know the length. It could be any value, but it will be always preceded by schedule=
.
text-processing awk sed
text-processing awk sed
edited Aug 11 at 11:20
miracle173
4142 silver badges10 bronze badges
4142 silver badges10 bronze badges
asked Aug 10 at 21:24
nathnath
1,2761 gold badge13 silver badges37 bronze badges
1,2761 gold badge13 silver badges37 bronze badges
It depends... you might try something likesed 's/"[^"]*"/"0.50"/g'
(this matches 0 or more characters that aren't '"' between '"' for replacement).
– vonbrand
Aug 11 at 20:37
@vonbrand this is pretty much what steeldriver suggested in his answer... (apart from the escapes).
– nath
Aug 11 at 22:31
add a comment |
It depends... you might try something likesed 's/"[^"]*"/"0.50"/g'
(this matches 0 or more characters that aren't '"' between '"' for replacement).
– vonbrand
Aug 11 at 20:37
@vonbrand this is pretty much what steeldriver suggested in his answer... (apart from the escapes).
– nath
Aug 11 at 22:31
It depends... you might try something like
sed 's/"[^"]*"/"0.50"/g'
(this matches 0 or more characters that aren't '"' between '"' for replacement).– vonbrand
Aug 11 at 20:37
It depends... you might try something like
sed 's/"[^"]*"/"0.50"/g'
(this matches 0 or more characters that aren't '"' between '"' for replacement).– vonbrand
Aug 11 at 20:37
@vonbrand this is pretty much what steeldriver suggested in his answer... (apart from the escapes).
– nath
Aug 11 at 22:31
@vonbrand this is pretty much what steeldriver suggested in his answer... (apart from the escapes).
– nath
Aug 11 at 22:31
add a comment |
3 Answers
3
active
oldest
votes
You can use [^"]*
to match a sequence of zero or more non-"
characters. So
$ echo 'schedule="0.25"' | sed 's/"[^"]*"/"0.5"/'
schedule="0.5"
add a comment |
It looks like you just want to replace whatever comes after the equal sign with "0.50"
. This is easily done with awk
:
$ echo 'schedule="0.25"' | awk -F = 'BEGIN OFS=FS $1 == "schedule" $2 = ""0.50"" print '
schedule="0.50"
The awk
program looks specifically for a record whose 1st =
-delimited field is schedule
. When found, it replaces the second field with "0.50"
.
If 0.50
is variable, you may pass this value into the awk
script like this:
$ newval=0.222
$ echo 'schedule="0.25"' | awk -v nv="$newval" -F = 'BEGIN OFS=FS $1 == "schedule" $2 = sprintf(""%s"", nv) print '
schedule="0.222"
And obviously, if schedule
may vary as well,
$ newval=0.222
$ variable=schedule
$ echo 'schedule="0.25"' | awk -v v="$variable" -v nv="$newval" -F = 'BEGIN OFS=FS $1 == v $2 = sprintf(""%s"", nv) print '
schedule="0.222"
With sed
, you could just ignore whatever comes after the =
and replace it with whatever you wish:
$ echo 'schedule="0.25"' | sed 's/=.*/="0.50"/'
schedule="0.50"
To make sure we replace only the value for schedule
, and not some other setting's value,
$ echo 'schedule="0.25"' | sed '/^schedule=/s/=.*/="0.50"/'
schedule="0.50"
This puts a condition on our substitution; The line must have the string schedule=
at the start of it for it to trigger.
We could even insert shell variables into our sed
expression, but be aware that we must make sure that the values of these variables do not break the sed
syntax (by containing characters that affect the matching or the delimiting of the commands):
$ newval=0.222
$ variable=schedule
$ echo 'schedule="0.25"' | sed "/^$variable=/s/=.*/="$newval"/"
schedule="0.222"
As opposed to the awk
example, here we rely on the shell to inject the values of our variables into the sed
editing script. This would normally be classified as a code injection vulnerability in the cases where the values of the variables are not under our control (like when we are reading them from a user supplied source).
add a comment |
If you are invoking sed/awk from a bash script/terminal, then you could avoid using the external tools and take advantage of bash's own string processing capabilities:
$ a='schedule="0.25"'
$ echo "$a"
schedule="0.25"
$ b="$a/"*/"0.50""
$ echo "$b"
schedule="0.50"
1
Shorter:b="$a/"*/"0.50""
. Note that you're using filename globbing patterns here, not regular expressions, so your*
just matches the rest of the string anyway. A standard compliant variation would beb="$a%%=*"'="0.50"'
.
– Kusalananda♦
Aug 11 at 8:18
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%2f534929%2freplace-value-with-variable-length-between-double-quotes%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
You can use [^"]*
to match a sequence of zero or more non-"
characters. So
$ echo 'schedule="0.25"' | sed 's/"[^"]*"/"0.5"/'
schedule="0.5"
add a comment |
You can use [^"]*
to match a sequence of zero or more non-"
characters. So
$ echo 'schedule="0.25"' | sed 's/"[^"]*"/"0.5"/'
schedule="0.5"
add a comment |
You can use [^"]*
to match a sequence of zero or more non-"
characters. So
$ echo 'schedule="0.25"' | sed 's/"[^"]*"/"0.5"/'
schedule="0.5"
You can use [^"]*
to match a sequence of zero or more non-"
characters. So
$ echo 'schedule="0.25"' | sed 's/"[^"]*"/"0.5"/'
schedule="0.5"
answered Aug 10 at 21:28
steeldriversteeldriver
42.4k5 gold badges56 silver badges94 bronze badges
42.4k5 gold badges56 silver badges94 bronze badges
add a comment |
add a comment |
It looks like you just want to replace whatever comes after the equal sign with "0.50"
. This is easily done with awk
:
$ echo 'schedule="0.25"' | awk -F = 'BEGIN OFS=FS $1 == "schedule" $2 = ""0.50"" print '
schedule="0.50"
The awk
program looks specifically for a record whose 1st =
-delimited field is schedule
. When found, it replaces the second field with "0.50"
.
If 0.50
is variable, you may pass this value into the awk
script like this:
$ newval=0.222
$ echo 'schedule="0.25"' | awk -v nv="$newval" -F = 'BEGIN OFS=FS $1 == "schedule" $2 = sprintf(""%s"", nv) print '
schedule="0.222"
And obviously, if schedule
may vary as well,
$ newval=0.222
$ variable=schedule
$ echo 'schedule="0.25"' | awk -v v="$variable" -v nv="$newval" -F = 'BEGIN OFS=FS $1 == v $2 = sprintf(""%s"", nv) print '
schedule="0.222"
With sed
, you could just ignore whatever comes after the =
and replace it with whatever you wish:
$ echo 'schedule="0.25"' | sed 's/=.*/="0.50"/'
schedule="0.50"
To make sure we replace only the value for schedule
, and not some other setting's value,
$ echo 'schedule="0.25"' | sed '/^schedule=/s/=.*/="0.50"/'
schedule="0.50"
This puts a condition on our substitution; The line must have the string schedule=
at the start of it for it to trigger.
We could even insert shell variables into our sed
expression, but be aware that we must make sure that the values of these variables do not break the sed
syntax (by containing characters that affect the matching or the delimiting of the commands):
$ newval=0.222
$ variable=schedule
$ echo 'schedule="0.25"' | sed "/^$variable=/s/=.*/="$newval"/"
schedule="0.222"
As opposed to the awk
example, here we rely on the shell to inject the values of our variables into the sed
editing script. This would normally be classified as a code injection vulnerability in the cases where the values of the variables are not under our control (like when we are reading them from a user supplied source).
add a comment |
It looks like you just want to replace whatever comes after the equal sign with "0.50"
. This is easily done with awk
:
$ echo 'schedule="0.25"' | awk -F = 'BEGIN OFS=FS $1 == "schedule" $2 = ""0.50"" print '
schedule="0.50"
The awk
program looks specifically for a record whose 1st =
-delimited field is schedule
. When found, it replaces the second field with "0.50"
.
If 0.50
is variable, you may pass this value into the awk
script like this:
$ newval=0.222
$ echo 'schedule="0.25"' | awk -v nv="$newval" -F = 'BEGIN OFS=FS $1 == "schedule" $2 = sprintf(""%s"", nv) print '
schedule="0.222"
And obviously, if schedule
may vary as well,
$ newval=0.222
$ variable=schedule
$ echo 'schedule="0.25"' | awk -v v="$variable" -v nv="$newval" -F = 'BEGIN OFS=FS $1 == v $2 = sprintf(""%s"", nv) print '
schedule="0.222"
With sed
, you could just ignore whatever comes after the =
and replace it with whatever you wish:
$ echo 'schedule="0.25"' | sed 's/=.*/="0.50"/'
schedule="0.50"
To make sure we replace only the value for schedule
, and not some other setting's value,
$ echo 'schedule="0.25"' | sed '/^schedule=/s/=.*/="0.50"/'
schedule="0.50"
This puts a condition on our substitution; The line must have the string schedule=
at the start of it for it to trigger.
We could even insert shell variables into our sed
expression, but be aware that we must make sure that the values of these variables do not break the sed
syntax (by containing characters that affect the matching or the delimiting of the commands):
$ newval=0.222
$ variable=schedule
$ echo 'schedule="0.25"' | sed "/^$variable=/s/=.*/="$newval"/"
schedule="0.222"
As opposed to the awk
example, here we rely on the shell to inject the values of our variables into the sed
editing script. This would normally be classified as a code injection vulnerability in the cases where the values of the variables are not under our control (like when we are reading them from a user supplied source).
add a comment |
It looks like you just want to replace whatever comes after the equal sign with "0.50"
. This is easily done with awk
:
$ echo 'schedule="0.25"' | awk -F = 'BEGIN OFS=FS $1 == "schedule" $2 = ""0.50"" print '
schedule="0.50"
The awk
program looks specifically for a record whose 1st =
-delimited field is schedule
. When found, it replaces the second field with "0.50"
.
If 0.50
is variable, you may pass this value into the awk
script like this:
$ newval=0.222
$ echo 'schedule="0.25"' | awk -v nv="$newval" -F = 'BEGIN OFS=FS $1 == "schedule" $2 = sprintf(""%s"", nv) print '
schedule="0.222"
And obviously, if schedule
may vary as well,
$ newval=0.222
$ variable=schedule
$ echo 'schedule="0.25"' | awk -v v="$variable" -v nv="$newval" -F = 'BEGIN OFS=FS $1 == v $2 = sprintf(""%s"", nv) print '
schedule="0.222"
With sed
, you could just ignore whatever comes after the =
and replace it with whatever you wish:
$ echo 'schedule="0.25"' | sed 's/=.*/="0.50"/'
schedule="0.50"
To make sure we replace only the value for schedule
, and not some other setting's value,
$ echo 'schedule="0.25"' | sed '/^schedule=/s/=.*/="0.50"/'
schedule="0.50"
This puts a condition on our substitution; The line must have the string schedule=
at the start of it for it to trigger.
We could even insert shell variables into our sed
expression, but be aware that we must make sure that the values of these variables do not break the sed
syntax (by containing characters that affect the matching or the delimiting of the commands):
$ newval=0.222
$ variable=schedule
$ echo 'schedule="0.25"' | sed "/^$variable=/s/=.*/="$newval"/"
schedule="0.222"
As opposed to the awk
example, here we rely on the shell to inject the values of our variables into the sed
editing script. This would normally be classified as a code injection vulnerability in the cases where the values of the variables are not under our control (like when we are reading them from a user supplied source).
It looks like you just want to replace whatever comes after the equal sign with "0.50"
. This is easily done with awk
:
$ echo 'schedule="0.25"' | awk -F = 'BEGIN OFS=FS $1 == "schedule" $2 = ""0.50"" print '
schedule="0.50"
The awk
program looks specifically for a record whose 1st =
-delimited field is schedule
. When found, it replaces the second field with "0.50"
.
If 0.50
is variable, you may pass this value into the awk
script like this:
$ newval=0.222
$ echo 'schedule="0.25"' | awk -v nv="$newval" -F = 'BEGIN OFS=FS $1 == "schedule" $2 = sprintf(""%s"", nv) print '
schedule="0.222"
And obviously, if schedule
may vary as well,
$ newval=0.222
$ variable=schedule
$ echo 'schedule="0.25"' | awk -v v="$variable" -v nv="$newval" -F = 'BEGIN OFS=FS $1 == v $2 = sprintf(""%s"", nv) print '
schedule="0.222"
With sed
, you could just ignore whatever comes after the =
and replace it with whatever you wish:
$ echo 'schedule="0.25"' | sed 's/=.*/="0.50"/'
schedule="0.50"
To make sure we replace only the value for schedule
, and not some other setting's value,
$ echo 'schedule="0.25"' | sed '/^schedule=/s/=.*/="0.50"/'
schedule="0.50"
This puts a condition on our substitution; The line must have the string schedule=
at the start of it for it to trigger.
We could even insert shell variables into our sed
expression, but be aware that we must make sure that the values of these variables do not break the sed
syntax (by containing characters that affect the matching or the delimiting of the commands):
$ newval=0.222
$ variable=schedule
$ echo 'schedule="0.25"' | sed "/^$variable=/s/=.*/="$newval"/"
schedule="0.222"
As opposed to the awk
example, here we rely on the shell to inject the values of our variables into the sed
editing script. This would normally be classified as a code injection vulnerability in the cases where the values of the variables are not under our control (like when we are reading them from a user supplied source).
edited Aug 11 at 7:17
answered Aug 10 at 21:41
Kusalananda♦Kusalananda
160k18 gold badges318 silver badges504 bronze badges
160k18 gold badges318 silver badges504 bronze badges
add a comment |
add a comment |
If you are invoking sed/awk from a bash script/terminal, then you could avoid using the external tools and take advantage of bash's own string processing capabilities:
$ a='schedule="0.25"'
$ echo "$a"
schedule="0.25"
$ b="$a/"*/"0.50""
$ echo "$b"
schedule="0.50"
1
Shorter:b="$a/"*/"0.50""
. Note that you're using filename globbing patterns here, not regular expressions, so your*
just matches the rest of the string anyway. A standard compliant variation would beb="$a%%=*"'="0.50"'
.
– Kusalananda♦
Aug 11 at 8:18
add a comment |
If you are invoking sed/awk from a bash script/terminal, then you could avoid using the external tools and take advantage of bash's own string processing capabilities:
$ a='schedule="0.25"'
$ echo "$a"
schedule="0.25"
$ b="$a/"*/"0.50""
$ echo "$b"
schedule="0.50"
1
Shorter:b="$a/"*/"0.50""
. Note that you're using filename globbing patterns here, not regular expressions, so your*
just matches the rest of the string anyway. A standard compliant variation would beb="$a%%=*"'="0.50"'
.
– Kusalananda♦
Aug 11 at 8:18
add a comment |
If you are invoking sed/awk from a bash script/terminal, then you could avoid using the external tools and take advantage of bash's own string processing capabilities:
$ a='schedule="0.25"'
$ echo "$a"
schedule="0.25"
$ b="$a/"*/"0.50""
$ echo "$b"
schedule="0.50"
If you are invoking sed/awk from a bash script/terminal, then you could avoid using the external tools and take advantage of bash's own string processing capabilities:
$ a='schedule="0.25"'
$ echo "$a"
schedule="0.25"
$ b="$a/"*/"0.50""
$ echo "$b"
schedule="0.50"
edited Aug 11 at 8:22
answered Aug 11 at 8:09
user000001user000001
1,1528 silver badges15 bronze badges
1,1528 silver badges15 bronze badges
1
Shorter:b="$a/"*/"0.50""
. Note that you're using filename globbing patterns here, not regular expressions, so your*
just matches the rest of the string anyway. A standard compliant variation would beb="$a%%=*"'="0.50"'
.
– Kusalananda♦
Aug 11 at 8:18
add a comment |
1
Shorter:b="$a/"*/"0.50""
. Note that you're using filename globbing patterns here, not regular expressions, so your*
just matches the rest of the string anyway. A standard compliant variation would beb="$a%%=*"'="0.50"'
.
– Kusalananda♦
Aug 11 at 8:18
1
1
Shorter:
b="$a/"*/"0.50""
. Note that you're using filename globbing patterns here, not regular expressions, so your *
just matches the rest of the string anyway. A standard compliant variation would be b="$a%%=*"'="0.50"'
.– Kusalananda♦
Aug 11 at 8:18
Shorter:
b="$a/"*/"0.50""
. Note that you're using filename globbing patterns here, not regular expressions, so your *
just matches the rest of the string anyway. A standard compliant variation would be b="$a%%=*"'="0.50"'
.– Kusalananda♦
Aug 11 at 8:18
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%2f534929%2freplace-value-with-variable-length-between-double-quotes%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
It depends... you might try something like
sed 's/"[^"]*"/"0.50"/g'
(this matches 0 or more characters that aren't '"' between '"' for replacement).– vonbrand
Aug 11 at 20:37
@vonbrand this is pretty much what steeldriver suggested in his answer... (apart from the escapes).
– nath
Aug 11 at 22:31