我正在尝试从 .txt 中读取行并将文本中的每一行复制到之前使用 ARRAY 创建的自己的文件夹
cat stores/locations.txt |while read -r LINE;do
echo "$LINE" > county/${ARRAY[${i}]}/localstores.txt
done
当我运行它时,它只在目录 county 中创建 1 个文件,其中包含商店的一行联系信息,但我真正想要它做的是将文件放入 ARRAY 的每个元素而不是父文件夹 county .
每行数据包括以下内容:
<storeid> <storename> <amountofEmployees> <nameofManager>
我 super 卡住了,非常感谢您的帮助!
最佳答案
一个简单的方法如下所示,它为每一行创建一个新文件,并且还可以选择将该行添加到数组中。
declare -A myArray()
while IFS= read -r line # Read a line
do
touch mytargetpath/"$line.txt" # Creates a new file for each of the line in the desired path
myArray+=("$line") # Append line to the array
done < stores/location.txt
并将数组内容打印为:-
# Print the file (print each element of the array)
for e in "${myArray[@]}"
do
echo "$e"
done
关于arrays - 从文本文件逐行读取并输出到数组 bash,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38426257/