我正在使用 awk 命令,但无法使用 shell 脚本在列中打印 IP

I am using awk command but unable to print IPs in in column using shell script

我有一个格式如下的文件 "LM1",我希望将它们打印在单独的列中,如下例所示

我使用了下面的 awk 命令来实现这一点,但我无法提取 csv 文件中的 IP4 字段,其余数据以列的形式出现。

我执行以下命令来实现我的要求

我有一个格式如下的文件 "LM1",我希望将它们打印在单独的列中,如下例所示

awk -F": " -v OFS="\t" '
BEGIN {print "CustName", "OS", "LM_Name", "Name", "IPv4", "Status" }
{
gsub(/"/,"")
sub(/^[[:blank:]]+/,"")
}
 == "customer_name" {
if ("customer_name" in data && !have_data)
print data["customer_name"]
have_data = 0
}
{
data[] = 
}
("os_type" in data) && ("local_hostname" in data) && ("name" in data) && ("local_ipv4" in data) && ("status" in data) {
print data["customer_name"], data["os_type"], data["local_hostname"], data["name"], data["local_ipv4"], data["status"]
delete data["os_type"]
delete data["local_hostname"]
delete data["name"]
delete data["local_ipv4"]
delete data["status"]
have_data = 1
}
' LM1 | column -s $'\t' -t > LM-Status-Report.csv

预期(输出)数据格式:

Column A   Column B         Column C              Column D
==========================================================
Customer   Local_Hostname   IP4 Addresse          Status
==========================================================
ABC        ABC-log-01       10.9.9.9,10.9.3.4     OK
ABC        ABC-log-02       10.8.8.8              New
XYZ        XYZ-log-01       10.10.2.2,10.2.4.6    Ok
XYZ        XYZ-log-02       10.2.3.4              New

RAW 数据现有(输入)格式:

customer_name: "ABC"
{
  "syslog": {
    "created": {
      "at": 1478195183
      "by": 0
    }
    "id": "886707D0-4069-1005-8535-0050568525D9"
    "metadata": {
      "local_hostname": "ABC-log-01"
      "local_ipv4": [
        "10.9.9.9"
        "10.9.3.4"
      ]
      "public_ipv4": [
        "127.0.0.1"
        "10.1.1.1"
      ]
      "total_mem_mb": 3884
    }
    "modified": {
      "at": 1478195247
      "by": 0
    }
    "name": "ABC-log-01"
    "policy_id": "9125663A-04EA-4F1D-A436-ADFEF069D4BA"
    "stats": {
      "last_day_bytes": 0
      "last_update_ts": 0
    }
    "status": {
      "details": []
      "status": "ok"
    }
  }
}
{
  "eventlog": {
    "created": {
      "at": 1499888362
      "by": 0
    }
    "id": "A4D2EA92-5423-1005-B1B3-0050568505BC"
    "metadata": {
      "os_type": "windows"
      "local_hostname": "ABC-log-02"
      "local_ipv4": [
        "10.8.8.8"
      ]
      "num_logical_processors": 2
      "os_details": "Windows Server (R) 2008 Standard; 6.0.6002; Service Pack 2; x86"
      "public_ipv4": [
        "10.1.1.1"
      ]
      "public_ipv4": [
        "10.4.4.4"
      ]
      "total_mem_mb": 2046
    }
    "modified": {
      "at": 1512582221
      "by": 13939
    }
    "name": "ABC-log-03"
    "stats": {
      "last_day_bytes": 0
    }
    "status": {
      "details": []
      "status": "new"
      "timestamp": 1508228598
      "updated": 1508228598
    }
    "tags": []
  }
}
customer_name: "XYZ"
{
  "syslog": {
    "created": {
      "at": 1507196910
      "by": 0
    }
    "id": "9E47B629-5AC9-1005-B1B3-0050568505BC"
    "metadata": {
      "host_type": "standalone"
      "os_type": "unix"
      "version": "1"
      "local_hostname": "XYZ-log-01"
      "local_ipv6": [
        "10.10.1.2"
        "10.10.2.3"
      ]
      "num_logical_processors": 4
      "os_details": "Linux; 2.6.32-696.6.3.el6.x86_64; #1 SMP Wed Jul 12 14:17:22 UTC 2017; x86_64"
      "public_ipv4": [
        "10.10.2.2"
        "10.2.4.6"
      ]
      "total_mem_mb": 3951
    }
    "modified": {
      "at": 1507196910
      "by": 0
    }
    "name": "XYZ-log-01"
    "policy_id": "7135663A-04EA-4F1D-A436-ADFEF069D4BA"
    "stats": {
      "last_day_bytes": 0
      "last_update_ts": 0
    }
    "status": {
      "details": []
      "status": "ok"
      "timestamp": 1508228712
      "updated": 1519723490
    }
    "tags": []
  }
}
{
  "eventlog": {
    "created": {
      "at": 1507196961
      "by": 0
    }
    "id": "2F417043-5AC9-1005-B1B3-0050568505BC"
    "metadata": {
      "host_type": "standalone"
      "os_type": "windows"
      "version": "1"
      "local_hostname": "XYZ-log-02"
      "local_ipv4": [
        "10.2.3.4"
      ]
      "num_logical_processors": 2
      "os_details": "Windows Server (R) 2008 Standard; 6.0.6002; Service Pack 2; x86"
      "public_ipv4": [
        "10.2.3.4"
      ]
      "public_ipv6": [
        "*.*.*.*"
      ]
      "total_mem_mb": 2046
    }
    "modified": {
      "at": 1507196961
      "by": 0
    }
    "name": "XYZ-log-02"
    "stats": {
      "last_day_bytes": 0
      "last_update_ts": 0
    }
    "status": {
      "details": []
      "status": "new"
      "timestamp": 1508228722
      "updated": 1508228722
    }
    "tags": []
  }
}

打印 IP 所需要做的就是替换此块:

{
data[] = 
}

这个:

{
    if ( == "[")
        list = 
    else if (list &&  =="]")
        list = 0
    else if (list)
        data[list] = data[list] "," 
    else
        data[] = 
}

这会将您数据中的所有 JSON 列表解析为单个字符串,其中所有列表元素将由逗号连接。所以这不仅适用于 local_ipv4,也适用于 public_ipv6 和其他人。

然后你只需要去掉一个额外的逗号。所以代替:

print data["customer_name"], data["os_type"], data["local_hostname"], data["name"], data["local_ipv4"], data["status"]

使用substr(data["local_ipv4"],2)函数去掉前面的逗号:

print data["customer_name"], data["os_type"], data["local_hostname"], data["name"], substr(data["local_ipv4"],2), data["status"]

至少,这是一种基于您的代码的简单方法。