2017-04-04 20 views
0

アムは、プロセスがあまりにも非常に再現性があるいくつかのハッキングはそれを検索し、ファイル内のマッチングパターンへの出力から置き換える

を自動化するためにしようとしている私は、CSV出力を持っていますawksedへの次の呼び出しから:

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

出力:

10.0.0.0/8,tcp,53,53 

私はFILに、この出力をマッピングする必要がありますコマンドIpProtocolの出力から、次の

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

すなわちを持っているeはtcpにマッピングする必要があります。

FromPortがマッピングされるべき53(カラム3または出力フィールド3)

ToPortがマッピングされるべき53(カラム4又は出力におけるフィールド4)

CidrIp10.0.0.0/8にマッピングされなければならない(フィールドこのファイルでは、出力の1)

答えて

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

柔軟なコマンドライン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" 
    } 
    } 
} 
+0

は@SaravananDの –

+0

のjqを試します。 – RomanPerekhrest

関連する問題