This question already has answers here:
jq special characters in nested keys
(1 answer)
How to use jq when the variable has reserved characters?
(3 answers)
Escape field name in jq that contains '#' and '-'? [duplicate]
(2 answers)
Closed 3 months ago.
I make a call to an HPE iLO to pull back SNMP v3 user information. I get:
curl --silent --insecure --header "X-API-Token: ${iLOAuth}" --request GET ${iLOSSO}/redfish/v1/Managers/v1/snmpservice/snmpusers | jq -r '.Members[]'
and get back:
{
"#odata.id": "/redfish/v1/Managers/1/SnmpService/SNMPUsers/1"
}
{
"#odata.id": "/redfish/v1/Managers/1/SnmpService/SNMPUsers/3"
}
I just want to get back the values, but if I try:
jq -r '.Members[] | .#odata.id'
I get back an error. Does anyone know the proper syntax to use?
Related
This question already has answers here:
How to check if element exists in array with jq
(6 answers)
Closed 1 year ago.
I've looked through many other stack overflow questions that are similar, but can't find exactly what I'm looking for. This seems simple, but I cannot get the syntax quite right and I'm tired of throwing spaghetti at the wall. Given this return from the AWS CLI:
{
"IPSet":{
"Description":"",
"Name":"testip",
"IPAddressVersion":"IPV4",
"Id":"a1b2c3d4-5678-90ab-cdef-EXAMPLE1111",
"ARN":"arn:aws:wafv2:us-west-2:123456789012:regional/ipset/testip/a1b2c3d4-5678-90ab-cdef-EXAMPLE1111",
"Addresses":[
"192.0.2.0/16"
]
},
"LockToken":"447e55ac-2396-4c6d-b9f9-86b67c17f8b5"
}
How would use JQ to determine if it contained a certain IP address? This is my closest guess so far, but not quite right.
echo ${single_ip_set} | jq -c '.IPSet.Addresses[] | contains("255.255.255.8")'
single_ip_set is my variable name. This checks every element in the array and gives a response. So does this:
echo ${single_ip_set} | jq -c '.IPSet.Addresses[] | index("255.255.255.8")'
I really just want one final answer as whether or not the array contains one instance. of my requested IP.
If you want to determine if any value in a sequence is true for a predicate, this is a job for any(). If you want to do an exact string comparison, == is the right tool for that job.
jq -c \
--arg tgt "192.0.2.0/16" \
'.IPSet.Addresses | any(. == $tgt)' \
<<<"$single_ip_set"
...is true, whereas:
jq -c \
--arg tgt "255.255.255.8" \
'.IPSet.Addresses | any(. == $tgt)' \
<<<"$single_ip_set"
...is false.
This question already has an answer here:
json format each object in a line
(1 answer)
Closed 2 years ago.
How can jq get convinced to return results in one row?
Without any options, the results get formatted
echo '{"name":"New release","description":"Super nice release","milestones":["v1.0","v1.0-rc"]}' | jq '.milestones'
[
"v1.0",
"v1.0-rc"
]
I would like to have it in one row:
echo '{"name":"New release","description":"Super nice release","milestones":["v1.0","v1.0-rc"]}' | jq '.milestones'
["v1.0","v1.0-rc"]
From the manual
--compact-output / -c:
By default, jq pretty-prints JSON output. Using this option will result in more compact output by instead putting each JSON object on a single line.
can stand in front and behind the filter
echo '{"name":"New release","description":"Super nice release","milestones":["v1.0","v1.0-rc"]}' | jq -c '.milestones'
["v1.0","v1.0-rc"]
echo '{"name":"New release","description":"Super nice release","milestones":["v1.0","v1.0-rc"]}' | jq '.milestones' -c
["v1.0","v1.0-rc"]
This question already has answers here:
Simple jq filters not working in Windows shell, various quoting issues
(1 answer)
jq: How to output quotes on raw output on windows
(3 answers)
Closed 3 years ago.
Thanks in advance for taking a look at this. I have constructed a working jq filter with the help of other Stack Overflow threads but I can't get it working in my windows terminal
Here it is directly from jqplay:
jq '.objects[ ] | "\(.id) \(.batch_fields.SJNB)"'
Here is what I have unsuccessfully tried thus far:
jq '.objects[ ] | "\(.id) \(.batch_fields.SJNB)"' file.json
jq ".objects[ ] | "\(.id) \(.batch_fields.SJNB)" file.json"
jq .objects[ ] | "\(.id) \(.batch_fields.SJNB) file.json
It's clear that it's a problem with quoting or not quoting and I can get more simple commands to work such as:
jq-win64.exe .objects[].id Row0.json
But I can't seem to crack the issue for the more complicated one.
Thanks,
Jason
If you're running this in the windows command prompt, you have to use double quotes to quote your filter, there's no getting around it. Then from there, you just need to escape characters in your filter appropriately.
> jq ".objects[] | \"\(.id) \(.batch_fields.SJNB)\"" file.json
This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 4 years ago.
I know that for replacing a string (in a file which has matchstring string), we can use following command
grep -rl matchstring somedir/ | xargs sed -i 's/string1/string2/g'
How can I use/change the command if my string has special characters like "/"?
For example:
string1: "/home/folder1"
string2: "/home/folder1/folder2"
As #jamieguinan mentioned in his command, almost any delimiter character can be used. So, I changed the command as following: grep -rl matchstring somedir/ | xargs sed -i 's,string1,string2,g' Where string1 and string2 are: /home/folder1 and /home/folder1/folder2, respectively.
This question already has answers here:
How to print matched regex pattern using awk?
(9 answers)
How to print regexp matches using awk? [duplicate]
(3 answers)
Closed 7 years ago.
Below are the file contents:
{30001002|XXparameter|XSD_LOC|$\{FILES_DIR\}/xsd/EDXFB_mbr_demo.xsd|3|2|$|#{0|}}
{30001002|XXparameter|source_files|$XSD/EDXFB_mbr_demo.xsd|3|1|l|#{0|}}
I trying to accomplish below using awk:
Firstly I want to search for string Pattern "EDXFB*.xsd".
If exists, then extract the strings that starts with "EDXFB" and ends with ".xsd"
Output:
EDXFB_mbr_demo.xsd
EDXFB_mbr_demo.xsd
The basic awk pattern to extract the expression and print out matched data is following:
gawk 'match($0, /EDXFB.+\.xsd/, a) { print a[0] }'
Though, you should really spend some time reading awk manual.
And the regular expression could be changed to /EDXFB[a-z_]+\.xsd/ if it contains only lower-cased characters and _.
[EDIT]: Updated with cleaner code from #JID. Thanks :)
Here is one way to do it:
awk -F/ '/EDXFB.*\.xsd/ {split($NF,a,"|");print a[1]}' file
EDXFB_mbr_demo.xsd
EDXFB_mbr_demo.xsd
It separate the line by / then print last field until |
In your example, probably grep would do what you want:
grep -o 'EDXFB.*\.xsd'