我試圖在bash中的兩組數組中存儲一個.txt文件的內容。該文件是給定數據文件的特徵列表,由豎線(|)分隔。到目前爲止,我已經編寫了讀取文件的代碼,並分別打印每行數據,每行都跟隨該行的給定部分。將數據存儲在多個數組中(bash)
#prints line of text and then separated version
while IFS='' read -r line || [[ -n "$line" ]]
do
echo "Text read from file: $line"
words=$(echo $line | tr "|" "\n")
for tests in $words
do
echo "> $tests"
done
done < "$1"
輸出示例:
Text read from file: this|is|data|in|a|file
> this
> is
> data
> in
> a
> file
Text read from file: another|example|of|data
> another
> example
> of
> data
是否有辦法讓我數據的每個單獨的線存儲在一個陣列中,然後它的分解部分內的另一?我想這可能是使用循環,但我很困惑數組使用bash(新手)。
你可能會對此都錯了 - 見[爲什麼 - 是 - 使用 - 一個殼 - 環 - 到 - 過程 - 文本視爲-bad實踐](https://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice)。 –