I am using awk command but unable to print IPs in in column using shell script - shell

I have a file "LM1" in below format and I wanted these to be printed in separated column like this below example
I have used below awk command to achieve this but I cannot extract IP4 field in csv file rest of the data is coming in columns.
I execute below command to achieve my requirements
I have a file "LM1" in below format and I wanted these to be printed in separated column like this below example
awk -F": " -v OFS="\t" '
BEGIN {print "CustName", "OS", "LM_Name", "Name", "IPv4", "Status" }
{
gsub(/"/,"")
sub(/^[[:blank:]]+/,"")
}
$1 == "customer_name" {
if ("customer_name" in data && !have_data)
print data["customer_name"]
have_data = 0
}
{
data[$1] = $2
}
("os_type" in data) && ("local_hostname" in data) && ("name" in data) && ("local_ipv4" in data) && ("status" in data) {
print data["customer_name"], data["os_type"], data["local_hostname"], data["name"], data["local_ipv4"], data["status"]
delete data["os_type"]
delete data["local_hostname"]
delete data["name"]
delete data["local_ipv4"]
delete data["status"]
have_data = 1
}
' LM1 | column -s $'\t' -t > LM-Status-Report.csv
Expected (Output) data format:
Column A Column B Column C Column D
==========================================================
Customer Local_Hostname IP4 Addresse Status
==========================================================
ABC ABC-log-01 10.9.9.9,10.9.3.4 OK
ABC ABC-log-02 10.8.8.8 New
XYZ XYZ-log-01 10.10.2.2,10.2.4.6 Ok
XYZ XYZ-log-02 10.2.3.4 New
RAW Data existing (input) format:
customer_name: "ABC"
{
"syslog": {
"created": {
"at": 1478195183
"by": 0
}
"id": "886707D0-4069-1005-8535-0050568525D9"
"metadata": {
"local_hostname": "ABC-log-01"
"local_ipv4": [
"10.9.9.9"
"10.9.3.4"
]
"public_ipv4": [
"127.0.0.1"
"10.1.1.1"
]
"total_mem_mb": 3884
}
"modified": {
"at": 1478195247
"by": 0
}
"name": "ABC-log-01"
"policy_id": "9125663A-04EA-4F1D-A436-ADFEF069D4BA"
"stats": {
"last_day_bytes": 0
"last_update_ts": 0
}
"status": {
"details": []
"status": "ok"
}
}
}
{
"eventlog": {
"created": {
"at": 1499888362
"by": 0
}
"id": "A4D2EA92-5423-1005-B1B3-0050568505BC"
"metadata": {
"os_type": "windows"
"local_hostname": "ABC-log-02"
"local_ipv4": [
"10.8.8.8"
]
"num_logical_processors": 2
"os_details": "Windows Server (R) 2008 Standard; 6.0.6002; Service Pack 2; x86"
"public_ipv4": [
"10.1.1.1"
]
"public_ipv4": [
"10.4.4.4"
]
"total_mem_mb": 2046
}
"modified": {
"at": 1512582221
"by": 13939
}
"name": "ABC-log-03"
"stats": {
"last_day_bytes": 0
}
"status": {
"details": []
"status": "new"
"timestamp": 1508228598
"updated": 1508228598
}
"tags": []
}
}
customer_name: "XYZ"
{
"syslog": {
"created": {
"at": 1507196910
"by": 0
}
"id": "9E47B629-5AC9-1005-B1B3-0050568505BC"
"metadata": {
"host_type": "standalone"
"os_type": "unix"
"version": "1"
"local_hostname": "XYZ-log-01"
"local_ipv6": [
"10.10.1.2"
"10.10.2.3"
]
"num_logical_processors": 4
"os_details": "Linux; 2.6.32-696.6.3.el6.x86_64; #1 SMP Wed Jul 12 14:17:22 UTC 2017; x86_64"
"public_ipv4": [
"10.10.2.2"
"10.2.4.6"
]
"total_mem_mb": 3951
}
"modified": {
"at": 1507196910
"by": 0
}
"name": "XYZ-log-01"
"policy_id": "7135663A-04EA-4F1D-A436-ADFEF069D4BA"
"stats": {
"last_day_bytes": 0
"last_update_ts": 0
}
"status": {
"details": []
"status": "ok"
"timestamp": 1508228712
"updated": 1519723490
}
"tags": []
}
}
{
"eventlog": {
"created": {
"at": 1507196961
"by": 0
}
"id": "2F417043-5AC9-1005-B1B3-0050568505BC"
"metadata": {
"host_type": "standalone"
"os_type": "windows"
"version": "1"
"local_hostname": "XYZ-log-02"
"local_ipv4": [
"10.2.3.4"
]
"num_logical_processors": 2
"os_details": "Windows Server (R) 2008 Standard; 6.0.6002; Service Pack 2; x86"
"public_ipv4": [
"10.2.3.4"
]
"public_ipv6": [
"*.*.*.*"
]
"total_mem_mb": 2046
}
"modified": {
"at": 1507196961
"by": 0
}
"name": "XYZ-log-02"
"stats": {
"last_day_bytes": 0
"last_update_ts": 0
}
"status": {
"details": []
"status": "new"
"timestamp": 1508228722
"updated": 1508228722
}
"tags": []
}
}

All you have to do to print IPs is to replace this block:
{
data[$1] = $2
}
with this one:
{
if ($2 == "[")
list = $1
else if (list && $1 =="]")
list = 0
else if (list)
data[list] = data[list] "," $1
else
data[$1] = $2
}
This will parse all the JSON lists in your data into single strings, in which all list elements will be joined by comma. So this will work not only for local_ipv4, but also for public_ipv6 and others.
And then you just need to get rid of an extra comma. So instead of:
print data["customer_name"], data["os_type"], data["local_hostname"], data["name"], data["local_ipv4"], data["status"]
use substr(data["local_ipv4"],2) function to remove the comma in front:
print data["customer_name"], data["os_type"], data["local_hostname"], data["name"], substr(data["local_ipv4"],2), data["status"]
At least, this is a simple way of doing it based on your code.

Related

unable to parse json into csv using jq

I have a JSON file that I want to convert into a CSV file using the jq in a shell script. I want to create a single row from this entire JSON file. I have to extract value from values. The row output should be something like
null,642,642,412,0,null,null
Here is my JSON file
{
"data": [
{
"name": "exits",
"period": "lifetime",
"values": [
{
"value": {}
}
],
"title": "Exits",
"description": "Number of times someone exited the carousel"
},
{
"name": "impressions",
"period": "lifetime",
"values": [
{
"value": 642
}
],
"title": "Impressions",
"description": "Total number of times the media object has been seen"
},
{
"name": "reach",
"period": "lifetime",
"values": [
{
"value": 412
}
],
"title": "Reach",
"description": "Total number of unique accounts that have seen the media object"
},
{
"name": "replies",
"period": "lifetime",
"values": [
{
"value": 0
}
],
"title": "Replies",
"description": "Total number of replies to the carousel"
},
{
"name": "taps_forward",
"period": "lifetime",
"values": [
{
"value": {}
}
],
"title": "Taps Forward",
"description": "Total number of taps to see this story's next photo or video"
},
{
"name": "taps_back",
"period": "lifetime",
"values": [
{
"value": {}
}
],
"title": "Taps Back",
"description": "Total number of taps to see this story's previous photo or video"
}
]
}
Hi tried using this jq command :
.data | map(.values[].value) | #csv
This is giving the following output:
jq: error (at :70): object ({}) is not valid in a csv row
exit status 5
So when I am getting this empty JSON object it is reflecting an error.
Please Help!!
The row output should be something like
null,642,642,412,0,null,null
Using length==0 here is dubious at best. To check for {} one could write:
jq '.data | map(.values[].value | if . == {} then "null" else . end) | #csv'
Similarly for [].
If you run the command without the #csv part you will see that the output is:
[
{},
642,
412,
0,
{},
{}
]
By replacing the empty objects with "null": (length == 0)
jq '.data | map(.values[].value) | map(if (type == "object" and length == 0 ) then "null" else . end) | #csv'
Output:
"\"null\",642,412,0,\"null\",\"null\""
Per suggestion from #aaron (see comment). The following can produce the requested output without extra post-processing. Disclaimer: this is not working with my jq 1.5, but working on jqplay with jq 1.6.
jq --raw-output '.data | map(.values[].value) | map(if (type == "object" and length == 0 ) then "null" else . end) | join(",")'
Output:
null,642,412,0,null,null

how to sort Data Sources in terraform based on arguments

I use following terraform code to get a list of available db resources:
data "alicloud_db_instance_classes" "resources" {
instance_charge_type = "PostPaid"
engine = "PostgreSQL"
engine_version = "10.0"
category = "HighAvailability"
zone_id = "${data.alicloud_zones.rds_zones.ids.0}"
multi_zone = true
output_file = "./classes.txt"
}
And the output file looks like this:
[
{
"instance_class": "pg.x4.large.2",
"storage_range": {
"max": "500",
"min": "250",
"step": "250"
},
"zone_ids": [
{
"id": "cn-shanghai-MAZ1(b,c)",
"sub_zone_ids": [
"cn-shanghai-b",
"cn-shanghai-c"
]
}
]
},
{
"instance_class": "pg.x8.medium.2",
"storage_range": {
"max": "250",
"min": "250",
"step": "0"
},
"zone_ids": [
{
"id": "cn-shanghai-MAZ1(b,c)",
"sub_zone_ids": [
"cn-shanghai-b",
"cn-shanghai-c"
]
}
]
},
{
"instance_class": "rds.pg.c1.xlarge",
"storage_range": {
"max": "2000",
"min": "5",
"step": "5"
},
"zone_ids": [
{
"id": "cn-shanghai-MAZ1(b,c)",
"sub_zone_ids": [
"cn-shanghai-b",
"cn-shanghai-c"
]
}
]
},
{
"instance_class": "rds.pg.s1.small",
"storage_range": {
"max": "2000",
"min": "5",
"step": "5"
},
"zone_ids": [
{
"id": "cn-shanghai-MAZ1(b,c)",
"sub_zone_ids": [
"cn-shanghai-b",
"cn-shanghai-c"
]
}
]
}
]
And I want to get the one that's cheapest.
One way to do so is by sorting with storage-range.min, but how do I sort this list based on 'storage_range.min'?
Or I can filter by 'instance_class', but "alicloud_db_instance_classes" doesn't seem to like filter as it says: Error: data.alicloud_db_instance_classes.resources: : invalid or unknown key: filter
Any ideas?
The sort() function orders lexicographical and you have no simple key here.
You can use filtering with some code like this (v0.12)
locals {
best_db_instance_class_key = "rds.pg.s1.small"
best_db_instance_class = element( alicloud_db_instance_classes.resources, index(alicloud_db_instance_classes.resources.*.instance_class, best_db_instance_class_key) )
}
(Untested code)

Elasticsearch partial update of Object(multi=True)

How to update document with field mapping Object(multi=True),
when a document can have both single (dictionary) and multiple values (list of dictionaries).
Example of documents in the same index:
A single value in items:
{
"title": "Some title",
"items": {
"id": 123,
"key": "foo"
}
}
Multiple values in items:
{
"title": "Some title",
"items": [{
"id": 456,
"key": "foo"
}, {
"id": 789,
"key": "bar"
}]
}
You can try to use the following script.
I intentionally formatted inline attribute to show what's inside.
POST index_name/_update_by_query
{
"search": {
"term": {
"items.key": "foo"
}
},
"script": {
"inline": "
if (ctx._source.items instanceof List) {
for (item in ctx.source.items) {
if (item.key == params.old_value) {
item.key = params.new_value;
break;
}
}
} else {
ctx._source.items.key = params.new_value;
}
",
"params": {"old_value": "foo", "new_value": "bar"},
"lang": "painless'
}
}
And to make it work, replace inline attribute with a single line value.
"inline": "if (ctx._source.items instanceof List) {for (item in ctx.source.items) {if (item.key == params.old_value) {item.key = params.new_value;break;}}} else {ctx._source.items.key = params.new_value;}"

Update single value in sub sub array in RethinkDB

We are trying to update a single answer in our sub sub array.
However our query is causing the following error:
{
"deleted": 0 ,
"errors": 1 ,
"first_error": "Inserted value must be an OBJECT (got ARRAY):
[
{
"answers": [
{
"answer": "wassup",
"owner": 12201836
}
],
"question": "Vraag 1?",
"questionId": 0,
"time": "10"
},
{
"answers": [],
"question": "Vraag 2?",
"questionId": 1,
"time": "15"
},
{
"answers": [],
"question": "Vraga 3?",
"questionId": 2,
"time": "20"
}
]" ,
"inserted": 0 ,
"replaced": 0 ,
"skipped": 0 ,
"unchanged": 0
}
Our table structure looks like the following:
Youtube
- Id
- Course
- Unit
- Session
- Number
- Group
- Questions (array)
- Question Id
- Time
- Answers (array)
- Id
- Answer
- Owner
Our query:
r.db('GitSmurf')
.table('youtube')
.update(function (row) {
return row('questions').merge(function (q) {
return r.branch(q('questionId').eq(0), { "answers": q('answers').merge(function(answer) {
return r.branch(answer('owner').eq(12201836), {"answer": "wassup"}, {})} )},{})
})
})
Test content:
{
"completed": [ ],
"course": "swd" ,
"group": "dwa-group-b" ,
"id": "44443377-ed15-4358-a005-f561e7b6a42d" ,
"number": 1 ,
"session": 1 ,
"unit": 1,
"questions": [
{
"answers": [
{
"answer": "hallo" ,
"owner": 12201836
}
] ,
"question": "Vraag 1?" ,
"questionId": 0 ,
"time": "10"
} ,
{
"answers": [ ],
"question": "Vraag 2?" ,
"questionId": 1 ,
"time": "15"
} ,
{
"answers": [ ],
"question": "Vraga 3?" ,
"questionId": 2 ,
"time": "20"
}
] ,
}
Any help is greatly appreciated!
We forgot to return a new object in the update query.
When we added that it worked.
r.db('GitSmurf')
.table('youtube')
.update(function (row) {
return { questions: row('questions').merge(function (q) {
return r.branch(q('questionId'), { "answers": q('answers').merge(function(answer) {
return r.branch(answer('owner').eq(12201836), {"answer": "tom"}, {})
})},{})
})}
})

Rethinkdb: Calculate tag occurrence per user

My table contains documents that look like this:
[{ user: {
key: '100'
},
product: {
name: 'Product 1',
tags: [ 'tag1', 'tag2' ],
}
}, { user: {
key: '100'
},
product: {
name: 'Product 1',
tags: [ 'tag1', 'tag3' ],
}
}, ...]
I would like to create a query which would
groupe documents by the user.key field (1 document per user on result),
the product.tags would be an object (instead of array) with tag occurrences count for each tag.
Result example:
[ { user: {
key: '100'
},
product: {
name: 'Product 1',
tags: {
tag1: 2, // tag1 found 2x for user.key=100
tag2: 1, // tag2 found 1x for user.key=100
tag3: 1
}
}
}, ...]
I think I could do this by mapping and reducing but I have problems - I'm using rethinkdb for the first time.
Here's a way to do it:
// Group by user key
r.table('30400911').group(r.row('user')('key'))
// Only get the product info inside the reduction
.map(r.row('product'))
.ungroup()
.map(function (row) {
return {
user: row('group'),
// Group by name
products: row('reduction').group('name').ungroup().map(function (row) {
return {
name: row('group'),
// Convert array of tags into key value pairs
tags: r.object(r.args(row('reduction').concatMap(function (row) {
return row('tags')
}).group(function (row) {
return row;
}).count().ungroup().concatMap(function (row) {
return [row('group'), row('reduction')]
})))
}
})
}
})
For the following data:
{
"id": "0565e91a-01ca-4ba3-b4d5-1043c918c79d" ,
"product": {
"name": "Product 2" ,
"tags": [
"tag1" ,
"tag3"
]
} ,
"user": {
"key": "100"
}
} {
"id": "39999c9f-bbef-4cb7-9311-2516ca8f9ba1" ,
"product": {
"name": "Product 1" ,
"tags": [
"tag1" ,
"tag3"
]
} ,
"user": {
"key": "100"
}
} {
"id": "566f3b79-01bf-4c29-8a9c-fd472431eeb6" ,
"product": {
"name": "Product 1" ,
"tags": [
"tag1" ,
"tag2"
]
} ,
"user": {
"key": "100"
}
} {
"id": "8e95c467-cedc-4734-ad4d-a1f7a371efd5" ,
"product": {
"name": "Product 1" ,
"tags": [
"tag1" ,
"tag2"
]
} ,
"user": {
"key": "200"
}
}
The results would be:
[
{
"products": [
{
"name": "Product 1" ,
"tags": {
"tag1": 2 ,
"tag2": 1 ,
"tag3": 1
}
}, {
"name": "Product 2" ,
"tags": {
"tag1": 1 ,
"tag3": 1
}
}
],
"user": "100"
} ,
{
"products": [
{
"name": "Product 1" ,
"tags": {
"tag1": 1 ,
"tag2": 1
}
}
] ,
"user": "200"
}
]

Resources