シェルスクリプトの Tips
jq コマンド#
- JSON をいじる際に便利なコマンド
- 参考にしたページ:
- 以下、例として以下のようなデータ(
example.json)を扱うとする
1[
2 {
3 "id": "0001",
4 "name": "test001",
5 "value": 112,
6 "group1": {
7 "subg01": [
8 {
9 "id": "1001",
10 "type": "type001"
11 },
12 {
13 "id": "1004",
14 "type": "type002"
15 }
16 ]
17 },
18 "group2": [
19 {
20 "id": "5001",
21 "type": "None"
22 },
23 {
24 "id": "5003",
25 "type": "type053"
26 },
27 {
28 "id": "5004",
29 "type": "type054"
30 }
31 ]
32 },
33 {
34 "id": "0002",
35 "name": "test002",
36 "value": 954,
37 "group1": {
38 "subg01": [
39 {
40 "id": "1021",
41 "type": "type101"
42 },
43 {
44 "id": "1054",
45 "type": "type052"
46 }
47 ]
48 },
49 "group2": [
50 {
51 "id": "5001",
52 "type": "None"
53 },
54 {
55 "id": "5004",
56 "type": "type054"
57 }
58 ]
59 }
60]select でフィルタリング#
1$ cat example.json | jq '.[] | select(.id == "0001")'
2{
3 "id": "0001",
4 "name": "test001",
5 "value": 112,
6 "group1": {
7 "subg01": [
8 {
9 "id": "1001",
10 "type": "type001"
11 },
12 {
13 "id": "1004",
14 "type": "type002"
15 }
16 ]
17 },
18 "group2": [
19 {
20 "id": "5001",
21 "type": "None"
22 },
23 {
24 "id": "5003",
25 "type": "type053"
26 },
27 {
28 "id": "5004",
29 "type": "type054"
30 }
31 ]
32}シェルで変数の空文字判定#
- 空じゃないときのみ処理を行う場合の条件の書き方
- 空のときのみ処理を行う場合の条件の書き方
- ↓以外では
elseを使う手もある。
- ↓以外では
空行判定#
- とあるものを動かしているときに
[: ']' expectedとか出てエラーになった。 - 行き当たった記事が これ
変数に格納されている文字列比較において、 文字列の比較は文字列と行う必要があるところを、 文字列と何かわからない型の変数を比較してしまっているので、 このようなエラーが発生するらしい。
- 以下ではダメ。
- 変数を
" "で囲うと良い。
ここに掲載する記事はまだありません。
