搜索并从输出替换为文件内的匹配模式

时间:2017-04-04 19:32:12

标签: csv amazon-web-services awk sed

我正在尝试为安全组编写AWS Cloud格式脚本,但尝试使用少量黑客进行自动化操作时,该过程非常可重复

我通过以下对awksed的调用获得了CSV输出:

awk -F, 'NR > 1 { OFS=",";print $2, $3, $4, $5 }' para.csv | sed -n 1p

输出:

10.0.0.0/8,tcp,53,53

我需要将此输出映射到具有以下

的文件
"ingress5": {
      "Type": "AWS::EC2::SecurityGroupIngress",
      "Properties": {
        "GroupId": {
          "Ref": "sginformatica"
        },
        "IpProtocol": "",
        "FromPort": "",
        "ToPort": "",
        "CidrIp": ""
      }
},

即从命令IpProtocol的输出应该映射到tcp

FromPort应映射53(输出中的第3列或第3列)

ToPort应映射53(第4列或输出中的第4栏)

CidrIp应映射到10.0.0.0/8(输出的字段1)

2 个答案:

答案 0 :(得分:1)

#!/bin/bash

while IFS="," read -r CidrIp IpProtocol FromPort ToPort; do
cat << EOF
"ingress5": {
      "Type": "AWS::EC2::SecurityGroupIngress",
      "Properties": {
        "GroupId": {
          "Ref": "sginformatica"
        },
        "IpProtocol": "$IpProtocol",
        "FromPort": "$FromPort",
        "ToPort": "$ToPort",
        "CidrIp": "$CidrIp"
      }
},
EOF
done < file

使用此文件:

10.0.0.0/8,tcp,53,53

输出:

"ingress5": {
      "Type": "AWS::EC2::SecurityGroupIngress",
      "Properties": {
        "GroupId": {
          "Ref": "sginformatica"
        },
        "IpProtocol": "tcp",
        "FromPort": "53",
        "ToPort": "53",
        "CidrIp": "10.0.0.0/8"
      }
},

或没有文件:

awk -F, 'NR > 1 { OFS=",";print $2, $3, $4, $5 }' para.csv | sed -n 1p | while ...; do ...; done

答案 1 :(得分:1)

安装并使用灵活的命令行JSON处理器 - jq

假设我们有ingress.json个文件包含内容:

{
    "ingress5": {
      "Type": "AWS::EC2::SecurityGroupIngress",
      "Properties": {
        "GroupId": {
          "Ref": "sginformatica"
        },
        "IpProtocol": "",
        "FromPort": "",
        "ToPort": "",
        "CidrIp": ""
      }
    }
}

首先,我们将关键输入字符串调整为有效的json字符串:

v="["$(awk -F, '{ OFS=",";print "\042"$2"\042", "\042"$3"\042", $4, $5 }' para.csv | sed -n 1p)"]"
echo $v
["10.0.0.0/8","tcp",53,53]

下一步是使用 jq 命令修改所需的属性值:

jq --argjson v "$v" '.ingress5.Properties.IpProtocol = $v[1] | .ingress5.Properties.FromPort = $v[2] 
  | .ingress5.Properties.ToPort = $v[3] | .ingress5.Properties.CidrIp = $v[0]' ingress.json

输出:

{
  "ingress5": {
    "Type": "AWS::EC2::SecurityGroupIngress",
    "Properties": {
      "GroupId": {
        "Ref": "sginformatica"
      },
      "IpProtocol": "tcp",
      "FromPort": 53,
      "ToPort": 53,
      "CidrIp": "10.0.0.0/8"
    }
  }
}
相关问题