次のCSVファイルがあります。
"ADFS-Administrators","Administrator-Access","arn:aws:iam::279052847476:saml-provider/companyADFS"
"ADFS-amtest-ro","arn:aws:iam::279052847476:saml-provider/companyADFS"
"AWSAccCorpAdmin","arn:aws:iam::279052847476:saml-provider/LastPass"
"AWScompanyCorpAdmin","arn:aws:iam::279052847476:saml-provider/LastPass"
"AWScompanyCorpPowerUser","arn:aws:iam::279052847476:saml-provider/LastPass"
"flowlogsRole","oneClick_flowlogsRole_1495032428381",
"companyDevShutdownEC2Instaces","oneClick_lambda_basic_execution_1516271285849",
"companySAMLUser","arn:aws:iam::279052847476:saml-provider/companyAzureAD"
"lambda_stop_rundeck_instance","oneClick_lambda_basic_execution_1519651160794",
"OneLoginAdmin","arn:aws:iam::279052847476:saml-provider/OneLoginAdmin"
"OneLoginDev","arn:aws:iam::279052847476:saml-provider/OneLoginDev"
"vmimport","vmimport",
"workspaces_DefaultRole","SkyLightServiceAccess",
最初のカンマの後に次に始まる文字列がある場合は、各行に別のカンマを追加したいと思います。arn:aws:iam:
希望の出力(部分):
"ADFS-amtest-ro",,"arn:aws:iam::279052847476:saml-provider/companyADFS"
"AWSAccCorpAdmin",,"arn:aws:iam::279052847476:saml-provider/LastPass"
"AWScompanyCorpAdmin",,"arn:aws:iam::279052847476:saml-provider/LastPass"
"AWScompanyCorpPowerUser",,"arn:aws:iam::279052847476:saml-provider/LastPass
文字列で始まらない行はarn:aws:iam
何も変更しないでください。
答え1
そしてsed
:
sed 's/^\([^,]*\)\(,"arn:aws:iam:\)/\1,\2/'
答え2
awk
解決策:
awk 'BEGIN{ FS=OFS="," }$2 ~ /^"arn:aws:iam:/{ $2 = ","$2 }1' file.csv
$2 ~ /^"arn:aws:iam:/
- 2番目のフィールドが次から$2
始まる場合"arn:aws:iam:
$2 = ","$2
- 2番目のフィールド値の前に追加,
出力:
"ADFS-Administrators","Administrator-Access","arn:aws:iam::279052847476:saml-provider/companyADFS"
"ADFS-amtest-ro",,"arn:aws:iam::279052847476:saml-provider/companyADFS"
"AWSAccCorpAdmin",,"arn:aws:iam::279052847476:saml-provider/LastPass"
"AWScompanyCorpAdmin",,"arn:aws:iam::279052847476:saml-provider/LastPass"
"AWScompanyCorpPowerUser",,"arn:aws:iam::279052847476:saml-provider/LastPass"
"flowlogsRole","oneClick_flowlogsRole_1495032428381",
"companyDevShutdownEC2Instaces","oneClick_lambda_basic_execution_1516271285849",
"companySAMLUser",,"arn:aws:iam::279052847476:saml-provider/companyAzureAD"
"lambda_stop_rundeck_instance","oneClick_lambda_basic_execution_1519651160794",
"OneLoginAdmin",,"arn:aws:iam::279052847476:saml-provider/OneLoginAdmin"
"OneLoginDev",,"arn:aws:iam::279052847476:saml-provider/OneLoginDev"
"vmimport","vmimport",
"workspaces_DefaultRole","SkyLightServiceAccess",