sed command to replace and write

Clash Royale CLAN TAG#URR8PPP
up vote
-1
down vote
favorite
I am trying to replace the following string with another one from a file which contains few lines.
"schema" : "AAAAA",
TO
"schema" : "BBBBB",
Currently, I am doing the following command to get the corresponding line with "schema"
currentSchema=cat test.json | grep schema | awk 'print $3'
It will give me the value "AAAAA",
I would like to replace AAAAA with BBBBB
I used the following command
sed -e 's/$currentSchema/BBBBB/p' test.json
However, it is not able to replace in the same file.
linux sed string json
New contributor
user3613331 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
-1
down vote
favorite
I am trying to replace the following string with another one from a file which contains few lines.
"schema" : "AAAAA",
TO
"schema" : "BBBBB",
Currently, I am doing the following command to get the corresponding line with "schema"
currentSchema=cat test.json | grep schema | awk 'print $3'
It will give me the value "AAAAA",
I would like to replace AAAAA with BBBBB
I used the following command
sed -e 's/$currentSchema/BBBBB/p' test.json
However, it is not able to replace in the same file.
linux sed string json
New contributor
user3613331 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Add an-iswitch to the sed command to edit the file in place.
â Doug O'Neal
1 hour ago
1
Consider usingjqinstead to perform operations on JSON files. It's a better tool, since it understands the actual underlying format, so it's able to do a better job on it than awk and sed.
â Filipe Brandenburger
1 hour ago
really? how jq can replace the strings?
â user3613331
1 hour ago
add a comment |Â
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I am trying to replace the following string with another one from a file which contains few lines.
"schema" : "AAAAA",
TO
"schema" : "BBBBB",
Currently, I am doing the following command to get the corresponding line with "schema"
currentSchema=cat test.json | grep schema | awk 'print $3'
It will give me the value "AAAAA",
I would like to replace AAAAA with BBBBB
I used the following command
sed -e 's/$currentSchema/BBBBB/p' test.json
However, it is not able to replace in the same file.
linux sed string json
New contributor
user3613331 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I am trying to replace the following string with another one from a file which contains few lines.
"schema" : "AAAAA",
TO
"schema" : "BBBBB",
Currently, I am doing the following command to get the corresponding line with "schema"
currentSchema=cat test.json | grep schema | awk 'print $3'
It will give me the value "AAAAA",
I would like to replace AAAAA with BBBBB
I used the following command
sed -e 's/$currentSchema/BBBBB/p' test.json
However, it is not able to replace in the same file.
linux sed string json
linux sed string json
New contributor
user3613331 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
user3613331 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 2 mins ago
Rui F Ribeiro
37.1k1274118
37.1k1274118
New contributor
user3613331 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 1 hour ago
user3613331
1
1
New contributor
user3613331 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
user3613331 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
user3613331 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Add an-iswitch to the sed command to edit the file in place.
â Doug O'Neal
1 hour ago
1
Consider usingjqinstead to perform operations on JSON files. It's a better tool, since it understands the actual underlying format, so it's able to do a better job on it than awk and sed.
â Filipe Brandenburger
1 hour ago
really? how jq can replace the strings?
â user3613331
1 hour ago
add a comment |Â
Add an-iswitch to the sed command to edit the file in place.
â Doug O'Neal
1 hour ago
1
Consider usingjqinstead to perform operations on JSON files. It's a better tool, since it understands the actual underlying format, so it's able to do a better job on it than awk and sed.
â Filipe Brandenburger
1 hour ago
really? how jq can replace the strings?
â user3613331
1 hour ago
Add an
-i switch to the sed command to edit the file in place.â Doug O'Neal
1 hour ago
Add an
-i switch to the sed command to edit the file in place.â Doug O'Neal
1 hour ago
1
1
Consider using
jq instead to perform operations on JSON files. It's a better tool, since it understands the actual underlying format, so it's able to do a better job on it than awk and sed.â Filipe Brandenburger
1 hour ago
Consider using
jq instead to perform operations on JSON files. It's a better tool, since it understands the actual underlying format, so it's able to do a better job on it than awk and sed.â Filipe Brandenburger
1 hour ago
really? how jq can replace the strings?
â user3613331
1 hour ago
really? how jq can replace the strings?
â user3613331
1 hour ago
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
1
down vote
Example JSON file:
[
"host": "myhost",
"schema": "AAAAA",
"lunch": "sandwich"
,
"host": "myotherhost",
"schema": "QQQQQ",
"lunch": "pizza"
]
We'd like to replace each schema that is AAAAA with BBBBB. We can do this with jq:
$ jq 'map(if .schema == "AAAAA" then .schema = "BBBBB" else . end)' file.json
[
"host": "myhost",
"schema": "BBBBB",
"lunch": "sandwhich"
,
"host": "myotherhost",
"schema": "QQQQQ",
"lunch": "pizza"
]
If it doesn't matter what the old schema was:
$ jq 'map(.schema = "BBBBB")' file.json
[
"host": "myhost",
"schema": "BBBBB",
"lunch": "sandwhich"
,
"host": "myotherhost",
"schema": "BBBBB",
"lunch": "pizza"
]
Since jq is a proper JSON parser, this would work even if the file was presented in a more compact form, for example as the single line
["host":"myhost","schema":"AAAAA","lunch":"sandwhich","host":"myotherhost","schema":"QQQQQ","lunch":"pizza"]
thanks for the guidance.. however, in my case "schema" can be anywhere in the tree. and it's value will be anything.. There will be only one "schema" actually.
â user3613331
37 mins ago
1
@user3613331 Is the data structure not defined so that theschemais always in the same place all the time? It may occur anywhere in the file, sure, but it must always be in the same logical position relative to its parent objects, or the program that uses the data would not be able to find it.
â Kusalananda
22 mins ago
schema will not be in the same place in the entire json file. So I will need to find the corresponding element and have to replace.
â user3613331
21 mins ago
i need to find a way to locate the schema, and if it exists.. For example, .rules.behaviors.options | select(.name = "schema") then replace only that
â user3613331
17 mins ago
Here is the exact file I am talking about jqplay.org/s/x7RirUrfCi
â user3613331
3 mins ago
 |Â
show 1 more comment
up vote
0
down vote
You don't need grep, awk, and sed for this; awk can do all the task (untested):
awk '/schema/ $3 = "BBBBB" 1' test.json > /tmp/$$ && mv /tmp/$$ test.json
Please try and report back.
1
That will change the entire.jsonfile to only the newly-changed line, discarding the rest of the contents.
â DopeGhoti
1 hour ago
@DopeGhoti: Not sure I understand?
â RudiC
1 hour ago
awk '/schema/ $3 = "BBBBB" 1' rex.json > /tmp/$$ && mv /tmp/$$ yy.json But yy.json contains the same AAAAA itself.. did not change. Where rex.json contains "AAAAA-AAAAA-AAAAA-AAAAA-AAAAA",
â user3613331
1 hour ago
oops... please excuse.. let me update the contents in rex.json
â user3613331
1 hour ago
currently, rex.json contains "schema" : "AAAAA-AAAAA-AAAAA-AAAAA-AAAAA", I tried the following command awk '/schema/ $3 = "BBBBB" 1' rex.json > /tmp/$$ && mv /tmp/$$ yy.json Now yy.json has "schema" : BBBBB Almost there... I need to keep double-quotes there itself along with comma. Basically the entire line should be same.
â user3613331
1 hour ago
 |Â
show 2 more comments
up vote
0
down vote
A couple of optimizations, not least of which is eschewing a "useless use of cat". You don't need to cat file | grep pattern; you can just grep pattern file. And since you're grepping into awk, that can be simplified too:
currentSchema="$( cat test.json | grep schema | awk 'print $3' )"
becomes
currentSchema="$( awk '/schema/ print $3' test.json )"
Now, for your sed script. You are using Strong Quotes (') around your sed command, which means your shell variables will not be parsed. You need to use Weak Quotes (") for this to work:
currentSchema="$( awk '/schema/ print $3' test.json )"
sed --in-place "s/$currentSchema/BBBBB/" test.json
Thanks for the optimization steps! I tried the second command but it is giving the following error sed --in-place "s/$currentSchema/BBBBB" rex.json
â user3613331
1 hour ago
sed: -e expression #1, char 40: unterminated `s' command
â user3613331
1 hour ago
You need a final/after the replacement.s/new/old/.
â DopeGhoti
1 hour ago
rex.json contains "schema" : "AAAAA-AAAAA-AAAAA-AAAAA-AAAAA", currentSchema="$( awk '/schema/ print $3' rex.json )" sed --in-place 's/$currentSchema/BBBBB-BBBBB-BBBBB-BBBBB-BBBBB/' rex.json But nothing has been changed in rex.json file
â user3613331
1 hour ago
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Example JSON file:
[
"host": "myhost",
"schema": "AAAAA",
"lunch": "sandwich"
,
"host": "myotherhost",
"schema": "QQQQQ",
"lunch": "pizza"
]
We'd like to replace each schema that is AAAAA with BBBBB. We can do this with jq:
$ jq 'map(if .schema == "AAAAA" then .schema = "BBBBB" else . end)' file.json
[
"host": "myhost",
"schema": "BBBBB",
"lunch": "sandwhich"
,
"host": "myotherhost",
"schema": "QQQQQ",
"lunch": "pizza"
]
If it doesn't matter what the old schema was:
$ jq 'map(.schema = "BBBBB")' file.json
[
"host": "myhost",
"schema": "BBBBB",
"lunch": "sandwhich"
,
"host": "myotherhost",
"schema": "BBBBB",
"lunch": "pizza"
]
Since jq is a proper JSON parser, this would work even if the file was presented in a more compact form, for example as the single line
["host":"myhost","schema":"AAAAA","lunch":"sandwhich","host":"myotherhost","schema":"QQQQQ","lunch":"pizza"]
thanks for the guidance.. however, in my case "schema" can be anywhere in the tree. and it's value will be anything.. There will be only one "schema" actually.
â user3613331
37 mins ago
1
@user3613331 Is the data structure not defined so that theschemais always in the same place all the time? It may occur anywhere in the file, sure, but it must always be in the same logical position relative to its parent objects, or the program that uses the data would not be able to find it.
â Kusalananda
22 mins ago
schema will not be in the same place in the entire json file. So I will need to find the corresponding element and have to replace.
â user3613331
21 mins ago
i need to find a way to locate the schema, and if it exists.. For example, .rules.behaviors.options | select(.name = "schema") then replace only that
â user3613331
17 mins ago
Here is the exact file I am talking about jqplay.org/s/x7RirUrfCi
â user3613331
3 mins ago
 |Â
show 1 more comment
up vote
1
down vote
Example JSON file:
[
"host": "myhost",
"schema": "AAAAA",
"lunch": "sandwich"
,
"host": "myotherhost",
"schema": "QQQQQ",
"lunch": "pizza"
]
We'd like to replace each schema that is AAAAA with BBBBB. We can do this with jq:
$ jq 'map(if .schema == "AAAAA" then .schema = "BBBBB" else . end)' file.json
[
"host": "myhost",
"schema": "BBBBB",
"lunch": "sandwhich"
,
"host": "myotherhost",
"schema": "QQQQQ",
"lunch": "pizza"
]
If it doesn't matter what the old schema was:
$ jq 'map(.schema = "BBBBB")' file.json
[
"host": "myhost",
"schema": "BBBBB",
"lunch": "sandwhich"
,
"host": "myotherhost",
"schema": "BBBBB",
"lunch": "pizza"
]
Since jq is a proper JSON parser, this would work even if the file was presented in a more compact form, for example as the single line
["host":"myhost","schema":"AAAAA","lunch":"sandwhich","host":"myotherhost","schema":"QQQQQ","lunch":"pizza"]
thanks for the guidance.. however, in my case "schema" can be anywhere in the tree. and it's value will be anything.. There will be only one "schema" actually.
â user3613331
37 mins ago
1
@user3613331 Is the data structure not defined so that theschemais always in the same place all the time? It may occur anywhere in the file, sure, but it must always be in the same logical position relative to its parent objects, or the program that uses the data would not be able to find it.
â Kusalananda
22 mins ago
schema will not be in the same place in the entire json file. So I will need to find the corresponding element and have to replace.
â user3613331
21 mins ago
i need to find a way to locate the schema, and if it exists.. For example, .rules.behaviors.options | select(.name = "schema") then replace only that
â user3613331
17 mins ago
Here is the exact file I am talking about jqplay.org/s/x7RirUrfCi
â user3613331
3 mins ago
 |Â
show 1 more comment
up vote
1
down vote
up vote
1
down vote
Example JSON file:
[
"host": "myhost",
"schema": "AAAAA",
"lunch": "sandwich"
,
"host": "myotherhost",
"schema": "QQQQQ",
"lunch": "pizza"
]
We'd like to replace each schema that is AAAAA with BBBBB. We can do this with jq:
$ jq 'map(if .schema == "AAAAA" then .schema = "BBBBB" else . end)' file.json
[
"host": "myhost",
"schema": "BBBBB",
"lunch": "sandwhich"
,
"host": "myotherhost",
"schema": "QQQQQ",
"lunch": "pizza"
]
If it doesn't matter what the old schema was:
$ jq 'map(.schema = "BBBBB")' file.json
[
"host": "myhost",
"schema": "BBBBB",
"lunch": "sandwhich"
,
"host": "myotherhost",
"schema": "BBBBB",
"lunch": "pizza"
]
Since jq is a proper JSON parser, this would work even if the file was presented in a more compact form, for example as the single line
["host":"myhost","schema":"AAAAA","lunch":"sandwhich","host":"myotherhost","schema":"QQQQQ","lunch":"pizza"]
Example JSON file:
[
"host": "myhost",
"schema": "AAAAA",
"lunch": "sandwich"
,
"host": "myotherhost",
"schema": "QQQQQ",
"lunch": "pizza"
]
We'd like to replace each schema that is AAAAA with BBBBB. We can do this with jq:
$ jq 'map(if .schema == "AAAAA" then .schema = "BBBBB" else . end)' file.json
[
"host": "myhost",
"schema": "BBBBB",
"lunch": "sandwhich"
,
"host": "myotherhost",
"schema": "QQQQQ",
"lunch": "pizza"
]
If it doesn't matter what the old schema was:
$ jq 'map(.schema = "BBBBB")' file.json
[
"host": "myhost",
"schema": "BBBBB",
"lunch": "sandwhich"
,
"host": "myotherhost",
"schema": "BBBBB",
"lunch": "pizza"
]
Since jq is a proper JSON parser, this would work even if the file was presented in a more compact form, for example as the single line
["host":"myhost","schema":"AAAAA","lunch":"sandwhich","host":"myotherhost","schema":"QQQQQ","lunch":"pizza"]
edited 38 mins ago
answered 45 mins ago
Kusalananda
110k14213337
110k14213337
thanks for the guidance.. however, in my case "schema" can be anywhere in the tree. and it's value will be anything.. There will be only one "schema" actually.
â user3613331
37 mins ago
1
@user3613331 Is the data structure not defined so that theschemais always in the same place all the time? It may occur anywhere in the file, sure, but it must always be in the same logical position relative to its parent objects, or the program that uses the data would not be able to find it.
â Kusalananda
22 mins ago
schema will not be in the same place in the entire json file. So I will need to find the corresponding element and have to replace.
â user3613331
21 mins ago
i need to find a way to locate the schema, and if it exists.. For example, .rules.behaviors.options | select(.name = "schema") then replace only that
â user3613331
17 mins ago
Here is the exact file I am talking about jqplay.org/s/x7RirUrfCi
â user3613331
3 mins ago
 |Â
show 1 more comment
thanks for the guidance.. however, in my case "schema" can be anywhere in the tree. and it's value will be anything.. There will be only one "schema" actually.
â user3613331
37 mins ago
1
@user3613331 Is the data structure not defined so that theschemais always in the same place all the time? It may occur anywhere in the file, sure, but it must always be in the same logical position relative to its parent objects, or the program that uses the data would not be able to find it.
â Kusalananda
22 mins ago
schema will not be in the same place in the entire json file. So I will need to find the corresponding element and have to replace.
â user3613331
21 mins ago
i need to find a way to locate the schema, and if it exists.. For example, .rules.behaviors.options | select(.name = "schema") then replace only that
â user3613331
17 mins ago
Here is the exact file I am talking about jqplay.org/s/x7RirUrfCi
â user3613331
3 mins ago
thanks for the guidance.. however, in my case "schema" can be anywhere in the tree. and it's value will be anything.. There will be only one "schema" actually.
â user3613331
37 mins ago
thanks for the guidance.. however, in my case "schema" can be anywhere in the tree. and it's value will be anything.. There will be only one "schema" actually.
â user3613331
37 mins ago
1
1
@user3613331 Is the data structure not defined so that the
schema is always in the same place all the time? It may occur anywhere in the file, sure, but it must always be in the same logical position relative to its parent objects, or the program that uses the data would not be able to find it.â Kusalananda
22 mins ago
@user3613331 Is the data structure not defined so that the
schema is always in the same place all the time? It may occur anywhere in the file, sure, but it must always be in the same logical position relative to its parent objects, or the program that uses the data would not be able to find it.â Kusalananda
22 mins ago
schema will not be in the same place in the entire json file. So I will need to find the corresponding element and have to replace.
â user3613331
21 mins ago
schema will not be in the same place in the entire json file. So I will need to find the corresponding element and have to replace.
â user3613331
21 mins ago
i need to find a way to locate the schema, and if it exists.. For example, .rules.behaviors.options | select(.name = "schema") then replace only that
â user3613331
17 mins ago
i need to find a way to locate the schema, and if it exists.. For example, .rules.behaviors.options | select(.name = "schema") then replace only that
â user3613331
17 mins ago
Here is the exact file I am talking about jqplay.org/s/x7RirUrfCi
â user3613331
3 mins ago
Here is the exact file I am talking about jqplay.org/s/x7RirUrfCi
â user3613331
3 mins ago
 |Â
show 1 more comment
up vote
0
down vote
You don't need grep, awk, and sed for this; awk can do all the task (untested):
awk '/schema/ $3 = "BBBBB" 1' test.json > /tmp/$$ && mv /tmp/$$ test.json
Please try and report back.
1
That will change the entire.jsonfile to only the newly-changed line, discarding the rest of the contents.
â DopeGhoti
1 hour ago
@DopeGhoti: Not sure I understand?
â RudiC
1 hour ago
awk '/schema/ $3 = "BBBBB" 1' rex.json > /tmp/$$ && mv /tmp/$$ yy.json But yy.json contains the same AAAAA itself.. did not change. Where rex.json contains "AAAAA-AAAAA-AAAAA-AAAAA-AAAAA",
â user3613331
1 hour ago
oops... please excuse.. let me update the contents in rex.json
â user3613331
1 hour ago
currently, rex.json contains "schema" : "AAAAA-AAAAA-AAAAA-AAAAA-AAAAA", I tried the following command awk '/schema/ $3 = "BBBBB" 1' rex.json > /tmp/$$ && mv /tmp/$$ yy.json Now yy.json has "schema" : BBBBB Almost there... I need to keep double-quotes there itself along with comma. Basically the entire line should be same.
â user3613331
1 hour ago
 |Â
show 2 more comments
up vote
0
down vote
You don't need grep, awk, and sed for this; awk can do all the task (untested):
awk '/schema/ $3 = "BBBBB" 1' test.json > /tmp/$$ && mv /tmp/$$ test.json
Please try and report back.
1
That will change the entire.jsonfile to only the newly-changed line, discarding the rest of the contents.
â DopeGhoti
1 hour ago
@DopeGhoti: Not sure I understand?
â RudiC
1 hour ago
awk '/schema/ $3 = "BBBBB" 1' rex.json > /tmp/$$ && mv /tmp/$$ yy.json But yy.json contains the same AAAAA itself.. did not change. Where rex.json contains "AAAAA-AAAAA-AAAAA-AAAAA-AAAAA",
â user3613331
1 hour ago
oops... please excuse.. let me update the contents in rex.json
â user3613331
1 hour ago
currently, rex.json contains "schema" : "AAAAA-AAAAA-AAAAA-AAAAA-AAAAA", I tried the following command awk '/schema/ $3 = "BBBBB" 1' rex.json > /tmp/$$ && mv /tmp/$$ yy.json Now yy.json has "schema" : BBBBB Almost there... I need to keep double-quotes there itself along with comma. Basically the entire line should be same.
â user3613331
1 hour ago
 |Â
show 2 more comments
up vote
0
down vote
up vote
0
down vote
You don't need grep, awk, and sed for this; awk can do all the task (untested):
awk '/schema/ $3 = "BBBBB" 1' test.json > /tmp/$$ && mv /tmp/$$ test.json
Please try and report back.
You don't need grep, awk, and sed for this; awk can do all the task (untested):
awk '/schema/ $3 = "BBBBB" 1' test.json > /tmp/$$ && mv /tmp/$$ test.json
Please try and report back.
answered 1 hour ago
RudiC
2,06219
2,06219
1
That will change the entire.jsonfile to only the newly-changed line, discarding the rest of the contents.
â DopeGhoti
1 hour ago
@DopeGhoti: Not sure I understand?
â RudiC
1 hour ago
awk '/schema/ $3 = "BBBBB" 1' rex.json > /tmp/$$ && mv /tmp/$$ yy.json But yy.json contains the same AAAAA itself.. did not change. Where rex.json contains "AAAAA-AAAAA-AAAAA-AAAAA-AAAAA",
â user3613331
1 hour ago
oops... please excuse.. let me update the contents in rex.json
â user3613331
1 hour ago
currently, rex.json contains "schema" : "AAAAA-AAAAA-AAAAA-AAAAA-AAAAA", I tried the following command awk '/schema/ $3 = "BBBBB" 1' rex.json > /tmp/$$ && mv /tmp/$$ yy.json Now yy.json has "schema" : BBBBB Almost there... I need to keep double-quotes there itself along with comma. Basically the entire line should be same.
â user3613331
1 hour ago
 |Â
show 2 more comments
1
That will change the entire.jsonfile to only the newly-changed line, discarding the rest of the contents.
â DopeGhoti
1 hour ago
@DopeGhoti: Not sure I understand?
â RudiC
1 hour ago
awk '/schema/ $3 = "BBBBB" 1' rex.json > /tmp/$$ && mv /tmp/$$ yy.json But yy.json contains the same AAAAA itself.. did not change. Where rex.json contains "AAAAA-AAAAA-AAAAA-AAAAA-AAAAA",
â user3613331
1 hour ago
oops... please excuse.. let me update the contents in rex.json
â user3613331
1 hour ago
currently, rex.json contains "schema" : "AAAAA-AAAAA-AAAAA-AAAAA-AAAAA", I tried the following command awk '/schema/ $3 = "BBBBB" 1' rex.json > /tmp/$$ && mv /tmp/$$ yy.json Now yy.json has "schema" : BBBBB Almost there... I need to keep double-quotes there itself along with comma. Basically the entire line should be same.
â user3613331
1 hour ago
1
1
That will change the entire
.json file to only the newly-changed line, discarding the rest of the contents.â DopeGhoti
1 hour ago
That will change the entire
.json file to only the newly-changed line, discarding the rest of the contents.â DopeGhoti
1 hour ago
@DopeGhoti: Not sure I understand?
â RudiC
1 hour ago
@DopeGhoti: Not sure I understand?
â RudiC
1 hour ago
awk '/schema/ $3 = "BBBBB" 1' rex.json > /tmp/$$ && mv /tmp/$$ yy.json But yy.json contains the same AAAAA itself.. did not change. Where rex.json contains "AAAAA-AAAAA-AAAAA-AAAAA-AAAAA",
â user3613331
1 hour ago
awk '/schema/ $3 = "BBBBB" 1' rex.json > /tmp/$$ && mv /tmp/$$ yy.json But yy.json contains the same AAAAA itself.. did not change. Where rex.json contains "AAAAA-AAAAA-AAAAA-AAAAA-AAAAA",
â user3613331
1 hour ago
oops... please excuse.. let me update the contents in rex.json
â user3613331
1 hour ago
oops... please excuse.. let me update the contents in rex.json
â user3613331
1 hour ago
currently, rex.json contains "schema" : "AAAAA-AAAAA-AAAAA-AAAAA-AAAAA", I tried the following command awk '/schema/ $3 = "BBBBB" 1' rex.json > /tmp/$$ && mv /tmp/$$ yy.json Now yy.json has "schema" : BBBBB Almost there... I need to keep double-quotes there itself along with comma. Basically the entire line should be same.
â user3613331
1 hour ago
currently, rex.json contains "schema" : "AAAAA-AAAAA-AAAAA-AAAAA-AAAAA", I tried the following command awk '/schema/ $3 = "BBBBB" 1' rex.json > /tmp/$$ && mv /tmp/$$ yy.json Now yy.json has "schema" : BBBBB Almost there... I need to keep double-quotes there itself along with comma. Basically the entire line should be same.
â user3613331
1 hour ago
 |Â
show 2 more comments
up vote
0
down vote
A couple of optimizations, not least of which is eschewing a "useless use of cat". You don't need to cat file | grep pattern; you can just grep pattern file. And since you're grepping into awk, that can be simplified too:
currentSchema="$( cat test.json | grep schema | awk 'print $3' )"
becomes
currentSchema="$( awk '/schema/ print $3' test.json )"
Now, for your sed script. You are using Strong Quotes (') around your sed command, which means your shell variables will not be parsed. You need to use Weak Quotes (") for this to work:
currentSchema="$( awk '/schema/ print $3' test.json )"
sed --in-place "s/$currentSchema/BBBBB/" test.json
Thanks for the optimization steps! I tried the second command but it is giving the following error sed --in-place "s/$currentSchema/BBBBB" rex.json
â user3613331
1 hour ago
sed: -e expression #1, char 40: unterminated `s' command
â user3613331
1 hour ago
You need a final/after the replacement.s/new/old/.
â DopeGhoti
1 hour ago
rex.json contains "schema" : "AAAAA-AAAAA-AAAAA-AAAAA-AAAAA", currentSchema="$( awk '/schema/ print $3' rex.json )" sed --in-place 's/$currentSchema/BBBBB-BBBBB-BBBBB-BBBBB-BBBBB/' rex.json But nothing has been changed in rex.json file
â user3613331
1 hour ago
add a comment |Â
up vote
0
down vote
A couple of optimizations, not least of which is eschewing a "useless use of cat". You don't need to cat file | grep pattern; you can just grep pattern file. And since you're grepping into awk, that can be simplified too:
currentSchema="$( cat test.json | grep schema | awk 'print $3' )"
becomes
currentSchema="$( awk '/schema/ print $3' test.json )"
Now, for your sed script. You are using Strong Quotes (') around your sed command, which means your shell variables will not be parsed. You need to use Weak Quotes (") for this to work:
currentSchema="$( awk '/schema/ print $3' test.json )"
sed --in-place "s/$currentSchema/BBBBB/" test.json
Thanks for the optimization steps! I tried the second command but it is giving the following error sed --in-place "s/$currentSchema/BBBBB" rex.json
â user3613331
1 hour ago
sed: -e expression #1, char 40: unterminated `s' command
â user3613331
1 hour ago
You need a final/after the replacement.s/new/old/.
â DopeGhoti
1 hour ago
rex.json contains "schema" : "AAAAA-AAAAA-AAAAA-AAAAA-AAAAA", currentSchema="$( awk '/schema/ print $3' rex.json )" sed --in-place 's/$currentSchema/BBBBB-BBBBB-BBBBB-BBBBB-BBBBB/' rex.json But nothing has been changed in rex.json file
â user3613331
1 hour ago
add a comment |Â
up vote
0
down vote
up vote
0
down vote
A couple of optimizations, not least of which is eschewing a "useless use of cat". You don't need to cat file | grep pattern; you can just grep pattern file. And since you're grepping into awk, that can be simplified too:
currentSchema="$( cat test.json | grep schema | awk 'print $3' )"
becomes
currentSchema="$( awk '/schema/ print $3' test.json )"
Now, for your sed script. You are using Strong Quotes (') around your sed command, which means your shell variables will not be parsed. You need to use Weak Quotes (") for this to work:
currentSchema="$( awk '/schema/ print $3' test.json )"
sed --in-place "s/$currentSchema/BBBBB/" test.json
A couple of optimizations, not least of which is eschewing a "useless use of cat". You don't need to cat file | grep pattern; you can just grep pattern file. And since you're grepping into awk, that can be simplified too:
currentSchema="$( cat test.json | grep schema | awk 'print $3' )"
becomes
currentSchema="$( awk '/schema/ print $3' test.json )"
Now, for your sed script. You are using Strong Quotes (') around your sed command, which means your shell variables will not be parsed. You need to use Weak Quotes (") for this to work:
currentSchema="$( awk '/schema/ print $3' test.json )"
sed --in-place "s/$currentSchema/BBBBB/" test.json
edited 1 hour ago
answered 1 hour ago
DopeGhoti
41.6k55180
41.6k55180
Thanks for the optimization steps! I tried the second command but it is giving the following error sed --in-place "s/$currentSchema/BBBBB" rex.json
â user3613331
1 hour ago
sed: -e expression #1, char 40: unterminated `s' command
â user3613331
1 hour ago
You need a final/after the replacement.s/new/old/.
â DopeGhoti
1 hour ago
rex.json contains "schema" : "AAAAA-AAAAA-AAAAA-AAAAA-AAAAA", currentSchema="$( awk '/schema/ print $3' rex.json )" sed --in-place 's/$currentSchema/BBBBB-BBBBB-BBBBB-BBBBB-BBBBB/' rex.json But nothing has been changed in rex.json file
â user3613331
1 hour ago
add a comment |Â
Thanks for the optimization steps! I tried the second command but it is giving the following error sed --in-place "s/$currentSchema/BBBBB" rex.json
â user3613331
1 hour ago
sed: -e expression #1, char 40: unterminated `s' command
â user3613331
1 hour ago
You need a final/after the replacement.s/new/old/.
â DopeGhoti
1 hour ago
rex.json contains "schema" : "AAAAA-AAAAA-AAAAA-AAAAA-AAAAA", currentSchema="$( awk '/schema/ print $3' rex.json )" sed --in-place 's/$currentSchema/BBBBB-BBBBB-BBBBB-BBBBB-BBBBB/' rex.json But nothing has been changed in rex.json file
â user3613331
1 hour ago
Thanks for the optimization steps! I tried the second command but it is giving the following error sed --in-place "s/$currentSchema/BBBBB" rex.json
â user3613331
1 hour ago
Thanks for the optimization steps! I tried the second command but it is giving the following error sed --in-place "s/$currentSchema/BBBBB" rex.json
â user3613331
1 hour ago
sed: -e expression #1, char 40: unterminated `s' command
â user3613331
1 hour ago
sed: -e expression #1, char 40: unterminated `s' command
â user3613331
1 hour ago
You need a final
/ after the replacement. s/new/old/.â DopeGhoti
1 hour ago
You need a final
/ after the replacement. s/new/old/.â DopeGhoti
1 hour ago
rex.json contains "schema" : "AAAAA-AAAAA-AAAAA-AAAAA-AAAAA", currentSchema="$( awk '/schema/ print $3' rex.json )" sed --in-place 's/$currentSchema/BBBBB-BBBBB-BBBBB-BBBBB-BBBBB/' rex.json But nothing has been changed in rex.json file
â user3613331
1 hour ago
rex.json contains "schema" : "AAAAA-AAAAA-AAAAA-AAAAA-AAAAA", currentSchema="$( awk '/schema/ print $3' rex.json )" sed --in-place 's/$currentSchema/BBBBB-BBBBB-BBBBB-BBBBB-BBBBB/' rex.json But nothing has been changed in rex.json file
â user3613331
1 hour ago
add a comment |Â
user3613331 is a new contributor. Be nice, and check out our Code of Conduct.
user3613331 is a new contributor. Be nice, and check out our Code of Conduct.
user3613331 is a new contributor. Be nice, and check out our Code of Conduct.
user3613331 is a new contributor. Be nice, and check out our Code of Conduct.
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%2f475864%2fsed-command-to-replace-and-write%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
Add an
-iswitch to the sed command to edit the file in place.â Doug O'Neal
1 hour ago
1
Consider using
jqinstead to perform operations on JSON files. It's a better tool, since it understands the actual underlying format, so it's able to do a better job on it than awk and sed.â Filipe Brandenburger
1 hour ago
really? how jq can replace the strings?
â user3613331
1 hour ago