【问题标题】:jq and Bash: How to get value of key from value of object? (How to build/update a json?)jq 和 Bash:如何从对象的值中获取键的值? (如何构建/更新 json?)
【发布时间】:2021-06-26 23:44:13
【问题描述】:

首先,对不起我的英语,我是法国人。

我正在编写一个脚本,该脚本从 M3U 文件中检索标签和链接,以将它们存储到变量中。 M3U:

#EXTM3U
#EXTINF:-1 tvg-id="TFX.fr" tvg-name="TFX" tvg-country="FR;AD;BE;LU;MC;CH" tvg-language="French" tvg-logo="http://www.exemple.com/image.jpg" group-title="",TFX (720p)
https://tfx-hls-live-ssl.tf1.fr/tfx/1/hls/live_2328.m3u8

脚本:

#!/bin/bash

tags='#EXTINF:-1 tvg-id="TFX.fr" tvg-name="TFX" tvg-country="FR;AD;BE;LU;MC;CH" tvg-language="French" tvg-logo="http://www.exemple.com/image.jpg" group-title="Fiction",TFX (720p)'

get_chno="$(echo "$tags" | grep -o 'tvg-chno="[^"]*' | cut -d '"'  -f2)"
get_id="$(echo "$tags" | grep -o 'tvg-id="[^"]*' | cut -d '"'  -f2)"
get_logo="$(echo "$tags" | grep -o 'tvg-logo="[^"]*' | cut -d '"'  -f2)"
get_grp_title="$(echo "$tags" | grep -o 'group-title="[^"]*' | cut -d '"'  -f2)"
get_title="$(echo "$tags" | grep -o ',[^*]*' | cut -d ','  -f2)"
get_name="$(echo "$tags" | grep -o 'tvg-name="[^"]*' | cut -d '"'  -f2)"
get_country="$(echo "$tags" | grep -o 'tvg-country="[^"]*' | cut -d '"'  -f2)"
get_language="$(echo "$tags" | grep -o 'tvg-language="[^"]*' | cut -d '"'  -f2)"

echo -e "chno:\n $get_chno"
echo -e "id:\n $get_id"
echo -e "logo:\n $get_logo"
echo -e "grp 1:\n $get_grp_title"
echo -e "title:\n $get_title"
echo -e "name:\n $get_name"
echo -e "country:\n $get_country"
echo -e "lang:\n $get_language"

我想将这些变量存储在一个 json 文件中。 此 json 将用于重建另一个播放列表。

    #EXTM3U
    #EXTINF:-1 tvg-id="TFX.fr" tvg-name="TFX" tvg-country="FR;AD;BE;LU;MC;CH" tvg-language="French" tvg-logo="http://www.exemple.com/image.jpg" group-title="",TFX (720p)
    https://tfx-hls-live-ssl.tf1.fr/tfx/1/hls/live_2328.m3u8
    #EXTINF:-1 tvg-id="TFX.fr" tvg-name="TFX" tvg-country="FR;AD;BE;LU;MC;CH" tvg-language="French" tvg-logo="http://127.0.0.1/img/image.jpg" group-title="",TFX (local)
    http://127.0.0.1:1234/tfx/live.m3u8

包含多个数组和多个对象的文件。

像这样:

{
  "Channels": [
    {
      "name": "TFX",
      "old_name": "NT1",
      "logo": "http://www.exemple.com/image.jpg",
      "category": "Fiction",
      "urls": {
        "Official": [
          {
            "server_name": "TF1",
            "IP_address": "8.8.8.8",
            "url": "tfx-hls-live-ssl.tf1.fr",
            "port": "",
            "https_port": "443",
            "path": "tfx/1/hls/",
            "file_name": "live_2328",
            "extension": ".m3u8",
            "full_url": "https://tfx-hls-live-ssl.tf1.fr/tfx/1/hls/live_2328.m3u8"
          }
        ],
        "Xtream_Servers": [
          {
            "server_name": "local",
            "user_name": "rickey",
            "stream_id": "11",
            "category_name": "Fiction",
            "category_id": "12"
          }
        ]
      },
      "languages": [
        {
          "code": "fr",
          "name": "Français"
        }
      ],
      "countries": [
        {
          "code": "fr",
          "name": "France"
        },
        {
          "code": "be",
          "name": "Belgium"
        }
      ],
      "tvg": {
        "id": "TFX.fr",
        "name": "TFX",
        "url": ""
      }
    },
    {
      "name": "France 2",
      "old_name": "",
      "logo": "http://www.exemple.com/image.jpg",
      "category": "Général",
      "urls": {
        "Official": [
          {
            "server_name": "France TV",
            "IP_address": "8.8.8.8",
            "url": "france2.fr",
            "port": "",
            "https_port": "443",
            "path": "live/",
            "file_name": "Playlist",
            "extension": ".m3u8",
            "full_url": "https://france2.fr/live/Playlist.m3u8"
          }
        ],
        "Xtream_Servers": [
          {
            "server_name": "localhost",
            "user_name": "rickey",
            "stream_id": "2",
            "category_name": "Général",
            "category_id": "10"
          }
        ]
      },
      "languages": [
        {
          "code": "fr",
          "name": "Français"
        }
      ],
      "countries": [
        {
          "code": "fr",
          "name": "France"
        },
        {
          "code": "be",
          "name": "Belgique"
        }
      ],
      "tvg": {
        "id": "France2.fr",
        "name": "France 2",
        "url": ""
      }
    },
    {
      "name": "M6",
      "old_name": "",
      "logo": "http://www.exemple.com/image.jpg",
      "category": "Général",
      "urls": {
        "Official": [
          {
            "server_name": "6Play",
            "IP_address": "8.8.8.8",
            "url": "6play.fr",
            "port": "",
            "https_port": "443",
            "path": "live/",
            "file_name": "Playlist",
            "extension": ".m3u8",
            "full_url": "https://6play.fr/M6/live/Playlist.m3u8"
          }
        ],
        "Xtream_Servers": [
          {
            "server_name": "localhost",
            "user_name": "rickey",
            "stream_id": "6",
            "category_name": "Général",
            "category_id": "10"
          }
        ]
      },
      "languages": [
        {
          "code": "fr",
          "name": "Français"
        }
      ],
      "countries": [
        {
          "code": "fr",
          "name": "France"
        },
        {
          "code": "be",
          "name": "Belgique"
        }
      ],
      "tvg": {
        "id": "France2.fr",
        "name": "France 2",
        "url": ""
      }
    }
  ],
  "Third_Party": {
    "Xtream_Servers": [
      {
        "server_name": "local",
        "url": "192.168.1.100",
        "port": "8080",
        "https_port": "8082",
        "server_protocol": "http",
        "rtmp_port": "12345",
        "Users_list": [
          {
            "username": "rickey",
            "password": "azerty01",
            "created_at": "",
            "exp_date": "",
            "is_trial": "0",
            "last_check": "",
            "max_connections": "3",
            "allowed_output_formats": [
              "m3u8",
              "ts",
              "rtmp"
            ]
          }
        ]
      },
      {
        "server_name": "localhost",
        "url": "127.0.0.1",
        "port": "8080",
        "https_port": "8082",
        "server_protocol": "http",
        "rtmp_port": "12345",
        "Users_list": [
          {
            "username": "rickey123",
            "password": "azerty321",
            "created_at": "",
            "exp_date": "",
            "is_trial": "0",
            "last_check": "",
            "max_connections": "3",
            "allowed_output_formats": [
              "m3u8",
              "ts",
              "rtmp"
            ]
          },
          {
            "username": "guest",
            "password": "guest01",
            "created_at": "",
            "exp_date": "",
            "is_trial": "1",
            "last_check": "",
            "max_connections": "1",
            "allowed_output_formats": [
              "ts"
            ]
          }
        ]
      }
    ]
  }
}

第一个问题:它是一个糟糕的 json 吗?

要添加或修改这个文件,脚本必须有入口号(我想,如果你有其他想法,我很感兴趣......)

cat File.json  | jq '.Channels | to_entries[]'

输出:

{
  "key": 0,
  "value": {
    "name": "TFX",
    "old_name": "NT1",
    

第二个问题: 如何使用“名称”的值获取值键(这种情况下为 0),以便在之后存储到变量中? (避免重复)

key_="$(cat file.json | jq ????????? search="name": "$get_name" ???? .key)"
echo $key_
"0"
key_2="$(cat file.json | jq ????????? search="name": "$get_url" ???? .key)"
echo $key_2
"0"

if [[ $key_ == $key_2 ]]; then

Chan_Name="$(cat $1 | jq '.Channels[$key_].name)"
Echo $Chan_Name
"TFX"
jq  '.[] ????? += {???? , ??? }' file.json | sponge file.json

fi

最后一个问题(最重要的): 当脚本不知道对象/数组的键的任何值时,如何查找和修改这些 f*** 对象?!

我已经找了 2 天了,我的大脑是液体。

谢谢。 :)

编辑 1:

我找到了替换值的部分解决方案:

{
      "name": "TFX",
      "old_name": "NT1",
      "logo": "http://www.exemple.com/image.jpg",
      "category": "Fiction",

与:

 cat file.json | jq -C '(.Channels[] | select(.name=="TFX").category="test")'

输出:

{
  "name": "TFX",
  "old_name": "NT1",
  "logo": "http://www.exemple.com/image.jpg",
  "category": "test",
  "urls": {

但是 "{"Channels": [" 不见了。 :/

【问题讨论】:

    标签: bash jq


    【解决方案1】:
    jq -C '(.Channels[] | select(.name=="TFX").category="test")'
    

    你是如此接近 - 只是一个错位的括号:

    jq '(.Channels[] | select(.name=="TFX")) .category="test"'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-24
      • 1970-01-01
      • 1970-01-01
      • 2023-02-23
      • 1970-01-01
      • 2019-04-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多