在某个字符前添加逗号

在某个字符前添加逗号

我有这个 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"arn:aws:iam:
  • $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",

相关内容