循环通过逗号分隔的Python-Format列表

发布于 2025-01-26 04:40:48 字数 222 浏览 3 评论 0原文

我有带有可变存储值的bash脚本,以python list格式:

business_unit = [“ a”,“ b”,“ c,d”,“ e f”]

并且需要循环要获得:

A
B
C,D
E F

脚本应将逗号,空格和任何特殊字符保留在双引号中。我已经尝试使用sed,ifs,但是它也在弦乐中分配逗号的线。

I have bash script with a variable storing values in sort of a python-list format :

Business_Unit=["A","B","C,D","E F"]

and need to loop over to get:

A
B
C,D
E F

The script should keep the commas, spaces and any special characters within the double quotes. I've tried using sed, IFS but it was splitting the line for commas within a string too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拍不死你 2025-02-02 04:40:48

SED为我做到了:

echo "$Business_Unit" |
sed 's/^[^"]*"//;s/"\]//;s/","/\n/g' |
#    ----------- ------- ----------
#       |           |        |
#       |           |       substitute "," by a new line
#       |          remove the last quotes and ]
#       remove everything upto and including the first " 
while read line ; do
    echo "$line"
done

Sed does it for me:

echo "$Business_Unit" |
sed 's/^[^"]*"//;s/"\]//;s/","/\n/g' |
#    ----------- ------- ----------
#       |           |        |
#       |           |       substitute "," by a new line
#       |          remove the last quotes and ]
#       remove everything upto and including the first " 
while read line ; do
    echo "$line"
done
秉烛思 2025-02-02 04:40:48
string='Business_Unit=["A","B","C,D","E F"]'
awk -F\" '{for(i=2; i<=NF; i+=2) print $(i)}' <<< $string
A
B
C,D
E F
string='Business_Unit=["A","B","C,D","E F"]'
awk -F\" '{for(i=2; i<=NF; i+=2) print $(i)}' <<< $string
A
B
C,D
E F
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文