如何在select和test函数内部传递参数?

时间:2019-09-02 20:27:25

标签: bash jq

我有从qbittorrent中提取的JSON数据:

[
  {
    "hash": "333333333333333333333333333",
    "name": "testtosearchcaseinsensitive",
    "magnet_uri": "magnet:somedata",
    "size": 1243989552,
    "progress": 1.0,
    "dlspeed": 0,
    "upspeed": 0,
    "priority": 0,
    "num_seeds": 0,
    "num_complete": 2,
    "num_leechs": 0,
    "num_incomplete": 32,
    "ratio": 0.0,
    "eta": "1.01:11:52",
    "state": "stalledUP",
    "seq_dl": false,
    "f_l_piece_prio": false,
    "category": "category",
    "tags": "",
    "super_seeding": false,
    "force_start": false,
    "save_path": "/data/path/",
    "added_on": 1567358333,
    "completion_on": 1567366287,
    "tracker": "somedata",
    "dl_limit": null,
    "up_limit": null,
    "downloaded": 1244073666,
    "uploaded": 0,
    "downloaded_session": 0,
    "uploaded_session": 0,
    "amount_left": 0,
    "completed": 1243989552,
    "ratio_limit": 1.0,
    "seen_complete": 1567408837,
    "last_activity": 1567366979,
    "time_active": "1.01:00:41",
    "auto_tmm": true,
    "total_size": 1243989552,
    "max_ratio": 1,
    "max_seeding_time": 2880,
    "seeding_time_limit": 2880
  },
  {
    "hash": "44444444444444",
    "name": "dontmatch",
    "magnet_uri": "magnet:somedata",
    "size": 2996838603,
    "progress": 1.0,
    "dlspeed": 0,
    "upspeed": 0,
    "priority": 0,
    "num_seeds": 0,
    "num_complete": 12,
    "num_leechs": 0,
    "num_incomplete": 0,
    "ratio": 0.06452786606740063,
    "eta": "100.00:00:00",
    "state": "stalledUP",
    "seq_dl": false,
    "f_l_piece_prio": false,
    "category": "category",
    "tags": "",
    "super_seeding": false,
    "force_start": false,
    "save_path": "/data/path/",
    "added_on": 1566420155,
    "completion_on": 1566424710,
    "tracker": "some data",
    "dl_limit": null,
    "up_limit": null,
    "downloaded": 0,
    "uploaded": 193379600,
    "downloaded_session": 0,
    "uploaded_session": 0,
    "amount_left": 0,
    "completed": 2996838603,
    "ratio_limit": -2.0,
    "seen_complete": 4294967295,
    "last_activity": 1566811636,
    "time_active": "10.23:07:42",
    "auto_tmm": true,
    "total_size": 2996838603,
    "max_ratio": -1,
    "max_seeding_time": -1,
    "seeding_time_limit": -2
  }
]

因此,我想匹配名称中包含一些文本的所有数据,因此,在Bash中我编写了此代码,但我无法使其正常工作。

一些声明开始了,实际上我是通过参数传递数据的,所以我使用$1

TXTIWANT="test"
MYJSONDATA= Here I put my JSON data

然后这个不适用于我的jq公式是这样的:

RESULTS=$(echo "$MYJSONDATA" | jq --raw-output --arg TOSEARCH "$TXTIWANT" '.[] | select(.name|test("$TOSEARCH.";"i")) .name')

但是我一直认为错误或所有数据都是错误的,因为$TOSEARCH没有展开。

也许有更好的方法来搜索值内的字符串?

我做错了什么?

2 个答案:

答案 0 :(得分:1)

使用jq进行变量(或过滤器)插值的正确语法如下:

"foo \(filter_or_var) bar"

在您的情况下:

jq --raw-output --arg TOSEARCH "$TXTIWANT" '.[]select(.name|test("\($TOSEARCH).";"i")) .name')

旁注:By convention, environment variables (PAGER, EDITOR, ...) and internal shell variables (SHELL, BASH_VERSION, ...) are capitalized. All other variable names should be lower case.

答案 1 :(得分:1)

如果(如名称TXTIWANT和示例所建议以及问题的措词所建议的)“ $ TXTIWANT”的值应为原义文字,则使用test是有问题的,因为test将搜索正则表达式。

由于尚不清楚为什么要在TOSEARCH中添加句点,因此在第一部分的其余部分中,我将忽略您对此的任何要求。

因此,如果您只是想查找字面上包含$ TXTIWANT的.name值(忽略大小写),则可以将.name和$ TXTIWANT的值都转换为相同的大小写,然后检查收容措施。

在jq中,忽略神秘的“。”,可以这样做:



jq --raw-output --arg TOSEARCH "$TXTIWANT" '
  ($TOSEARCH|ascii_upcase) as $up
  | .[]
  | .name
  | select(ascii_upcase|index($up))'

搜索$ TXTIWANT的非最终出现而忽略大小写

如果是“。”表示$ TXTIWANT之后必须有一个附加字符,那么您可以如下添加另一个select

  ($TOSEARCH|length) as $len
  | ($TOSEARCH|ascii_upcase) as $up
  | .[]
  | .name
  | (ascii_upcase|index($up)) as $ix
  | select($ix)
  | select($ix + $len < length)