使用不同分隔符时 Bash cut 命令中的空格字符问题
我已经设置|作为 cut
命令的分隔符,但空格字符似乎仍然被解释为分隔符。
这是我的测试脚本:
people[1]="Mr|Smith"
people[2]="Mrs|Jane Brown"
for person in ${people[@]}
do
title=$(echo $person | cut -f1 -d\|)
name=$(echo $person | cut -f2 -d\|)
echo $title $name
echo
done
哪些输出:
Mr Smith
Mrs Jane
Brown Brown
任何人都可以解释为什么 Jane Brown 中的空格字符会导致问题吗?
谢谢 西蒙
I have set | to be the delimiter for a cut
command, but space characters seem to still be interpreted as a delimiter too.
Here's my test script:
people[1]="Mr|Smith"
people[2]="Mrs|Jane Brown"
for person in ${people[@]}
do
title=$(echo $person | cut -f1 -d\|)
name=$(echo $person | cut -f2 -d\|)
echo $title $name
echo
done
Which outputs:
Mr Smith
Mrs Jane
Brown Brown
Can anyone shed some light on why the space character in Jane Brown is causing problems?
Thanks
Simon
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
${people[@]}
用双引号引起来:否则,
Mrs|Jane Brown
将被解释为 2 个单独的标记:Mrs|Jane
和棕色
。将其视为:
与
Enclose
${people[@]}
in double quotes:Otherwise,
Mrs|Jane Brown
will get interpreted as 2 separate tokens:Mrs|Jane
andBrown
.Think of it as:
versus