I am trying to replace the previous line of a string match.
Example json
"test" : {
"aa" : true,
"ac" : "port",
"tr" : "p2",
"ll" : 90,
"mp" : true
}
If "ll" equals 90, I need to change the previous line to "mu" : "p1". I have tried the below sed but it always replaces the same line instead on the previous line. Please suggest
sed -e '/"ll" : 90/!b;!N;c"mu" : "p1"'
You can try this sed:
sed 'N;/\n *"ll" : 90/{s/^\([^"]*\).*\n/\1"mu" : "p1",\n/;};P;D' file
As suggested by #potong,
sed -r 'N;s/.*(\n(\s*)"ll" : 90,)/\2"mu : "p1",\1/;P;D' file
Output:
"test" : {
"aa" : true,
"ac" : "port",
"mu" : "p1",
"ll" : 90,
"mp" : true
}
Related
I used switch statement to assign some text based on different value ; here it returns correct output.
Now from the above output ,again I need to filter out few documents using regular expression.
Some thing like "where status likes 'open%'"
db.accountMailLog.aggregate([
{$match:{'$and': [{recipientId: "3003590"}, {recipientType: "2"}
]}},
{$project :
{
'_id' : 0,
'logCreated' : '$eventOccurredTime',
'subject' : 1,
'resourceType' : 'email',
'campaignName' : 1,
'emailSendDay' : 1,
'logDate' : '$eventOccurredTime',
'sentMethod' : 'campaign-email',
'visitorKey' : '',
'mediaHash' : '',
"status" : {'$switch' :
{'branches' :
[
{'case' : {'$eq' : ['$status','1']},'then' : "delivered"},
{'case' : {'$eq' : ['$status','2']},'then' : "opened"},
{'case' : {'$eq' : ['$status','3']},'then' : "clicked"},
{'case' : {'$eq' : ['$status','4']},'then' : "bounced"},
{'case' : {'$eq' : ['$status','5']},'then' : "unsubscribed"},
{'case' : {'$eq' : ['$status','6']},'then' : "complained"}
]
}
}
}},
]);
Thanks !!!
You can use Regexp. Something like:
"status": open/
I have this output from urlread function:
// [
{
"id": "22144"
,"t" : "AAPL"
,"e" : "NASDAQ"
,"l" : "148.59"
,"l_fix" : "148.59"
,"l_cur" : "148.59"
,"s": "0"
,"ltt":"1:13PM EDT"
,"lt" : "May 5, 1:13PM EDT"
,"lt_dts" : "2017-05-05T13:13:23Z"
,"c" : "+2.06"
,"c_fix" : "2.06"
,"cp" : "1.41"
,"cp_fix" : "1.41"
,"ccol" : "chg"
,"pcls_fix" : "146.53"
,"eo" : ""
,"delay": ""
,"op" : "146.76"
,"hi" : "148.91"
,"lo" : "146.76"
,"vo" : "-"
,"avvo" : "-"
,"hi52" : "148.91"
,"lo52" : "89.47"
,"mc" : "771.93B"
,"pe" : "17.38"
,"fwpe" : ""
,"beta" : "1.21"
,"eps" : "8.55"
,"shares" : "5.21B"
,"inst_own" : "63%"
,"name" : "Apple Inc."
,"type" : "Company"
}
]
My question is how can I convert this to a two-column cell? Or even better create a structure called AAPL which gives me , for example for AAPL.l the price?
Use jsondecode function to convert JSON format text to a MATLAB struct type. Typically the text would start with a '[' or '{'. You can try code using a simpler subset as below.
jsondecode('{"id": "22144","t" : "AAPL","e" : "NASDAQ","l" : "148.59"}')
This produces a struct with the following fields.
id: '22144'
t: 'AAPL'
e: 'NASDAQ'
l: '148.59'
I've searched a lot but still can't find (or understand) the answer. I have a collection in my mongodb database that is called "btest". Inside of that collection I have a list of randomly generated strings (from 1000 to 1000000) that look something like this:
> db.btest.find()
{ "_id" : ObjectId("5818ed42c33b12a7c902cd34"), "0" : 1, "wgickjkwxfimleot" : "r
scjuvarvmvuheom" }
{ "_id" : ObjectId("5818ed42c33b12a7c902cd35"), "0" : 2, "wgickjkwxfimleot" : "t
gdqnegjscsmnjsi" }
{ "_id" : ObjectId("5818ed42c33b12a7c902cd36"), "0" : 4, "wgickjkwxfimleot" : "d
qjvndthelmtqknj" }
{ "_id" : ObjectId("5818ed42c33b12a7c902cd37"), "0" : 5, "wgickjkwxfimleot" : "u
qtmbuhgwxntcixh" }
{ "_id" : ObjectId("5818ed42c33b12a7c902cd38"), "0" : 6, "wgickjkwxfimleot" : "i
rguwjvectjvimjk" }
{ "_id" : ObjectId("5818ed42c33b12a7c902cd39"), "0" : 7, "wgickjkwxfimleot" : "n
sggjpodfvebjumk" }
{ "_id" : ObjectId("5818ed42c33b12a7c902cd3a"), "0" : 8, "wgickjkwxfimleot" : "a
wvjtlxtoqwpdltp" }
I imported these using this command:
mongoimport -d ee_db -c btest --type csv --file "C:\Users\USER\Desktop\Random\projekty java\ee_bulk_insert\src\10000.csv" --headerline
What I want to do is to change all the strings in this collection to uppercase letters. In MySQL I've done the same thing with the "SELECT UCASE(row) FROM btest;" command.
How to achieve the same result in MongoDB? Thanks for all the answers.
You can use an aggregation with a $project using $toUpper to convert string to uppercase and then write the results in a new collection with $out :
db.btest.aggregate(
[{
$project: {
"0": 1,
"wgickjkwxfimleot": { $toUpper: "$wgickjkwxfimleot" }
}
}, {
$out: "results"
}]
)
I am trying to modify a file content, which has a specific structure:
{
"name" : "thename",
"value" : {
"a" : "123",
"b" : "456"
}
}
I would like to replace the content of a or b; but I can only append to the values, not replace what is in the quotes. For example instead of 123 for a, I would like to put 999.
sed -i 's/"a":*/"a": "999"/g' myfile.txt
But what I get is
{
"name" : "thename",
"value" : {
"a" : "999" "123",
"b" : "456"
}
}
How do I tell sed that I want to replace what is in the quote after that string, and not append to it?
Try:
$ sed 's/"a" *:.*/"a" : "999"/' myfile.txt
{
"name" : "thename",
"value" : {
"a" : "999"
"b" : "456"
}
}
The issue is that sed uses regular expressions, not globs. As a consequence, "a":* matches "a" followed by zero or more colons. Since, in your sample file above, "a" is followed by a space, this matches just "a".
In the code above, "a":* is replaced with "a" *:.*/ which matches "a" followed by zero or more spaces, followed by a colon, and .* which matches everything that follows the colon. In a regular expression, . matches any character and .* therefore matches zero or more of any character. This will match the entirety of the line that follows the colon.
I would like to know if there is a way to get the hostname of the server which has the problem through MongoDB MMS. By default they give only following details.
{
u'status': u'OPEN',
u'updated': u'2015-04-14T03: 24: 33Z',
u'links': [
{
u'href': u'https: //mms.mongodb.com/api/public/v1.0/groups/9b5ba/alerts/5521',
u'rel': u'self'
}
],
u'created': u'2015-04-14T03: 24: 33Z',
u'lastNotified': u'2015-04-14T03: 24: 41Z',
u'alertConfigId': u'0927',
u'eventTypeName': u'HOST_DOWN',
u'groupId': u'5baa',
u'typeName': u'HOST',
u'id': u'1012'
}
Try this:
curl -u "username:apiKey" --digest -i "https://mms.mongodb.com/api/public/v1.0/groups/533c5895b91030606f21033a/hosts/56e9378f601dc49360a40949c8a6df6c"
Founded this code from this link: https://docs.mms.mongodb.com/reference/api/hosts/
See, if that helps.
Please find this link as well : http://www.litixsoft.de/english/mms/
Hope below will make sense to your question
casinomongodb:PRIMARY> db.isMaster(); {
"setName" : "casinomongodb",
"setVersion" : 31173,
"ismaster" : true,
"secondary" : false,
"hosts" : [
"10.1.243.66:27017",
"10.1.243.62:27017",
"10.1.243.61:27017"
],
"primary" : "10.1.243.62:27017",
"me" : "10.1.243.62:27017",
"electionId" : ObjectId("56c5657c8994dff92e8996fa"),
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 1000,
"localTime" : ISODate("2016-04-06T09:19:03.077Z"),
"maxWireVersion" : 3,
"minWireVersion" : 0,
"ok" : 1 }