linux - 使用 Bash 用 IFS 分割字符串?

标签 linux bash split ifs

我有一个像这样的字符串:

Spain-South Africa:2-1

我想将其拆分为:

Spain-South Africa
2-1

我尝试将其分割为 IFS=':'但它给了我:

Spain-South

Africa
2-1

我的代码:

code

最佳答案

无法重现,但您可能没有为 read 命令正确设置 IFS,或者没有正确显示输出。

$ str="Spain-South Africa:2-1"
$ IFS=: read -ra results <<< "$str"
$ declare -p results
declare -a results=([0]="Spain-South Africa" [1]="2-1")

根据您短暂的评论,您想要类似的东西

while IFS=: read -ra results; do
    ...
done < "$1"

而不是

for str in $(cat "$1"); do
    ...
done

关于linux - 使用 Bash 用 IFS 分割字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59359700/

相关文章:

linux - 使用 sed 删除 Linux 中文本文件任意行的前五个字符

linux - 添加用户 : Specify only one name in this mode

方案中的正则表达式和转义字符

dart - 颤动/Dart : Split string by first occurrence

linux - xubuntu 插件区域中缺少 Dropbox 图标

linux - qtcreator 在 "stop"发送的信号

linux - tcp keepalive - 协议(protocol)不可用?

ruby-on-rails - 为什么我的 Git Bash 不能访问 "bundle"?

bash - `uniq` 没有对巨大的文本文件进行排序?

python - 在 Python 中提取序列