Grep and cut command in putty terminal - shell

I'm trying to cut a specific line from several files.
I have about 300 json files.
file structure:
{
"FieldName1": "XXXX",
"FieldName2": "1",
"FieldName3": "XXX",
"FieldName4": "XXX",
"FieldName5": "1",
.
.
.
}
,
{
"FieldName1": "XXXX",
"FieldName2": "2",
"FieldName3": "XXX",
"FieldName4": "XXX",
"FieldName5": "2",
.
.
.
}
some of the files are written in one row:
{"FieldName1": "XXXX", "FieldName2": "3", "FieldName3": "XXX", "FieldName4": "XXX", "FieldName5": "3"}, {"FieldName1": "XXXX", "FieldName2": "4", "FieldName3": "XXX", "FieldName4": "XXX", "FieldName5": "4"}, ...
}
What i need to get in my output file is just a list of 2 fields from the files :
"FieldName1": "1",
"FieldName2": "1",
"FieldName1": "2",
"FieldName2": "2",
"FieldName1": "3",
"FieldName2": "3",
"FieldName1": "4",
"FieldName2": "4",
.
.
.
Is there an easy way to do so?
Thanks!

For the single line, you can use this gnu awk (do to multiple characters in RS)
awk -v RS='"FieldName' 'NR>1 {sub(/, "/,",");print RS$0}' file
"FieldName1": "XXXX",
"FieldName2": "3",
"FieldName3": "XXX",
"FieldName4": "XXX",
For multiple line use grep or awk like this:
awk '/FieldName/' file
"FieldName1": "XXXX",
"FieldName2": "1",
"FieldName3": "XXX",
"FieldName4": "XXX",
"FieldName5": "1",

Related

How to select related events from the same table on Elasticsearch?

How do I make the next self query on Elasticsearch?
SELECT e.user_id AS user_id,
e.datetime AS started_at,
(SELECT MIN(datetime) ## taking the closest "end" event datetime of that userId ##
FROM events
WHERE type = "end" AND
user_id = e.user_id AND
datetime > e.datetime) AS end_at,
FROM events AS e
WHERE e.type = "start"
Over the next event data table:
{"_id" : "1", "type": "start", "datetime": "2022-02-01T10:15Z", "userId": "1"},
{"_id" : "2", "type": "end", "datetime": "2022-02-01T10:20Z", "userId": "1"},
{"_id" : "3", "type": "start", "datetime": "2022-02-01T10:16Z", "userId": "2"},
{"_id" : "4", "type": "end", "datetime": "2022-02-01T10:21Z", "userId": "2"},
{"_id" : "5", "type": "start", "datetime": "2022-02-02T11:01Z", "userId": "1"},
{"_id" : "6", "type": "end", "datetime": "2022-02-02T11:02Z", "userId": "1"}
The expected result should look like:
user_id
started_at
end_at
1
2022-02-01T10:15Z
2022-02-01T10:20Z
2
2022-02-01T10:16Z
2022-02-01T10:21Z
1
2022-02-02T11:01Z
2022-02-02T11:02Z

Ruby string to array conversion using 'scan' function

I have a string:
"10/2+3*3/2-3/2+10"
I need to convert it to an array:
["10", "/", "2", "+", "3", "*", "3", "/", "2", "-", "3", "/", "2", "+", "10"]
without using any gem.
I am using the following code to try to convert it:
"10/2+3*3/2-3/2+10".scan(/[\d*,+,\-,*,\/]/)
# => ["1", "0", "/", "2", "+", "3", "*", "3", "/", "2", "-", "3", "/", "2", "+", "1", "0"]
The output is not what I was expecting.
Use String#split
"10/2+3*3/2-3/2+10".split(/(\D)/)
#=> ["10", "/", "2", "+", "3", "*", "3", "/", "2", "-", "3", "/", "2", "+", "10"]
Commas inside square brackets in regular expression are treated as commas. What you want is the union of patterns.
"10/2+3*3/2-3/2+10".scan(/\d+|\+|\*|\/|\-/)
"10/2+3*3/2-3/2+10".scan(/\d+|./)
#=> ["10", "/", "2", "+", "3", "*", "3", "/", "2", "-", "3", "/", "2", "+", "10"]

In d3 4.0, how can I get extra data from the stratified object?

I'm a new to D3 also stackoverflow, and have some question.
[
{"id": "1", "name": "Eve", "parent": "" , "txt": "text1."},
{"id": "2", "name": "Cain", "parent": "1", "txt": "text2."},
{"id": "3", "name": "Seth", "parent": "1", "txt": "text3."},
{"id": "4", "name": "Enos", "parent": "3", "txt": "text4."},
{"id": "5", "name": "Noam", "parent": "3", "txt": "text5."},
{"id": "6", "name": "Abel", "parent": "1", "txt": "text6."},
{"id": "7", "name": "Awan", "parent": "1", "txt": "text7."},
{"id": "8", "name": "Enoch", "parent": "7", "txt": "text8."},
{"id": "9", "name": "Azura", "parent": "1", "txt": "text9."}
];
Above is array object which is not stratified yet.
below is the stratified result and code.
var stratify = d3.stratify()
.id(function(d){ return d.id; })
.parentId(function(d){ return d.parent; });
stratified object
I want to use the data object, like below code
node.append("text")
.text(function(d) { return d.name; });
but, above doesn't work.
refered to https://github.com/d3/d3-hierarchy/blob/master/README.md#stratify
and I edited the array object a little.
thanks & best regards. and It would be greatly appreciated if you could give me a feedback.
Finally, I found the answer.
return d.data.name
what an embarrassingly amateurish thing...

Get particular key's and value's from JSON array - Ruby [duplicate]

This question already has answers here:
Ruby: Easiest Way to Filter Hash Keys?
(14 answers)
Closed 6 years ago.
I am a beginner with Ruby, and I have the following Json array:
"elements": [
{
"type": "Contact",
"id": "1",
"createdAt": "131231235",
"name": "test",
"updatedAt": "1456328049",
"accountName": "Mr Test",
"country": "China",
"firstName": "Test",
"lastName": "lastNameTest",
},
{
"type": "Contact",
"id": "2",
"createdAt": "156453447",
"name": "test2",
"updatedAt": "124464554",
"accountName": "Mr Test2",
"country": "Germany",
"firstName": "Test2",
"lastName": "lastNameTest2",
},...
]
I want to filter out only a few keys + values: for example I want to return only the id,name,accountName,firstname and lastname.
So the exspected output is the following:
"elements": [
{
"id": "1",
"name": "test",
"accountName": "Mr Test",
"firstName": "Test",
"lastName": "lastNameTest",
},
{
"id": "2",
"name": "test2",
"accountName": "Mr Test2",
"firstName": "Test2",
"lastName": "lastNameTest2",
},...
]
I tried the following: create a filter array which has the elements I want to return and then map over the items but then I get stuck..
filters = []
filters.push("accountName")
filters.push("lastName")
filters.push("firstName")
filters.push("Id")
output["elements"].each do |item|
result = []
item.map {|key,value|filters.include? key}
result.push(?)
Thank you for the help.
Check this out, you should be able to work out from this:
output = { "elements": [
{
"id": "1",
"name": "test",
"accountName": "Mr Test",
"firstName": "Test",
"lastName": "lastNameTest",
"somethoong": "sdsad"
},
{
"id": "2",
"name": "test2",
"accountName": "Mr Test2",
"firstName": "Test2",
"lastName": "lastNameTest2"
}
]}
attribs = %w(accountName lastName firstName id)
output[:elements].each do |item|
item.delete_if{|k,v| !attribs.include?(k.to_s)}
end

Eloquent Order By alphabetically doesn't work

Here is a very simple line of code I am running:
return User::orderBy('first_name', 'asc')->get();
I expect to get the returned result in alphabetical order. Here is what I get:
{
"id": "1",
"first_name": "Kousha",
...
},
{
"id": "10",
"first_name": "Monica",
...
},
{
"id": "9",
"first_name": "Alex",
...
},
{
"id": "2",
"first_name": "Ali",
...
},
{
"id": "7",
"first_name": "Chris",
...
},
{
"id": "5",
"first_name": "Amin",
...
},
{
"id": "6",
"first_name": "Felix",
...
},
{
"id": "8",
"first_name": "Aleksi",
...
},
{
"id": "3",
"first_name": "Ryan",
...
},
{
"id": "4",
"first_name": "Paul",
...
}
If I try the order in descending order, I get another gibberish result. The only thing the order works for is the id. What am I missing here?

Resources