sed command to replace and write

The name of the pictureThe name of the pictureThe name of the pictureClash 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.










share|improve this question









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 -i switch to the sed command to edit the file in place.
    – Doug O'Neal
    1 hour ago






  • 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










  • really? how jq can replace the strings?
    – user3613331
    1 hour ago














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.










share|improve this question









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 -i switch to the sed command to edit the file in place.
    – Doug O'Neal
    1 hour ago






  • 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










  • really? how jq can replace the strings?
    – user3613331
    1 hour ago












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.










share|improve this question









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






share|improve this question









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.











share|improve this question









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.









share|improve this question




share|improve this question








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 -i switch to the sed command to edit the file in place.
    – Doug O'Neal
    1 hour ago






  • 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










  • 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






  • 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










  • 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










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"]





share|improve this answer






















  • 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 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











  • 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

















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.






share|improve this answer
















  • 1




    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










  • 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

















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





share|improve this answer






















  • 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










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',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






user3613331 is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















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






























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"]





share|improve this answer






















  • 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 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











  • 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














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"]





share|improve this answer






















  • 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 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











  • 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












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"]





share|improve this answer














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"]






share|improve this answer














share|improve this answer



share|improve this answer








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 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











  • 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






  • 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










  • 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












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.






share|improve this answer
















  • 1




    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










  • 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














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.






share|improve this answer
















  • 1




    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










  • 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












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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered 1 hour ago









RudiC

2,06219




2,06219







  • 1




    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










  • 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




    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










  • 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










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





share|improve this answer






















  • 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














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





share|improve this answer






















  • 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












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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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










user3613331 is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















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.













 


draft saved


draft discarded














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













































































Popular posts from this blog

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)