我需要将单词表单文件存储在其他变量中。就像我有一个test.txt
包含以下内容的文件:
uname : null empty amarjeet sharma
ver : null 1.2 empty 1.3 1.4
txn : null 123 124 empty 125
需要将冒号之前的单词存储在一个变量中,并以每行的形式保留在数组中。
示例:uname
存储在var中,而ramaining存储在value[]
数组中
var = uname
value[] ={null, empty, amarjeet, sharma}
var1 = ver
value1[] = {null, 1.2, empty, 1.3, 1.4}
etc…
试试这个:
declare -A hash # create associative array
declare -a array # create array
# first part: read file to associative array hash
c=0
while IFS=" :" read hash[$c,0] hash[$c,1]; do
((c++))
done < filename
# second part: print hash and array
for ((i=0;i<$c;i++)); do
# create array from hash with part right from :
array=(${hash[$i,1]})
echo "${hash[$i,0]} -> ${array[@]}"
done
输出:
uname-> null空的Amarjeet Sharma ver-> null 1.2空1.3 1.4 txn->空123124空125
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句