【发布时间】:2021-10-20 15:27:28
【问题描述】:
$string="name: Destination Administrator
description: Manage the destination configurations, certificates and subaccount trust.
readOnly:
roleReferences:
- roleTemplateAppId: destination-xsappname!b62
roleTemplateName: Destination_Administrator
name: Destination Administrator"
我有上面的字符串,每一行都由换行符分隔,我喜欢在“-”之后创建两列的数组,如下所示
Col1 col2
roleTemplateAppId destination-xsappname!b62
roleTemplateName Destination_Administrator
name Destination Administrator
我在下面尝试过,但它没有返回正确的数组
IFS='- ' read -r -a arrstring <<< "$string"
echo "${arrstring [1]}"
【问题讨论】:
-
这是哪个 shell 脚本变体?您应该标记您的问题 [bash]、[zsh] 等。
-
对不起,我添加为 Bash
-
您的数据是 YAML,请使用 YAML 解析器。 Bash/shell 无法解析 YAML。
-
array with two column是什么意思?第一列应该是数组的索引吗? -
我的示例中显示的数据的二维数组。 Col1 和 col2 显示的值。