![](/img/trans.png)
[英]Read strings from file and store them in multiple variables with 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.