1

I have trouble Concatenating output of jq command-Parsing JSON file using jq,convert it to CSV - combine 2 columns into one | Stack Overflow

So i wonder if it's possible to search CSV file and merge columns with multiple policies.Here is CSV file:

"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"

How to merge columns with multiple policies into one (replace commas with -) and remove quotes in strings between first and last one for each role (first column) "aws-elasticbeanstalk-ec2-role","AWSElasticBeanstalkWebTier","AWSElasticBeanstalkMulticontainerDocker","AWSElasticBeanstalkWorkerTier" Output should be

"aws-elasticbeanstalk-ec2-role","AWSElasticBeanstalkWebTier-AWSElasticBeanstalkMulticontainerDocker-AWSElasticBeanstalkWorkerTier"

For

"aws-elasticbeanstalk-service-role","AWSElasticBeanstalkEnhancedHealth","AWSElasticBeanstalkService",

"aws-elasticbeanstalk-service-role","AWSElasticBeanstalkEnhancedHealth-AWSElasticBeanstalkService"

same for lambda_api_gateway

Common for all lines is that arn:aws:iam string is missing.How to detect lines without arn:aws string and with multiple policies ?

1 Answer 1

1

Found an answer without need to use text editor:multiple values in same columns will be separated by --

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .