arrays - 从文本文件逐行读取并输出到数组 bash

标签 arrays linux bash shell while-loop

我正在尝试从 .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/

相关文章:

bash - 是否有 mac 的 rgrep 别名

bash - 有人可以解释这个 bash : “_ {}\;” 末尾符号的内部工作原理吗

c++ - 大型3D阵列性能: contiguous 1D storage vs T***

javascript - 多维关联 javascript 对象

php - UTF-8贯穿始终

linux - 自停尾操作

arrays - 算法 - 查找数组的最佳元素

java - 如何在http请求头中发送签名

linux - 如何临时将名称解析为本地主机端口?

linux - 无法使用 Bash 脚本连接到 POP3 服务器