我在连接 jq 命令的输出时遇到问题-使用 jq 解析 JSON 文件,将其转换为 CSV - 将 2 列合并为一列 |堆栈溢出
所以我想知道是否可以搜索 CSV 文件并合并具有多个策略的列。这是 CSV 文件:
"ADFS-Administrators","Administrator-Access","arn:aws:iam::279052847476:saml-provider/CompanyADFS"
"ADFS-amtest-ro","pol-amtest-ro","arn:aws:iam::279052847476:saml-provider/CompanyADFS"
"adfs-host-role","pol-amtest-ro",
"aws-elasticbeanstalk-ec2-role","AWSElasticBeanstalkWebTier","AWSElasticBeanstalkMulticontainerDocker","AWSElasticBeanstalkWorkerTier",
"aws-elasticbeanstalk-service-role","AWSElasticBeanstalkEnhancedHealth","AWSElasticBeanstalkService",
"AWSAccCorpAdmin","AdministratorAccess","arn:aws:iam::279052847476:saml-provider/LastPass"
"lambda_api_gateway_twilio_processor","AWSLambdaBasicExecutionRole-f47a6b57-b716-4740-b2c6-a02fa6480153","AWSLambdaSNSPublishPolicyExecutionRole-d31a9f16-80e7-47c9-868a-f162396cccf6"
如何将具有多个策略的列合并为一个(用 - 替换逗号)并删除每个角色的第一个和最后一个之间的字符串中的引号(第一列)"aws-elasticbeanstalk-ec2-role","AWSElasticBeanstalkWebTier","AWSElasticBeanstalkMulticontainerDocker","AWSElasticBeanstalkWorkerTier"
输出应该是
"aws-elasticbeanstalk-ec2-role","AWSElasticBeanstalkWebTier-AWSElasticBeanstalkMulticontainerDocker-AWSElasticBeanstalkWorkerTier"
为了
"aws-elasticbeanstalk-service-role","AWSElasticBeanstalkEnhancedHealth","AWSElasticBeanstalkService",
"aws-elasticbeanstalk-service-role","AWSElasticBeanstalkEnhancedHealth-AWSElasticBeanstalkService"
与 lambda_api_gateway 相同
所有行的共同点是缺少 arn:aws:iam 字符串。如何检测没有 arn:aws 字符串且具有多个策略的行?
答案1
无需使用文本编辑器即可找到答案:同一列中的多个值将由--
jq -rc '.RoleDetailList | map(select((.AssumeRolePolicyDocument.Statement | length > 0) and (.AssumeRolePolicyDocument.Statement[].Principal.Federated) or (.AttachedManagedPolicies | length >0) or (.RolePolicyList | length > 0)) )[] | [.RoleName,([.RolePolicyList[].PolicyName,([.AttachedManagedPolicies[].PolicyName] | join("--"))] | join(" ")),(.AssumeRolePolicyDocument.Statement[] | .Principal.Federated)] | @csv' output.json