I'm need simple example for using libyaml to get all keys+values as strings from yaml file.
lets say i have this yaml file:
Active Groups:
btcoex: yes
datapath: no
Header:
TLV_data_version: 1
command id: 0xf1
group id: 0x01
btcoex:
enabel ATS:
Active: yes
Apply_point: APPLY_POINT_AFTER_ALIVE
selection:
set: 1
table: 3
cpu: 1
how can I just print all the keys and values?
Related
I have a YAML document that contains an array. I would like to conditionally add properties to the elements of that array using yq version 4 from mikefarah.
Here is a sample YAML document.
name: "My Pets"
pets:
- name: "cat"
age: 8
- name: "dog"
age: 3
- name: "mouse"
age: 1
I would like to transform this into,
name: "My Pets"
pets:
- name: "cat"
age: 8
shots: cat.upToDate
- name: "dog"
age: 3
shots: dog.upToDate
- name: "mouse"
age: 1
shots: mouse.upToDate
where we add a shots property to each element of pets. The value of shots should be whatever the name value is, dot, upToDate.
I'm trying something like this,
yq eval '.pets[] | select(.name == "cat").shots = "cat.upToDate"' test.yaml
but that produces,
name: "cat"
age: 8
shots: cat.upToDate
name: "dog"
age: 3
name: "mouse"
age: 1
I need to preserve the entire original YAML document and just insert the shots property.
This is close but missing all of the other pets.
yq eval '.pets = (.pets[] | select(.name == "cat").shots = "cats.upToDate")' test.yaml
It produces,
name: "My Pets"
pets:
name: "cat"
age: 8
shots: cats.upToDate
I'm thinking maybe we could store the name of the pet in a variable and reference that later, but v4 is brand new to me today.
I would prefer to have a one-liner so that I don't have to filter on .name. This array has less than 10 elements so I could easily hard-code the name and call yq 10 times.
Any thoughts or suggestions? Many thanks, Weldon
Use |=, e.g. like so:
yq eval '.pets[] |= (.shots = (.name + ".upToDate"))' pets.yaml
I'm am trying to use the i18napis.appspot.com/address address formats for a React application so that I can create localized address forms, but it's not clear to me how to use it.
For example, this is the info for Germany:
{
zipex: "26133,53225",
key: "DE",
zip: "\d{5}",
fmt: "%N%n%O%n%A%n%Z %C",
id: "data/DE",
posturl: "http://www.postdirekt.de/plzserver/",
require: "ACZ",
name: "GERMANY"
}
The zip is a pretty straight forward regex, but how do I use the fmt: "%N%n%O%n%A%n%Z %C" format? What are N O A Z and C in this context?
According to Google's libaddressinput documentation,
N - Name
O - Organization
A - Street Address Line(s)
Z - Zip or postal code
C - City or Locality
I am writing in shellscript.
I have a JSON data. I need to extract the counterid and pass that data to a .yml file. I want to replace data in the yml file if counter_name (in the JSON data) is matched with the data in the yml file and replace with it counterid.
Example:
{"counterId" : 200001 ,"counterName" = status-total_access"}
Original yml file:
- rename:
fields:
- from: "apache.status.total_kbytes"
to: "apache.status-total_kbytes"
yml file should be changed as:
- rename:
fields:
- from: "apache.status.total_kbytes"
to: "apache.2000001"
So, is there any way to do it in shellscript?
I know there is an option called sed, but don't know how to extract the JSON values.
This is my json data:
[{"counterId":200001,"counterName":"status-total_accesses"},{"counterId":200002,"counterName":"status-total_kbytes"},{"counterId":200003,"counterName":"status-requests_per_sec"},{"counterId":200004,"counterName":"status-bytes_per_sec"},{"counterId":200005,"counterName":"status-bytes_per_request"},{"counterId":200006,"counterName":"workers-busy"}]
This is my yml file:
- rename:
fields:
- from: "apache.status.workers.busy"
to: "apache.workers-busy"
ignore_missing: true
- rename:
fields:
- from: "apache.status.workers.idle"
to: "apache.workers-idle"
ignore_missing: true
- rename:
fields:
- from: "apache.status.total_accesses"
to: "apache.status-total_accesses"
ignore_missing: true
- rename:
fields:
- from: "apache.status.total_kbytes"
to: "apache.status-total_kbytes"
ignore_missing: true
- rename:
fields:
- from: "apache.status.uptime.server_uptime"
to: "apache.uptime-server_uptime"
ignore_missing: true
You will need something like jq. Using jq you will get the counterId and counterName value from each object. In order to achieve this first to iterate over each value, and from it you use again the jq to extract each field. After that, you just sed the name with the value in the original file or a copy.
A very quick implementation billow, this will print each time is doing a sed in the file on the standard output the whole file. You should use -i to replace values in place. json.in and yaml.in are the input files having the content you provided.
#!/bin/bash
for row in $(cat json.in | jq -c '.[]' ); do
value=$(echo ${row}|jq '.counterId' )
name=$(echo ${row}|jq -r '.counterName' )
echo "$value $name"
sed -i "s/$name/$value/g" yaml.in
done
the output was:
- rename:
fields:
- from: "apache.status.workers.busy"
to: "apache.200006"
ignore_missing: true
- rename:
fields:
- from: "apache.status.workers.idle"
to: "apache.workers-idle"
ignore_missing: true
- rename:
fields:
- from: "apache.status.total_accesses"
to: "apache.200001"
ignore_missing: true
- rename:
fields:
- from: "apache.status.total_kbytes"
to: "apache.200002"
ignore_missing: true
- rename:
fields:
- from: "apache.status.uptime.server_uptime"
to: "apache.uptime-server_uptime"
ignore_missing: true
Using Powershell I am calling win32_computersystem. I want to list data about the machine including $_thermalstate - Here is my code
The code looks as though it should work but returns a empty value. I want to create an inline array or hash table that the value $_.thermalstate references.
Get-WmiObject win32_computersystem | select Name, Model, Caption, #{n="Timezone"; e={$_.currenttimezone}}, Description, DNShostname,Domain,#{n='Domain Role'; E={$_.domainrole}},Roles,Status,#{n='System Type'; e={$_.systemtype}},#{n='Thermal State'; e={$_.thermalstate[#{'3'='safe'}]}}
Output
Name : MYPC
Model : Latitude E5470
Caption : MYPC
Timezone : 600
Description : AT/AT COMPATIBLE
DNShostname : MYPC
Domain : work.biz
Domain Role : 1
Roles : {LM_Workstation, LM_Server, NT}
Status : OK
System Type : x64-based PC
Thermal State : Safe
your lookup structure was ... wrong. [grin]
replace the last line of this reformatted version of your code ...
Get-WmiObject win32_computersystem |
Select-Object Name, Model, Caption,
#{n="Timezone"; e={$_.currenttimezone}},
Description, DNShostname,Domain,
#{n='Domain Role'; E={$_.domainrole}},
Roles,Status,
#{n='System Type'; e={$_.systemtype}},
#{n='Thermal State'; e={$_.thermalstate[#{'3'='safe'}]}}
... with this line ...
#{n='Thermal State'; e={#{'3'='Safe'}["$($_.ThermalState)"]}}
note that the lookup table is on the OUTSIDE of the [] and that the value is forced to a string.
however, i would NOT do it this way. it's too finicky. create the lookup table BEFORE your call and use that to perform the lookup.
Your code looks like you are trying to declare/initialize a hash table while also trying to use thermalstate as a hash array.
If you initialize the hash array first, the code looks like this:
$h = #{'3'='safe'}; Get-WmiObject win32_computersystem | select Name, Model, Caption, #{n="Timezone"; e={$_.currenttimezone}}, Description, DNShostname,Domain,#{n='Domain Role';E={$_.domainrole}},Roles,Status,#{n='System Type'; e={$_.systemtype}},#{n='Thermal State'; e={$h[$_.thermalstate.toString()]}}
According to https://wutils.com/wmi/root/cimv2/win32_computersystem/
ThermalState property
CIMTYPE 'uint16'
Description 'The ThermalState property identifies the enclosure's thermal state when last booted.'
MappingStrings ['SMBIOS|Type 3|System Enclosure or Chassis|Thermal State']
read True
ValueMap ['1', '2', '3', '4', '5', '6']
Values ['Other', 'Unknown', 'Safe', 'Warning', 'Critical', 'Non-recoverable']
ThermalState property is in 1 class (Win32_ComputerSystem) of ROOT\cimv2 and in 2 namespaces
You could create an enum
enum ThermalState {
Other = 1
Unknown = 2
Safe = 3
Warning = 4
Critical = 5
NonRecoverable = 6
}
And use that to get a verbose response from the property
Get-WmiObject win32_computersystem | Select-Object Name, Model, Caption,
#{n="Timezone"; e={$_.currenttimezone}}, Description, DNShostname,Domain,
#{n='Domain Role';E={$_.domainrole}},Roles,Status,
#{n='System Type'; e={$_.systemtype}},
#{n='Thermal State'; e={[ThermalState]$_.thermalstate}}
Sample output
Name : HP-G1610
Model : ProLiant MicroServer Gen8
Caption : HP-G1610
Timezone : 120
Description : AT/AT COMPATIBLE
DNShostname : HP-G1610
Domain : DOMAIN
Domain Role : 0
Roles : {...}
Status : OK
System Type : x64-based PC
Thermal State : Safe
In general to get a list of an enum :
> $Enum ='System.DayOfWeek'
> [Enum]::GetValues($Enum) | ForEach-Object {'{0} {1}' -f [int]$_,$_ }
0 Sunday
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
I am a beginner with R. My situation is I have a JSON dataset with a nested array. In the JSON file, one institution looks like this:
{
"website": "www.123.org",
"programs": [
{
"website": "www.111.com",
"contact": "Jim"
},
{
"website": "www.222.com",
"contact": "Han"
}
]
}
To each institution, there may be one program or may be more. I have more than 100 hundreds institution and nearly two hundreds programs in the JSON. I want to ad id for each institution and idpr for each program. Finally, i hope i can get a data.frame that looks like:
id idpr website websitepr contactpr
1 1 www.123.org www.111.com Jim
1 2 www.123.org www.222.com Han
2 1 www.345.org www.aaa.com Lily
3 1 www.567.org www.bbb.com Jack
3 2 www.567.org www.ccc.com Mike
3 3 www.567.org www.ddd.com Minnie
.........
I tried to write a nested loop like this:
count<-0
for (n in json_data){
count<-count+1
id<-c(id,count)
website<-c(website,n$website)
countpr<-1
for (i in n$programs){
id<-c(id,count)
website<-c(website,n$website)
idpr<-c(idpr,countpr)
websitepr<-c(websitepr,i$website)
contactpr<-c(contactpr,i$contact)
countpr<-countpr+1
}
}
but this nested loop can not give me the result i want. Thanks for helping me!
Try this:
# sample data
json.file <- textConnection('[{"website":"www.123.org","programs":[{"website":"www.111.com","contact":"Jim"},{"website":"www.222.com","contact":"Han"}]},{"website":"www.345.org","programs":[{"website":"www.aaa.com","contact":"Lily"}]},{"website":"www.567.org","programs":[{"website":"www.bbb.com","contact":"Jack"},{"website":"www.ccc.com","contact":"Mike"},{"website":"www.ddd.com","contact":"Minnie"}]}]')
# read the data into an R nested list
library(rjson)
raw.data <- fromJSON(file = json.file)
# a function that will process one institution
process.one <- function(id, institution) {
website <- institution$website
websitepr <- sapply(institution$programs, `[[`, "website")
contactpr <- sapply(institution$programs, `[[`, "contact")
data.frame(id, idpr = seq_along(websitepr),
website, websitepr, contactpr)
}
# run the function on all institutions and put the pieces together
do.call(rbind, Map(process.one, seq_along(raw.data), raw.data))
# id idpr website websitepr contactpr
# 1 1 1 www.123.org www.111.com Jim
# 2 1 2 www.123.org www.222.com Han
# 3 2 1 www.345.org www.aaa.com Lily
# 4 3 1 www.567.org www.bbb.com Jack
# 5 3 2 www.567.org www.ccc.com Mike
# 6 3 3 www.567.org www.ddd.com Minnie
you can write a
class website {
//write all as data member
//program as object of class program
}
and use jackson api to convert it into string.
using Mapper.writeValueAsString(object of website)
jar needed are
1.jackson-core-2.0.2.jar
jackson-databind-2.0.2.jar.