我有以下文字:
roledetails="name: CLD:HYB_Y_BASIS_ADMIN
description: Basis Admin
readOnly: <null>
roleReferences:
- description: desc1
name: AuthGroup_Administrator
roleTemplateAppId: it!b2455
roleTemplateName: AuthGroup_Administrator
- description: Desc2
name: AuthGroup_BusinessExpert
roleTemplateAppId: it!b2455
roleTemplateName: AuthGroup_BusinessExpert
"
我喜欢转换为以下格式的 .csv 文件。我将如何使用 AWK 来做到这一点?
Collection Role Desc,Role_Name,App_Id, Role_Template
CLD:HYB_Y_BASIS_ADMIN, desc1, AuthGroup_Administrator,it!b2455,AuthGroup_Administrator
CLD:HYB_Y_BASIS_ADMIN, desc2, AuthGroup_BusinessExpert,it!b2455,AuthGroup_BusinessExpert
我尝试了下面的代码但不起作用:
declare fullfilepath=$(pwd)"/test1.csv"
awk -F':[[:space:]]+' -v fullfilepath="$fullfilepath" 'NR==1{ # When on Line 1
a=$2;cnt=0 # Set a and cnt vars
}
/^-/{ # When line starts with -
rta[cnt]=$2; getline; # Add role template app ID to rta array
rtn[cnt]=$2; getline; # Add role template name to rtn array
n[cnt]=$2;cnt++ # Add name to n array
}
END{ # When the file processing is over
for(i=0;i<cnt;i++) { # Iterate over the found values and...
#print a","n[i]","rtn[i]","rta[i] # print them
printf a","n[i]","rtn[i]","rta[i] "\n" >> fullfilepath # print them
}
}' <<< "$roledetails"
TXR中的解决方案
使用 Bash 重定向从变量
<<<
:代码:
假设:
rollReferences
块都有相同的 5 个字段,最后一个字段始终是roleTemplateName
一个
awk
想法:注意:这在很大程度上取决于输入数据的确切格式
这会生成: