mirror of
https://github.com/jbranchaud/til
synced 2026-01-07 09:08:01 +00:00
Compare commits
2 Commits
5a72dcfb95
...
80f502f661
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
80f502f661 | ||
|
|
a09ec39aa1 |
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||
|
||||
_1289 TILs and counting..._
|
||||
_1291 TILs and counting..._
|
||||
|
||||
---
|
||||
|
||||
@@ -488,6 +488,7 @@ _1289 TILs and counting..._
|
||||
- [Count Each Collection In A JSON Object](jq/count-each-collection-in-a-json-object.md)
|
||||
- [Count The Number Of Things In A JSON File](jq/count-the-number-of-things-in-a-json-file.md)
|
||||
- [Extract A List Of Values](jq/extract-a-list-of-values.md)
|
||||
- [Find All Objects In An Array Where Key Is Set](jq/find-all-objects-in-an-array-where-key-is-set.md)
|
||||
- [Find All Objects With A Matching Key Value Pair](jq/find-all-objects-with-a-matching-key-value-pair.md)
|
||||
- [Reduce Object To Just Entries Of A Specific Type](jq/reduce-object-to-just-entries-of-a-specific-type.md)
|
||||
|
||||
@@ -502,6 +503,7 @@ _1289 TILs and counting..._
|
||||
- [Configure Your Server Timezone](linux/configure-your-server-timezone.md)
|
||||
- [List The Statuses Of All Upstart Jobs](linux/list-the-statuses-of-all-upstart-jobs.md)
|
||||
- [Show Current System Time And Settings](linux/show-current-system-time-and-settings.md)
|
||||
- [Show Used And Available System Memory](linux/show-used-and-available-system-memory.md)
|
||||
- [Upgrading Ubuntu](linux/upgrading-ubuntu.md)
|
||||
|
||||
### Mac
|
||||
|
||||
50
jq/find-all-objects-in-an-array-where-key-is-set.md
Normal file
50
jq/find-all-objects-in-an-array-where-key-is-set.md
Normal file
@@ -0,0 +1,50 @@
|
||||
# Find All Objects In An Array Where Key Is Set
|
||||
|
||||
Let's say we have a large array of objects. And the data in those objects,
|
||||
while generally having the same shape, does not always have certain values set.
|
||||
|
||||
For instance, here is some data where the `token` is sometimes set, sometimes
|
||||
`null`, and sometimes missing altogether.
|
||||
|
||||
```json
|
||||
[
|
||||
{
|
||||
"id": 1,
|
||||
"token": "abc"
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"token": null
|
||||
},
|
||||
{
|
||||
"id": 5
|
||||
},
|
||||
...
|
||||
]
|
||||
```
|
||||
|
||||
We can find out how many objects in this collection have the `token` key set to
|
||||
an actual value with [a `jq` query](https://stedolan.github.io/jq/manual) like
|
||||
the following.
|
||||
|
||||
```bash
|
||||
jq '. | map(select(has("token") and .token != null)) | length' data.json
|
||||
```
|
||||
|
||||
This maps over each object selecting those where it has `token` and `token` is
|
||||
not `null`.
|
||||
|
||||
We can instead produce the inverse count—those objects where `token` is not set
|
||||
to a value—with the `not` operator.
|
||||
|
||||
```bash
|
||||
jq '. | map(select(has("token") and .token != null | not)) | length' data.json
|
||||
```
|
||||
|
||||
If you want to inspect the array of objects that either of these queries filters down to, you can drop the `| length` part.
|
||||
|
||||
```bash
|
||||
jq '. | map(select(has("token") and .token != null))' data.json
|
||||
```
|
||||
|
||||
Here is [a live example](https://jqterm.com/?query=.%20%7C%20map%28select%28has%28%22token%22%29%20and%20.token%20!%3D%20null%20%7C%20not%29%29).
|
||||
24
linux/show-used-and-available-system-memory.md
Normal file
24
linux/show-used-and-available-system-memory.md
Normal file
@@ -0,0 +1,24 @@
|
||||
# Show Used And Available System Memory
|
||||
|
||||
The `free` command will display the total RAM on your system as well as how
|
||||
much of it is used and how much is still freely available.
|
||||
|
||||
```bash
|
||||
$ free
|
||||
total used free shared buff/cache available
|
||||
Mem: 1000260 166004 552808 14440 281448 664724
|
||||
Swap: 0 0 0
|
||||
```
|
||||
|
||||
The default output format of `free` is not very readable though. To make it
|
||||
more human readable, you can add the `-h` (_human_) and `--si` (_International
|
||||
System of Units_) flags.
|
||||
|
||||
```bash
|
||||
$ free -h --si
|
||||
total used free shared buff/cache available
|
||||
Mem: 976M 162M 547M 14M 266M 649M
|
||||
Swap: 0B 0B 0B
|
||||
```
|
||||
|
||||
See `man free` for more details.
|
||||
Reference in New Issue
Block a user