Linux shell 脚本中表格的单元格求和

发布于 2024-10-06 08:13:03 字数 370 浏览 0 评论 0原文

我有一组采用以下格式的表格:

1000 3 0 15 14
2000 3 0 7 13
3000 2 3 14 12
4000 3 1 11 14
5000 1 1 9 14
6000 3 1 13 11
7000 3 0 10 15

它们位于简单的文本文件中。

我想将这些文件合并到相同格式的新表中,其中每个单元格 (X,Y) 是原始表集中所有单元格 (X,Y) 的总和。一个稍微复杂的因素是第一列中的数字不应该被求和,因为这些是标签。

我怀疑这可以用 AWK 来完成,但我不是特别熟悉这种语言,并且在网上找不到解决方案。如果有人建议使用其他工具,那也没关系。

我想从 bash shell 脚本执行此操作。

I have a set of tables in the following format:

1000 3 0 15 14
2000 3 0 7 13
3000 2 3 14 12
4000 3 1 11 14
5000 1 1 9 14
6000 3 1 13 11
7000 3 0 10 15

They are in simple text files.

I want to merge these files into a new table in the same format, where each cell (X,Y) is the sum of all cells (X,Y) from the original set of tables. One slightly complicating factor is that the numbers from the first column should not be summed, since these are labels.

I suspect this can be done with AWK, but I'm not particularly versed in this language and can't find a solution on the web. If someone suggests another tool, that's also fine.

I want to do this from a bash shell script.

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

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

发布评论

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

评论(3

も让我眼熟你 2024-10-13 08:13:03

尝试一下:

#!/usr/bin/awk -f
{
    for (i=2;i<=NF; i++)
        a[$1,i]+=$i
    b[$1]=$1
    if (NF>maxNF) maxNF=NF
}

END {
    n=asort(b,c)
    for (i=1; i<=n; i++) {
        printf "%s ", b[c[i]]
        for (j=2;j<=maxNF;j++) {
            printf "%d ", a[c[i],j]
        }
        print ""
    }
}

像这样运行它:

./sumcell.awk table1 table2 table3

./sumcell.awk table*

两次使用示例输入的输出将如下所示:

$ ./sumcell.awk table1 table1
1000 6 0 30 28
2000 6 0 14 26
3000 4 6 28 24
4000 6 2 22 28
5000 2 2 18 28
6000 6 2 26 22
7000 6 0 20 30

Give this a try:

#!/usr/bin/awk -f
{
    for (i=2;i<=NF; i++)
        a[$1,i]+=$i
    b[$1]=$1
    if (NF>maxNF) maxNF=NF
}

END {
    n=asort(b,c)
    for (i=1; i<=n; i++) {
        printf "%s ", b[c[i]]
        for (j=2;j<=maxNF;j++) {
            printf "%d ", a[c[i],j]
        }
        print ""
    }
}

Run it like this:

./sumcell.awk table1 table2 table3

or

./sumcell.awk table*

The output using your example input twice would look like this:

$ ./sumcell.awk table1 table1
1000 6 0 30 28
2000 6 0 14 26
3000 4 6 28 24
4000 6 2 22 28
5000 2 2 18 28
6000 6 2 26 22
7000 6 0 20 30
琴流音 2024-10-13 08:13:03

对每一行求和,假定每行至少有一个数字列。

while read line ; do
    label=($line)
    printf ${label[0]}' ' ;
    expr $(
        printf "${label[1]}"
        for c in "${label[@]:2}" ; do
            printf ' + '$c
        done 
    )
done < table

编辑:当然,我没有看到关于基于标签进行组合的评论,所以这是不完整的。

Sum each line, presuming at least one numeric column on each line.

while read line ; do
    label=($line)
    printf ${label[0]}' ' ;
    expr $(
        printf "${label[1]}"
        for c in "${label[@]:2}" ; do
            printf ' + '$c
        done 
    )
done < table

EDIT: Of course I didn't see the comment about combining based on the label, so this is incomplete.

他夏了夏天 2024-10-13 08:13:03
perl -anE'$h{$F[0]}[$_]+=$F[$_]for 1..4}{say$_,"@{$h{$_}}"for sort{$a<=>$b}keys%h' file_1 file_2
perl -anE'$h{$F[0]}[$_]+=$F[$_]for 1..4}{say$_,"@{$h{$_}}"for sort{$a<=>$b}keys%h' file_1 file_2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文