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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我正在使用awk命令,但无法使用shell脚本在列中打印IP相关的知识,希望对你有一定的参考价值。

我有一个以下格式的文件“LM1”,我希望这些打印在单独的列中,如下例所示

我已经使用下面的awk命令来实现这一点,但我无法在csv文件中提取IP4字段,其余数据正在列中。

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

我有一个以下格式的文件“LM1”,我希望这些打印在单独的列中,如下例所示

awk -F": " -v OFS="	" '
BEGIN {print "CustName", "OS", "LM_Name", "Name", "IPv4", "Status" }
{
gsub(/"/,"")
sub(/^[[:blank:]]+/,"")
}
$1 == "customer_name" {
if ("customer_name" in data && !have_data)
print data["customer_name"]
have_data = 0
}
{
data[$1] = $2
}
("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 > 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[$1] = $2
}

这一个:

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

这会将数据中的所有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"]

至少,这是一种基于代码执行此操作的简单方法。

以上是关于我正在使用awk命令,但无法使用shell脚本在列中打印IP的主要内容,如果未能解决你的问题,请参考以下文章

Linux Shell编程 awk命令

shell脚本之awk工具的使用

shell脚本江湖秘籍只传有缘人——流编辑器“三剑客”之awk命令

详解Shell脚本:sed命令工具 ,awk命令工具

shell脚本之awk编辑器

shell脚本之awk编辑器