如何将 while read 行循环的内容输出到 bash 中的多个数组?

发布于 2024-11-17 00:15:24 字数 427 浏览 3 评论 0原文

我读取目录的文件并将每个文件名放入数组中(搜索) 然后,我使用循环遍历数组中的每个文件名(搜索),并使用 while read 行循环打开它们,并将每一行读入另一个数组(文件计数)。我的问题是它有一个巨大的数组,有 39 行(每个文件有 13 行),我需要它是 3 个独立的数组,其中 filecount1[line1] 是第一个文件的第一行,依此类推。这是到目前为止我的代码...

typeset -A files
for file in ${SEARCH[@]}; do
    while read line; do
        files["$file"]+="$line"
    done < "$file"
done

所以,感谢伊万提供的这个例子!但是我不确定我是否遵循如何将其放入单独的数组中,因为在这个示例中,所有数组是否仍被命名为“文件”?

I read the files of a directory and put each file name into an array (SEARCH)
Then I use a loop to go through each file name in the array (SEARCH) and open them up with a while read line loop and read each line into another array (filecount). My problem is its one huge array with 39 lines (each file has 13 lines) and I need it to be 3 seperate arrays, where
filecount1[line1] is the first line from the 1st file and so on. here is my code so far...

typeset -A files
for file in ${SEARCH[@]}; do
    while read line; do
        files["$file"]+="$line"
    done < "$file"
done

So, Thanks Ivan for this example! However I'm not sure I follow how this puts it into a seperate array because with this example wouldnt all the arrays still be named "files"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柏拉图鍀咏恒 2024-11-24 00:15:24

如果您只是尝试将文件内容存储到数组中:

declare -A contents
for file in "${!SEARCH[@]}"; do
    contents["$file"]=$(< $file)
done

如果您想将各个行存储在数组中,您可以创建一个伪多维数组:

declare -A contents
for file in "${!SEARCH[@]}"; do
    NR=1
    while read -r line; do
        contents["$file,$NR"]=$line
        (( NR++ ))
    done < "$file"
done

for key in "${!contents[@]}"; do 
    printf "%s\t%s\n" "$key" "${contents["$key"]}"
done

If you're just trying to store the file contents into an array:

declare -A contents
for file in "${!SEARCH[@]}"; do
    contents["$file"]=$(< $file)
done

If you want to store the individual lines in a array, you can create a pseudo-multi-dimensional array:

declare -A contents
for file in "${!SEARCH[@]}"; do
    NR=1
    while read -r line; do
        contents["$file,$NR"]=$line
        (( NR++ ))
    done < "$file"
done

for key in "${!contents[@]}"; do 
    printf "%s\t%s\n" "$key" "${contents["$key"]}"
done
孤云独去闲 2024-11-24 00:15:24

第 6 行

$filecount[$linenum]}="$line" 

似乎在 $ 之后缺少 {
应该是:

${filecount[$linenum]}="$line" 

如果上述情况为真,那么它正在尝试将输出作为命令运行。
第 6 行是(在上面“修复”它之后):

${filecount[$linenum]}="$line"

但是 ${filecount[$linenum]} 是一个,并且您不能对一个值
应该是:

filecount[$linenum]="$line"

现在我很困惑,因为 { 是否实际上丢失了,或者 } 是实际的拼写错误:S :P


顺便说一句,bash 也支持这种语法

filecount=$((filecount++)) # no need for $ inside ((..)) and use of increment operator ++

这应该有效:

typeset -A files
for file in ${SEARCH[@]}; do       # foreach file 
    while read line; do            # read each line
        files["$file"]+="$line"    # and place it in a new array
    done < "$file"                 # reading each line from the current file
done

一个小测试表明它有效

# set up
mkdir -p /tmp/test && cd $_
echo "abc" > a
echo "foo" > b
echo "bar" > c

# read files into arrays
typeset -A files
for file in *; do 
    while read line; do
        files["$file"]+="$line" 
    done < "$file"
done

# print arrays
for file in *; do
    echo ${files["$file"]}
done

# same as:
echo ${files[a]}     # prints: abc
echo ${files[b]}     # prints: foo
echo ${files[c]}     # prints: bar

line 6 is

$filecount[$linenum]}="$line" 

Seems it is missing a {, right after the $.
Should be:

${filecount[$linenum]}="$line" 

If the above is true, then it is trying to run the output as a command.
Line 6 is (after "fixing" it above):

${filecount[$linenum]}="$line"

However ${filecount[$linenum]} is a value and you can't have an assignment on a value.
Should be:

filecount[$linenum]="$line"

Now I'm confused, as in whether the { is actually missing, or } is the actual typo :S :P


btw, bash supports this syntax too

filecount=$((filecount++)) # no need for $ inside ((..)) and use of increment operator ++

This should work:

typeset -A files
for file in ${SEARCH[@]}; do       # foreach file 
    while read line; do            # read each line
        files["$file"]+="$line"    # and place it in a new array
    done < "$file"                 # reading each line from the current file
done

a small test shows it works

# set up
mkdir -p /tmp/test && cd $_
echo "abc" > a
echo "foo" > b
echo "bar" > c

# read files into arrays
typeset -A files
for file in *; do 
    while read line; do
        files["$file"]+="$line" 
    done < "$file"
done

# print arrays
for file in *; do
    echo ${files["$file"]}
done

# same as:
echo ${files[a]}     # prints: abc
echo ${files[b]}     # prints: foo
echo ${files[c]}     # prints: bar
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文