【问题标题】:Bash - Parse a String Into Array separated with commaBash - 将字符串解析为用逗号分隔的数组
【发布时间】:2017-05-18 08:09:28
【问题描述】:

在 Bash 中,我如何在 Bash 的数组中转换下面的字符串:

details="cl roo,dara shi,GD leader, is the man,YG FAMILY"

这是我的代码:

IFS="[a-z],[a-z]" read -r -a details_list <<< "$details"

数组迭代时的预期输出:

cl roo
dara shii
GD leader, is the man
YG FAMILY

数组迭代时的实际输出:

cl roo
dara shii
GD leader
is the man
YG FAMILY

【问题讨论】:

  • 重复问题:这里是a link
  • 我已经检查过了,它与预期的输出不同。
  • 您如何确定要中断的逗号?
  • 我在想 bdelimiter 是放在两个字符之间的逗号......我不知道如何在正则表达式上使用它。

标签: arrays regex bash delimiter ifs


【解决方案1】:

这应该可行:

details="cl roo;dara shi;GD leader, is the man;YG FAMILY"

IFS=';' read -r -a array <<< "$details"

echo "${array[2]}"   -> GD leader, is the man

【讨论】:

  • 感谢您的回答。我试过这个,但“GD领导,是男人”被切成两半。我想把它放在一个索引中。
  • @stickyservernotes:看看我的编辑。 'GD领导'合二为一
  • @stickyservernotes 现在可以使用了。请使用另一个分隔符,例如 ';'
  • 我将其保存为最后的手段。我要解析的字符串将来自日志文件,所以我不能随意更改分隔符。
猜你喜欢
  • 2023-03-29
  • 1970-01-01
  • 2011-02-20
  • 2021-02-24
  • 2019-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多