Sed add newline before last occurrence of brace?2019 Community Moderator ElectionHow does the “tail” command's “-f” parameter work?sed: replacing newline at a given line (or last line)Need to insert single quotes in text file for use as SQL query using sedAppend something to each list in a fileReplacing matching text after two matching lineshow to subtract first value from the last one within a column across a row in shell?Add newline character to fileEdit /etc/lvm/lvm.conf and add a new global filterappending into middle of a string using sedUsing sed to replace the last occurrence of character
Why didn’t Eve recognize the little cockroach as a living organism?
How to preserve electronics (computers, iPads and phones) for hundreds of years
How to make a list of partial sums using forEach
How to leave product feedback on macOS?
Do you waste sorcery points if you try to apply metamagic to a spell from a scroll but fail to cast it?
Limit max CPU usage SQL SERVER with WSRM
How do I prevent inappropriate ads from appearing in my game?
Anime with legendary swords made from talismans and a man who could change them with a shattered body
Pre-Employment Background Check With Consent For Future Checks
Proving an identity involving cross products and coplanar vectors
Determining multivariate least squares with constraint
Can I cause damage to electrical appliances by unplugging them when they are turned on?
Do I have to know the General Relativity theory to understand the concept of inertial frame?
How can ruler support inventing of useful things?
What should be the ideal length of sentences in a blog post for ease of reading?
Should I assume I have passed probation?
How to make money from a browser who sees 5 seconds into the future of any web page?
Does Doodling or Improvising on the Piano Have Any Benefits?
Overlapping circles covering polygon
If A is dense in Q, then it must be dense in R.
Ways of geometrical multiplication
Check if object is null and return null
If the only attacker is removed from combat, is a creature still counted as having attacked this turn?
Do I have to take mana from my deck or hand when tapping a dual land?
Sed add newline before last occurrence of brace?
2019 Community Moderator ElectionHow does the “tail” command's “-f” parameter work?sed: replacing newline at a given line (or last line)Need to insert single quotes in text file for use as SQL query using sedAppend something to each list in a fileReplacing matching text after two matching lineshow to subtract first value from the last one within a column across a row in shell?Add newline character to fileEdit /etc/lvm/lvm.conf and add a new global filterappending into middle of a string using sedUsing sed to replace the last occurrence of character
I want to insert a new line before the last occurrence of brace. My text file looks like that
"accounts":
"0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"
So what I want to do is adding a new account
through sed script.
Please note that the new account will be specified with a variable, something like:
"$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376"
shell-script shell sed command-line
add a comment |
I want to insert a new line before the last occurrence of brace. My text file looks like that
"accounts":
"0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"
So what I want to do is adding a new account
through sed script.
Please note that the new account will be specified with a variable, something like:
"$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376"
shell-script shell sed command-line
add a comment |
I want to insert a new line before the last occurrence of brace. My text file looks like that
"accounts":
"0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"
So what I want to do is adding a new account
through sed script.
Please note that the new account will be specified with a variable, something like:
"$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376"
shell-script shell sed command-line
I want to insert a new line before the last occurrence of brace. My text file looks like that
"accounts":
"0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"
So what I want to do is adding a new account
through sed script.
Please note that the new account will be specified with a variable, something like:
"$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376"
shell-script shell sed command-line
shell-script shell sed command-line
asked Mar 12 at 12:37
Stefano De AngelisStefano De Angelis
61
61
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
sed
is the wrong tool for this job. One of the right tools is jq
.
% cat wibble.json
"accounts":
"0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"
% ACCOUNT_ADDR="0xdeadbeeffeefdface0badd00dcacad0d0eeeeeeee"
% jq '."accounts"."'"$ACCOUNT_ADDR"'"."balance"="42"' wibble.json
"accounts":
"0x0000000000000000000000000000000000000008":
"builtin":
"name": "alt_bn128_pairing",
"activate_at": "0x0",
"pricing":
"alt_bn128_pairing":
"base": 100000,
"pair": 80000
,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432":
"balance": "1606938044258990275541962092341162602522202993782792835301376"
,
"0xdeadbeeffeefdface0badd00dcacad0d0eeeeeeee":
"balance": "42"
%
It also caught the fact that you had a key+value pair with no enclosing object. ☺
I just have a text file, not json.
– Stefano De Angelis
Mar 12 at 15:51
You contradict yourself.
– JdeBP
Mar 14 at 9:45
It looks like a json but if you read the question I specify that is a text file :)
– Stefano De Angelis
Mar 14 at 10:21
add a comment |
You could add a line after the occurence of "accounts",
sed "/accounts/ a
"$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
" file
(The "
are escaped, to insert variable)
sed "s/^ }$/ "$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i" n }/g" file
Will replace your }
line and print two lines.
Run sed -i ...
for changing the file, in cycle:
$ cat file
"accounts":
"0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"
$ cat script.sh
#!/bin/bash
for i in $(seq 1 5); do
ACCOUNT_ADDR="account_"$i
sed -i "/accounts/ a
"$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
" file
done
$ ./script.sh
$ cat file
"accounts":
"account_5": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
"account_4": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
"account_3": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
"account_2": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
"account_1": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
"0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"
The first solution partially works for me. However, since I need to add more than one address i repeat the command inside a for cycle, in this way I am able to iterate over all the addresses. However, this generates several replicas of the entire json, just updating the first account after the keyword "accounts", and not one unique json with the list of all the accounts.
– Stefano De Angelis
Mar 12 at 15:49
I see, just runsed -i
instead of justsed
, see my edited answer.
– ILikeMatDotH
Mar 13 at 6:50
Thank you, this worked!
– Stefano De Angelis
Mar 13 at 11:09
add a comment |
Every now and and then I like a sed challenge: using ACCOUNT_ADDR=1234
:
sed -n -e '
x
$ i
"'"$ACCOUNT_ADDR"'": "balance":0,
2,$ p
$ x; p
' file
"accounts":
"0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
"1234": "balance":0,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"
This uses the x
to stash the current line into the hold space to become the "previous" line in the next cycle.
As mentioned elsewhere, use sed -i
to save the edits in-place
You can get the same result by reversing the file and using a simpler sed command:
temp=$(mktemp)
tac file | sed '2a
"'"$ACCOUNT_ADDR"'": "balance":0,
' | tac > "$temp" && mv "$temp" file
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%2f505866%2fsed-add-newline-before-last-occurrence-of-brace%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
sed
is the wrong tool for this job. One of the right tools is jq
.
% cat wibble.json
"accounts":
"0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"
% ACCOUNT_ADDR="0xdeadbeeffeefdface0badd00dcacad0d0eeeeeeee"
% jq '."accounts"."'"$ACCOUNT_ADDR"'"."balance"="42"' wibble.json
"accounts":
"0x0000000000000000000000000000000000000008":
"builtin":
"name": "alt_bn128_pairing",
"activate_at": "0x0",
"pricing":
"alt_bn128_pairing":
"base": 100000,
"pair": 80000
,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432":
"balance": "1606938044258990275541962092341162602522202993782792835301376"
,
"0xdeadbeeffeefdface0badd00dcacad0d0eeeeeeee":
"balance": "42"
%
It also caught the fact that you had a key+value pair with no enclosing object. ☺
I just have a text file, not json.
– Stefano De Angelis
Mar 12 at 15:51
You contradict yourself.
– JdeBP
Mar 14 at 9:45
It looks like a json but if you read the question I specify that is a text file :)
– Stefano De Angelis
Mar 14 at 10:21
add a comment |
sed
is the wrong tool for this job. One of the right tools is jq
.
% cat wibble.json
"accounts":
"0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"
% ACCOUNT_ADDR="0xdeadbeeffeefdface0badd00dcacad0d0eeeeeeee"
% jq '."accounts"."'"$ACCOUNT_ADDR"'"."balance"="42"' wibble.json
"accounts":
"0x0000000000000000000000000000000000000008":
"builtin":
"name": "alt_bn128_pairing",
"activate_at": "0x0",
"pricing":
"alt_bn128_pairing":
"base": 100000,
"pair": 80000
,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432":
"balance": "1606938044258990275541962092341162602522202993782792835301376"
,
"0xdeadbeeffeefdface0badd00dcacad0d0eeeeeeee":
"balance": "42"
%
It also caught the fact that you had a key+value pair with no enclosing object. ☺
I just have a text file, not json.
– Stefano De Angelis
Mar 12 at 15:51
You contradict yourself.
– JdeBP
Mar 14 at 9:45
It looks like a json but if you read the question I specify that is a text file :)
– Stefano De Angelis
Mar 14 at 10:21
add a comment |
sed
is the wrong tool for this job. One of the right tools is jq
.
% cat wibble.json
"accounts":
"0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"
% ACCOUNT_ADDR="0xdeadbeeffeefdface0badd00dcacad0d0eeeeeeee"
% jq '."accounts"."'"$ACCOUNT_ADDR"'"."balance"="42"' wibble.json
"accounts":
"0x0000000000000000000000000000000000000008":
"builtin":
"name": "alt_bn128_pairing",
"activate_at": "0x0",
"pricing":
"alt_bn128_pairing":
"base": 100000,
"pair": 80000
,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432":
"balance": "1606938044258990275541962092341162602522202993782792835301376"
,
"0xdeadbeeffeefdface0badd00dcacad0d0eeeeeeee":
"balance": "42"
%
It also caught the fact that you had a key+value pair with no enclosing object. ☺
sed
is the wrong tool for this job. One of the right tools is jq
.
% cat wibble.json
"accounts":
"0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"
% ACCOUNT_ADDR="0xdeadbeeffeefdface0badd00dcacad0d0eeeeeeee"
% jq '."accounts"."'"$ACCOUNT_ADDR"'"."balance"="42"' wibble.json
"accounts":
"0x0000000000000000000000000000000000000008":
"builtin":
"name": "alt_bn128_pairing",
"activate_at": "0x0",
"pricing":
"alt_bn128_pairing":
"base": 100000,
"pair": 80000
,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432":
"balance": "1606938044258990275541962092341162602522202993782792835301376"
,
"0xdeadbeeffeefdface0badd00dcacad0d0eeeeeeee":
"balance": "42"
%
It also caught the fact that you had a key+value pair with no enclosing object. ☺
answered Mar 12 at 13:23
JdeBPJdeBP
37.2k477178
37.2k477178
I just have a text file, not json.
– Stefano De Angelis
Mar 12 at 15:51
You contradict yourself.
– JdeBP
Mar 14 at 9:45
It looks like a json but if you read the question I specify that is a text file :)
– Stefano De Angelis
Mar 14 at 10:21
add a comment |
I just have a text file, not json.
– Stefano De Angelis
Mar 12 at 15:51
You contradict yourself.
– JdeBP
Mar 14 at 9:45
It looks like a json but if you read the question I specify that is a text file :)
– Stefano De Angelis
Mar 14 at 10:21
I just have a text file, not json.
– Stefano De Angelis
Mar 12 at 15:51
I just have a text file, not json.
– Stefano De Angelis
Mar 12 at 15:51
You contradict yourself.
– JdeBP
Mar 14 at 9:45
You contradict yourself.
– JdeBP
Mar 14 at 9:45
It looks like a json but if you read the question I specify that is a text file :)
– Stefano De Angelis
Mar 14 at 10:21
It looks like a json but if you read the question I specify that is a text file :)
– Stefano De Angelis
Mar 14 at 10:21
add a comment |
You could add a line after the occurence of "accounts",
sed "/accounts/ a
"$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
" file
(The "
are escaped, to insert variable)
sed "s/^ }$/ "$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i" n }/g" file
Will replace your }
line and print two lines.
Run sed -i ...
for changing the file, in cycle:
$ cat file
"accounts":
"0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"
$ cat script.sh
#!/bin/bash
for i in $(seq 1 5); do
ACCOUNT_ADDR="account_"$i
sed -i "/accounts/ a
"$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
" file
done
$ ./script.sh
$ cat file
"accounts":
"account_5": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
"account_4": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
"account_3": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
"account_2": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
"account_1": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
"0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"
The first solution partially works for me. However, since I need to add more than one address i repeat the command inside a for cycle, in this way I am able to iterate over all the addresses. However, this generates several replicas of the entire json, just updating the first account after the keyword "accounts", and not one unique json with the list of all the accounts.
– Stefano De Angelis
Mar 12 at 15:49
I see, just runsed -i
instead of justsed
, see my edited answer.
– ILikeMatDotH
Mar 13 at 6:50
Thank you, this worked!
– Stefano De Angelis
Mar 13 at 11:09
add a comment |
You could add a line after the occurence of "accounts",
sed "/accounts/ a
"$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
" file
(The "
are escaped, to insert variable)
sed "s/^ }$/ "$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i" n }/g" file
Will replace your }
line and print two lines.
Run sed -i ...
for changing the file, in cycle:
$ cat file
"accounts":
"0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"
$ cat script.sh
#!/bin/bash
for i in $(seq 1 5); do
ACCOUNT_ADDR="account_"$i
sed -i "/accounts/ a
"$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
" file
done
$ ./script.sh
$ cat file
"accounts":
"account_5": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
"account_4": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
"account_3": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
"account_2": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
"account_1": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
"0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"
The first solution partially works for me. However, since I need to add more than one address i repeat the command inside a for cycle, in this way I am able to iterate over all the addresses. However, this generates several replicas of the entire json, just updating the first account after the keyword "accounts", and not one unique json with the list of all the accounts.
– Stefano De Angelis
Mar 12 at 15:49
I see, just runsed -i
instead of justsed
, see my edited answer.
– ILikeMatDotH
Mar 13 at 6:50
Thank you, this worked!
– Stefano De Angelis
Mar 13 at 11:09
add a comment |
You could add a line after the occurence of "accounts",
sed "/accounts/ a
"$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
" file
(The "
are escaped, to insert variable)
sed "s/^ }$/ "$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i" n }/g" file
Will replace your }
line and print two lines.
Run sed -i ...
for changing the file, in cycle:
$ cat file
"accounts":
"0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"
$ cat script.sh
#!/bin/bash
for i in $(seq 1 5); do
ACCOUNT_ADDR="account_"$i
sed -i "/accounts/ a
"$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
" file
done
$ ./script.sh
$ cat file
"accounts":
"account_5": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
"account_4": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
"account_3": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
"account_2": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
"account_1": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
"0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"
You could add a line after the occurence of "accounts",
sed "/accounts/ a
"$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
" file
(The "
are escaped, to insert variable)
sed "s/^ }$/ "$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i" n }/g" file
Will replace your }
line and print two lines.
Run sed -i ...
for changing the file, in cycle:
$ cat file
"accounts":
"0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"
$ cat script.sh
#!/bin/bash
for i in $(seq 1 5); do
ACCOUNT_ADDR="account_"$i
sed -i "/accounts/ a
"$ACCOUNT_ADDR": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
" file
done
$ ./script.sh
$ cat file
"accounts":
"account_5": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
"account_4": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
"account_3": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
"account_2": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
"account_1": "balance": "1606938044258990275541962092341162602522202993782792835301376i"
"0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"
edited Mar 13 at 6:54
answered Mar 12 at 12:55
ILikeMatDotHILikeMatDotH
915
915
The first solution partially works for me. However, since I need to add more than one address i repeat the command inside a for cycle, in this way I am able to iterate over all the addresses. However, this generates several replicas of the entire json, just updating the first account after the keyword "accounts", and not one unique json with the list of all the accounts.
– Stefano De Angelis
Mar 12 at 15:49
I see, just runsed -i
instead of justsed
, see my edited answer.
– ILikeMatDotH
Mar 13 at 6:50
Thank you, this worked!
– Stefano De Angelis
Mar 13 at 11:09
add a comment |
The first solution partially works for me. However, since I need to add more than one address i repeat the command inside a for cycle, in this way I am able to iterate over all the addresses. However, this generates several replicas of the entire json, just updating the first account after the keyword "accounts", and not one unique json with the list of all the accounts.
– Stefano De Angelis
Mar 12 at 15:49
I see, just runsed -i
instead of justsed
, see my edited answer.
– ILikeMatDotH
Mar 13 at 6:50
Thank you, this worked!
– Stefano De Angelis
Mar 13 at 11:09
The first solution partially works for me. However, since I need to add more than one address i repeat the command inside a for cycle, in this way I am able to iterate over all the addresses. However, this generates several replicas of the entire json, just updating the first account after the keyword "accounts", and not one unique json with the list of all the accounts.
– Stefano De Angelis
Mar 12 at 15:49
The first solution partially works for me. However, since I need to add more than one address i repeat the command inside a for cycle, in this way I am able to iterate over all the addresses. However, this generates several replicas of the entire json, just updating the first account after the keyword "accounts", and not one unique json with the list of all the accounts.
– Stefano De Angelis
Mar 12 at 15:49
I see, just run
sed -i
instead of just sed
, see my edited answer.– ILikeMatDotH
Mar 13 at 6:50
I see, just run
sed -i
instead of just sed
, see my edited answer.– ILikeMatDotH
Mar 13 at 6:50
Thank you, this worked!
– Stefano De Angelis
Mar 13 at 11:09
Thank you, this worked!
– Stefano De Angelis
Mar 13 at 11:09
add a comment |
Every now and and then I like a sed challenge: using ACCOUNT_ADDR=1234
:
sed -n -e '
x
$ i
"'"$ACCOUNT_ADDR"'": "balance":0,
2,$ p
$ x; p
' file
"accounts":
"0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
"1234": "balance":0,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"
This uses the x
to stash the current line into the hold space to become the "previous" line in the next cycle.
As mentioned elsewhere, use sed -i
to save the edits in-place
You can get the same result by reversing the file and using a simpler sed command:
temp=$(mktemp)
tac file | sed '2a
"'"$ACCOUNT_ADDR"'": "balance":0,
' | tac > "$temp" && mv "$temp" file
add a comment |
Every now and and then I like a sed challenge: using ACCOUNT_ADDR=1234
:
sed -n -e '
x
$ i
"'"$ACCOUNT_ADDR"'": "balance":0,
2,$ p
$ x; p
' file
"accounts":
"0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
"1234": "balance":0,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"
This uses the x
to stash the current line into the hold space to become the "previous" line in the next cycle.
As mentioned elsewhere, use sed -i
to save the edits in-place
You can get the same result by reversing the file and using a simpler sed command:
temp=$(mktemp)
tac file | sed '2a
"'"$ACCOUNT_ADDR"'": "balance":0,
' | tac > "$temp" && mv "$temp" file
add a comment |
Every now and and then I like a sed challenge: using ACCOUNT_ADDR=1234
:
sed -n -e '
x
$ i
"'"$ACCOUNT_ADDR"'": "balance":0,
2,$ p
$ x; p
' file
"accounts":
"0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
"1234": "balance":0,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"
This uses the x
to stash the current line into the hold space to become the "previous" line in the next cycle.
As mentioned elsewhere, use sed -i
to save the edits in-place
You can get the same result by reversing the file and using a simpler sed command:
temp=$(mktemp)
tac file | sed '2a
"'"$ACCOUNT_ADDR"'": "balance":0,
' | tac > "$temp" && mv "$temp" file
Every now and and then I like a sed challenge: using ACCOUNT_ADDR=1234
:
sed -n -e '
x
$ i
"'"$ACCOUNT_ADDR"'": "balance":0,
2,$ p
$ x; p
' file
"accounts":
"0x0000000000000000000000000000000000000008": "builtin": "name": "alt_bn128_pairing", "activate_at": "0x0", "pricing": "alt_bn128_pairing": "base": 100000, "pair": 80000 ,
"1234": "balance":0,
"0x00Ea169ce7e0992960D3BdE6F5D539C955316432": "balance": "1606938044258990275541962092341162602522202993782792835301376"
This uses the x
to stash the current line into the hold space to become the "previous" line in the next cycle.
As mentioned elsewhere, use sed -i
to save the edits in-place
You can get the same result by reversing the file and using a simpler sed command:
temp=$(mktemp)
tac file | sed '2a
"'"$ACCOUNT_ADDR"'": "balance":0,
' | tac > "$temp" && mv "$temp" file
answered 23 hours ago
glenn jackmanglenn jackman
52.4k573113
52.4k573113
add a comment |
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%2f505866%2fsed-add-newline-before-last-occurrence-of-brace%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