将 2 个文件合并到第三个文件中,使用列作为索引并合并行

发布于 2024-12-27 23:35:50 字数 825 浏览 6 评论 0原文

我一直在学习 awk,遇到一个我无法解决的问题,如果可以的话请帮忙。

我有 2 个使用 awk、sort 和 uniq -c 生成的文件。

文件 1 的格式为:

1 aaa.c 10/10/2010

1 bbb.h 1/1/2011

3 ccc.c 2/2/2012

1 ccc.c 20/6/2011

1 ddd.c 1/1/2010

1 ddd.c 2/4/1999

1 ddd.c 7/1/2012

1 ddd.c 10/1/1977

含义: number_of_equal_files name date(因此,来自同一日期的 3 个文件 ccc.c 和来自另一个日期的 1 个文件 ccc.c)

文件 2 的格式为:

4 ddd.c

2 ccc.c

3 xxx.c

含义: number_of_ different_dates 名称(因此,ccc.c 已被发现具有 2 个不同的日期)->我使用反向 grep 删除了 number=1 的文件,所以不会有任何

我想要做的是生成格式为

number_of_ different_dates name date1 date2 date 3 date4 (...) 的

第三个文件喜欢:

2 ccc.c 2/2/2012 20/6/2011 

4 ddd.c 1/1/2010 2/4/1999 7/1/2012 10/1/1977

提前致谢!

I've been studying awk and i've come upon a problem i'm not being able to solve, please help if you can.

I have 2 files I generated using awk, sort and uniq -c.

File 1 is in the format:

1 aaa.c 10/10/2010

1 bbb.h 1/1/2011

3 ccc.c 2/2/2012

1 ccc.c 20/6/2011

1 ddd.c 1/1/2010

1 ddd.c 2/4/1999

1 ddd.c 7/1/2012

1 ddd.c 10/1/1977

Meaning: number_of_equal_files name date (so, 3 files ccc.c from the same date and 1 file ccc.c from another)

File 2 is in the format:

4 ddd.c

2 ccc.c

3 xxx.c

Meaning: number_of_different_dates name (so, ccc.c has been found with 2 different dates) -> files that would have number=1 i removed usind a reverse grep, so there won't be any

What i'd like to do is to generate a third file in the format

number_of_different_dates name date1 date2 date 3 date4 (...)

something like:

2 ccc.c 2/2/2012 20/6/2011 

4 ddd.c 1/1/2010 2/4/1999 7/1/2012 10/1/1977

Thanks in advance!

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

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

发布评论

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

评论(2

嗳卜坏 2025-01-03 23:35:50

您应该能够仅使用第一个文件作为输入来获得该结果。下面使用两个关联数组。第一个收集文件被查看的次数,第二个收集日期。 END 块仅打印出现多次的条目。

{
   counts[$2] += 1;
   dates[$2] = sprintf( "%s %s", dates[$2], $3 );
}

END {
   for ( f in dates ) {
      if ( counts[f] > 1 )
     printf( "%d %s %s\n", counts[f], f, dates[f]);
   }
}

You should be able to get that result using only the first file as input. The following uses two associative arrays. The first collects the number of times a file is seen and the second collects the dates. The END block just prints the entries that appeared more than once.

{
   counts[$2] += 1;
   dates[$2] = sprintf( "%s %s", dates[$2], $3 );
}

END {
   for ( f in dates ) {
      if ( counts[f] > 1 )
     printf( "%d %s %s\n", counts[f], f, dates[f]);
   }
}
泅渡 2025-01-03 23:35:50

你可以尝试这样的事情 -

#!/usr/bin/awk -f

NR==FNR{
            a[$3]=$2; b[$2]++;next
       } 

($2 in b){
            printf ("%s %s ", $1,$2);
            for (i in a) 
                if (a[i]==$2) 
                    printf i" "; print ""
          }

测试:

[jaypal:~/Temp] cat file1
1 aaa.c 10/10/2010

1 bbb.h 1/1/2011

3 ccc.c 2/2/2012

1 ccc.c 20/6/2011

1 ddd.c 1/1/2010

1 ddd.c 2/4/1999

1 ddd.c 7/1/2012

1 ddd.c 10/1/1977

[jaypal:~/Temp] cat file2
4 ddd.c

2 ccc.c

3 xxx.c

[jaypal:~/Temp] ./s.awk ff1 ff2
4 ddd.c 10/1/1977 1/1/2010 2/4/1999 7/1/2012 

2 ccc.c 20/6/2011 2/2/2012 

You can try something like this -

#!/usr/bin/awk -f

NR==FNR{
            a[$3]=$2; b[$2]++;next
       } 

($2 in b){
            printf ("%s %s ", $1,$2);
            for (i in a) 
                if (a[i]==$2) 
                    printf i" "; print ""
          }

Test:

[jaypal:~/Temp] cat file1
1 aaa.c 10/10/2010

1 bbb.h 1/1/2011

3 ccc.c 2/2/2012

1 ccc.c 20/6/2011

1 ddd.c 1/1/2010

1 ddd.c 2/4/1999

1 ddd.c 7/1/2012

1 ddd.c 10/1/1977

[jaypal:~/Temp] cat file2
4 ddd.c

2 ccc.c

3 xxx.c

[jaypal:~/Temp] ./s.awk ff1 ff2
4 ddd.c 10/1/1977 1/1/2010 2/4/1999 7/1/2012 

2 ccc.c 20/6/2011 2/2/2012 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文