How to find and replace multiple field values using jq?
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
In the following json file,
{
"email": "xxx",
"pass": "yyy",
"contact": [
"id": 111,
"name": "AAA"
],
"lname": "YYY",
"name": "AAA",
"group": [
"name": "AAA",
"lname": "YYY",
],
I need to look for the key "name" and replace its value to "XXX" at all places. Can anyone please help me with the jq command which does that ?
sed json jq
New contributor
add a comment |Â
up vote
1
down vote
favorite
In the following json file,
{
"email": "xxx",
"pass": "yyy",
"contact": [
"id": 111,
"name": "AAA"
],
"lname": "YYY",
"name": "AAA",
"group": [
"name": "AAA",
"lname": "YYY",
],
I need to look for the key "name" and replace its value to "XXX" at all places. Can anyone please help me with the jq command which does that ?
sed json jq
New contributor
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
In the following json file,
{
"email": "xxx",
"pass": "yyy",
"contact": [
"id": 111,
"name": "AAA"
],
"lname": "YYY",
"name": "AAA",
"group": [
"name": "AAA",
"lname": "YYY",
],
I need to look for the key "name" and replace its value to "XXX" at all places. Can anyone please help me with the jq command which does that ?
sed json jq
New contributor
In the following json file,
{
"email": "xxx",
"pass": "yyy",
"contact": [
"id": 111,
"name": "AAA"
],
"lname": "YYY",
"name": "AAA",
"group": [
"name": "AAA",
"lname": "YYY",
],
I need to look for the key "name" and replace its value to "XXX" at all places. Can anyone please help me with the jq command which does that ?
sed json jq
sed json jq
New contributor
New contributor
edited 41 mins ago
Goro
10.4k64994
10.4k64994
New contributor
asked 1 hour ago
user2181698
82
82
New contributor
New contributor
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
4
down vote
awk
is the best available tool for any text processing:
awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file
{
"email": "xxx",
"pass": "yyy",
"contact": [
"id": 111,
"name": "XXX"
],
"lname": "YYY",
"name": "XXX",
"group": [
"name": "XXX",
"lname": "YYY",
],
The field separator -F
in awk
detects the colon :
, then awk
can see before (i.e. $1)
and after (i.e. $2)
the :
. After then, awk
will match the lines that contain the word "name". Finally, every value in the second field in the line that matches the word name
will be replaced by the string of interest (e.g. XXX).
Thanks for the reply. It didn;t work. I used the same command you provided "awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file"
â user2181698
55 mins ago
No replacement happened.. It still shows the old value
â user2181698
53 mins ago
Dear @user2181698 I just tested it and it is working would you please try again? do you want the replacement inside the file. The code prints the output in terminal if you want it to be applied to the file then do thisawk -F: '$1 ~ /"name"/sub($2,"XXX")1' file > output
please let me know ;-)
â Goro
48 mins ago
add a comment |Â
up vote
0
down vote
Using jq
based on the walk
function (needs a recent version):
jq 'walk(.name?="XXX")' file
If your jq
doesn't support the walk
function, just define it as:
jq '
# Apply f to composite entities recursively, and to atoms
def walk(f):
. as $in
| if type == "object" then
reduce keys as $key
( ; . + walk(f)) ) | f
elif type == "array" then map( walk(f) ) | f
else f
end;
walk(.name?="XXX")
' file
Credits: https://github.com/stedolan/jq/issues/963
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
awk
is the best available tool for any text processing:
awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file
{
"email": "xxx",
"pass": "yyy",
"contact": [
"id": 111,
"name": "XXX"
],
"lname": "YYY",
"name": "XXX",
"group": [
"name": "XXX",
"lname": "YYY",
],
The field separator -F
in awk
detects the colon :
, then awk
can see before (i.e. $1)
and after (i.e. $2)
the :
. After then, awk
will match the lines that contain the word "name". Finally, every value in the second field in the line that matches the word name
will be replaced by the string of interest (e.g. XXX).
Thanks for the reply. It didn;t work. I used the same command you provided "awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file"
â user2181698
55 mins ago
No replacement happened.. It still shows the old value
â user2181698
53 mins ago
Dear @user2181698 I just tested it and it is working would you please try again? do you want the replacement inside the file. The code prints the output in terminal if you want it to be applied to the file then do thisawk -F: '$1 ~ /"name"/sub($2,"XXX")1' file > output
please let me know ;-)
â Goro
48 mins ago
add a comment |Â
up vote
4
down vote
awk
is the best available tool for any text processing:
awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file
{
"email": "xxx",
"pass": "yyy",
"contact": [
"id": 111,
"name": "XXX"
],
"lname": "YYY",
"name": "XXX",
"group": [
"name": "XXX",
"lname": "YYY",
],
The field separator -F
in awk
detects the colon :
, then awk
can see before (i.e. $1)
and after (i.e. $2)
the :
. After then, awk
will match the lines that contain the word "name". Finally, every value in the second field in the line that matches the word name
will be replaced by the string of interest (e.g. XXX).
Thanks for the reply. It didn;t work. I used the same command you provided "awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file"
â user2181698
55 mins ago
No replacement happened.. It still shows the old value
â user2181698
53 mins ago
Dear @user2181698 I just tested it and it is working would you please try again? do you want the replacement inside the file. The code prints the output in terminal if you want it to be applied to the file then do thisawk -F: '$1 ~ /"name"/sub($2,"XXX")1' file > output
please let me know ;-)
â Goro
48 mins ago
add a comment |Â
up vote
4
down vote
up vote
4
down vote
awk
is the best available tool for any text processing:
awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file
{
"email": "xxx",
"pass": "yyy",
"contact": [
"id": 111,
"name": "XXX"
],
"lname": "YYY",
"name": "XXX",
"group": [
"name": "XXX",
"lname": "YYY",
],
The field separator -F
in awk
detects the colon :
, then awk
can see before (i.e. $1)
and after (i.e. $2)
the :
. After then, awk
will match the lines that contain the word "name". Finally, every value in the second field in the line that matches the word name
will be replaced by the string of interest (e.g. XXX).
awk
is the best available tool for any text processing:
awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file
{
"email": "xxx",
"pass": "yyy",
"contact": [
"id": 111,
"name": "XXX"
],
"lname": "YYY",
"name": "XXX",
"group": [
"name": "XXX",
"lname": "YYY",
],
The field separator -F
in awk
detects the colon :
, then awk
can see before (i.e. $1)
and after (i.e. $2)
the :
. After then, awk
will match the lines that contain the word "name". Finally, every value in the second field in the line that matches the word name
will be replaced by the string of interest (e.g. XXX).
edited 30 mins ago
answered 1 hour ago
Goro
10.4k64994
10.4k64994
Thanks for the reply. It didn;t work. I used the same command you provided "awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file"
â user2181698
55 mins ago
No replacement happened.. It still shows the old value
â user2181698
53 mins ago
Dear @user2181698 I just tested it and it is working would you please try again? do you want the replacement inside the file. The code prints the output in terminal if you want it to be applied to the file then do thisawk -F: '$1 ~ /"name"/sub($2,"XXX")1' file > output
please let me know ;-)
â Goro
48 mins ago
add a comment |Â
Thanks for the reply. It didn;t work. I used the same command you provided "awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file"
â user2181698
55 mins ago
No replacement happened.. It still shows the old value
â user2181698
53 mins ago
Dear @user2181698 I just tested it and it is working would you please try again? do you want the replacement inside the file. The code prints the output in terminal if you want it to be applied to the file then do thisawk -F: '$1 ~ /"name"/sub($2,"XXX")1' file > output
please let me know ;-)
â Goro
48 mins ago
Thanks for the reply. It didn;t work. I used the same command you provided "awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file"
â user2181698
55 mins ago
Thanks for the reply. It didn;t work. I used the same command you provided "awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file"
â user2181698
55 mins ago
No replacement happened.. It still shows the old value
â user2181698
53 mins ago
No replacement happened.. It still shows the old value
â user2181698
53 mins ago
Dear @user2181698 I just tested it and it is working would you please try again? do you want the replacement inside the file. The code prints the output in terminal if you want it to be applied to the file then do this
awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file > output
please let me know ;-)â Goro
48 mins ago
Dear @user2181698 I just tested it and it is working would you please try again? do you want the replacement inside the file. The code prints the output in terminal if you want it to be applied to the file then do this
awk -F: '$1 ~ /"name"/sub($2,"XXX")1' file > output
please let me know ;-)â Goro
48 mins ago
add a comment |Â
up vote
0
down vote
Using jq
based on the walk
function (needs a recent version):
jq 'walk(.name?="XXX")' file
If your jq
doesn't support the walk
function, just define it as:
jq '
# Apply f to composite entities recursively, and to atoms
def walk(f):
. as $in
| if type == "object" then
reduce keys as $key
( ; . + walk(f)) ) | f
elif type == "array" then map( walk(f) ) | f
else f
end;
walk(.name?="XXX")
' file
Credits: https://github.com/stedolan/jq/issues/963
add a comment |Â
up vote
0
down vote
Using jq
based on the walk
function (needs a recent version):
jq 'walk(.name?="XXX")' file
If your jq
doesn't support the walk
function, just define it as:
jq '
# Apply f to composite entities recursively, and to atoms
def walk(f):
. as $in
| if type == "object" then
reduce keys as $key
( ; . + walk(f)) ) | f
elif type == "array" then map( walk(f) ) | f
else f
end;
walk(.name?="XXX")
' file
Credits: https://github.com/stedolan/jq/issues/963
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Using jq
based on the walk
function (needs a recent version):
jq 'walk(.name?="XXX")' file
If your jq
doesn't support the walk
function, just define it as:
jq '
# Apply f to composite entities recursively, and to atoms
def walk(f):
. as $in
| if type == "object" then
reduce keys as $key
( ; . + walk(f)) ) | f
elif type == "array" then map( walk(f) ) | f
else f
end;
walk(.name?="XXX")
' file
Credits: https://github.com/stedolan/jq/issues/963
Using jq
based on the walk
function (needs a recent version):
jq 'walk(.name?="XXX")' file
If your jq
doesn't support the walk
function, just define it as:
jq '
# Apply f to composite entities recursively, and to atoms
def walk(f):
. as $in
| if type == "object" then
reduce keys as $key
( ; . + walk(f)) ) | f
elif type == "array" then map( walk(f) ) | f
else f
end;
walk(.name?="XXX")
' file
Credits: https://github.com/stedolan/jq/issues/963
answered 54 mins ago
oliv
1,386210
1,386210
add a comment |Â
add a comment |Â
user2181698 is a new contributor. Be nice, and check out our Code of Conduct.
user2181698 is a new contributor. Be nice, and check out our Code of Conduct.
user2181698 is a new contributor. Be nice, and check out our Code of Conduct.
user2181698 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%2f476536%2fhow-to-find-and-replace-multiple-field-values-using-jq%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