如何存储在一个数组元素的空白? [英] How to store elements with whitespace in an array?
问题描述
只是想知道,假设我存储在一个以下列格式称为BookDB.txt文件我的数据:
Just wondering, assuming I am storing my data in a file called BookDB.txt in the following format :
C++ for dummies:Jared:10.52:5:6
Java for dummies:David:10.65:4:6
由此每个字段由定界符分隔:
whereby each field is seperated by the delimeter ":".
我多么preserve空白的第一场,并有包含以下内容的数组:('傻瓜C ++'的Java傻瓜') ?
How would I preserve whitespace in the first field and have an array with the following contents : ('C++ for dummies' 'Java for dummies')?
任何帮助非常多AP preciated!
Any help is very much appreciated!
推荐答案
Ploutox的解决方案几乎是正确的,但没有设置IFS,你不会得到你所寻求的数组,在这种情况下,两个元素。
Ploutox's solution is almost correct, but without setting IFS, you will not get the array that you seek, with two elements in this case.
注意:他纠正了这个帖子后,他的解决方案。
Note: He corrected his solution after this post.
IFS=$'\n': arr=( $(awk -F':' '{print $1 }' Input.txt ) )
echo ${#arr[@]}
echo ${arr[0]}
echo ${arr[1]}
输出:
2
C++ for dummies
Java for dummies
这篇关于如何存储在一个数组元素的空白?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!