繁体   English   中英

bash从文件中读取带空格的带引号的元素,并将其存储到数组中

[英]bash read quoted element with space from a file and store them into an array

这可能是一个愚蠢的问题,但我找不到答案,我正在搜索一个多小时。

我有一行文件:

"Snow In Paradise" "Tomorrow Sea" "Edge Time" "etc ..."

我希望能够从另一个线程完全像此示例一样进行操作:

## declare an array variable
declare -a arr=("elem ent1" "e lement2" "element3")

## now loop through the above array
for i in "${arr[@]}"
do
   echo "$i"
   # or do whatever with individual element of the array
done

bash-4.2$ ./test
elem ent1
e lement2
element3

这应该做到(使用grep ):

IFS=$'\n' arr=($(grep -o '"[^"]*"' file))
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM