从远程服务器检索json文件中的值,然后使用sed替换大写、双引号和空格

从远程服务器检索json文件中的值,然后使用sed替换大写、双引号和空格,json,bash,ssh,jq,Json,Bash,Ssh,Jq,我必须查询一个jason文件来检索service_name的值,然后获取该值,删除双引号,将空格转换为破折号,然后将大写转换为小写 这是我的json production.json文件 { "port": 5000, "machine": "102", "ip": "xxx.xxx.xxx.xxx", "drum_id": "1305145216186552", "client": { "service_name": "Two Men An

我必须查询一个jason文件来检索service_name的值,然后获取该值,删除双引号,将空格转换为破折号,然后将大写转换为小写

这是我的json production.json文件

{
    "port": 5000,
    "machine": "102",
    "ip": "xxx.xxx.xxx.xxx",
    "drum_id": "1305145216186552",
    "client": {
        "service_name": "Two Men And A Truck",
        "vendor": "default"
    }
}
这是我的命令


jq'\(.client.service_name)'/home/systems/clients/000002/production.json | sed's//“//g;s//-/g;s//\(.*)/\L\1//”

这个很好用

root@0200 ~ # cat /home/systems/clients/000002/production.json
{
    "port": 5000,
    "machine": "102",
    "ip": "xxx.xxx.xxx.xxx",
    "drum_id": "1305145216186552",
    "client": {
        "service_name": "Two Men And A Truck",
        "vendor": "default"
    }
}
root@0200 ~ # jq '"\(.client.service_name)"' /home/systems/clients/000002/production.json | sed 's/"//g;s/ /-/g;s/\(.*\)/\L\1/'
two-men-and-a-truck
现在,这是我的初始脚本。它都是vlan atm,我最终将通过ssh隧道运行它

#!/bin/bash
#

#-- tmp files
tmp_dir="$(mktemp -d -t 'text.XXXXX' || mktemp -d 2>/dev/null)"
tmp_input1="${tmp_dir}/temp_input1.txt"
tmp_input2="${tmp_dir}/temp_input2.txt"
tmp_input3="${tmp_dir}/temp_input3.txt"

#- servers
cbservers=( "xxx.xxx.xxx.xxx" "xxx.xxx.xxx.xxx" )

for cbserver in "${cbservers[*]}"; do
  ssh user@"$cbserver" "ls /home/systems/clients | grep '^[0-9]'" > "$tmp_input1"
  while read client; do
    ssh user@"$cbserver" "jq '"\(.client.service_name)"' /home/systems/clients/000002/production.json | sed 's/"//g;s/ /-/g;s/\(.*\)/\L\1/'" > "$tmp_input3"
  done<"$tmp_input1"
done

rm -rf "$tmp_dir"

“while read client”循环只运行一次,应该运行12次。

您不需要引用整个命令,可以提供多个参数。我会这样做:

file=/home/systems/clients/000002/production.json
...
ssh user@"$cbserver" jq -r '.client.service_name' "$file" | perl -lnE 's/\s+/-/g; say lc' > "$tmp_input3"
注:

  • perl
    命令将在本地系统上运行
  • 我首先使用
    jq-r
    来避免引用

    • 您不需要引用整个命令,可以提供多个参数。我会这样做:

      file=/home/systems/clients/000002/production.json
      ...
      ssh user@"$cbserver" jq -r '.client.service_name' "$file" | perl -lnE 's/\s+/-/g; say lc' > "$tmp_input3"
      
      注:

      • perl
        命令将在本地系统上运行
      • 我首先使用
        jq-r
        来避免引用

        • 您可以在
          jq
          本身中完成所有这些:

          $ jq -r '.client.service_name | ascii_downcase | split(" ") | join("-")' <<EOF
          {
              "port": 5000,
              "machine": "102",
              "ip": "xxx.xxx.xxx.xxx",
              "drum_id": "1305145216186552",
              "client": {
                  "service_name": "Two Men And A Truck",
                  "vendor": "default"
              }
          }
          EOF
          two-men-and-a-truck
          

          $jq-r'.client.service_name | ascii_downcase | split(“”)| join(“”)您可以在
          jq
          本身中完成这一切:

          $ jq -r '.client.service_name | ascii_downcase | split(" ") | join("-")' <<EOF
          {
              "port": 5000,
              "machine": "102",
              "ip": "xxx.xxx.xxx.xxx",
              "drum_id": "1305145216186552",
              "client": {
                  "service_name": "Two Men And A Truck",
                  "vendor": "default"
              }
          }
          EOF
          two-men-and-a-truck
          

          jq-r'.client.service_name | ascii_downcase | split(“”)join(“-”)使用
          jq--raw output
          比使用sed删除双引号(如果字符串包含转义双引号呢?)@choroba更有效。请欣赏这一技巧。使用
          jq--raw output
          比使用sed删除双引号更有效(如果字符串包含转义双引号怎么办?)@choroba说得对。感谢这个提示。我现在要尝试一下。我没有使用你的perl语句,但我遵循了你的建议,即不必引用所有内容,但循环只运行一次。Ssh将阻塞stdin。有一个选项可以防止它:可能
          -n
          ,但我附近没有手册页。谢谢。我要尝试一下这是现在。我没有使用您的perl语句,但我遵循了您的建议,即不必引用所有内容,但循环只运行一次。Ssh将清除stdin。有一个选项可以防止它:可能
          -n
          ,但我附近没有手册页,就是这样。非常感谢。