awk 列出元素

发布于 2024-12-20 12:53:13 字数 384 浏览 0 评论 0原文

我有一个这样的列表:

s1   d2
s1   d4
s3   d2
s4   d1
s1   d3 
s4   d1
s5   d6
s3   d5
s1   d2
s1   d3

对于第一列 (s_) 中的每个元素,我需要获取第二列 (d_) 中的元素列表出现顺序。在这种情况下:

s1  d2 d4 d3 d2 d3
s3  d2 d5
s4  d1 d1    
s5  d6 

s_ 的顺序并不重要,d_ 的顺序很重要。 你能建议一个简单而快速的方法来做到这一点(因为列表很大),也许是在 awk 中?

I have a list like this:

s1   d2
s1   d4
s3   d2
s4   d1
s1   d3 
s4   d1
s5   d6
s3   d5
s1   d2
s1   d3

I need to obtain, for every element in the first column (s_) the list of element in the second column (d_) in the same order of appearance. In this case:

s1  d2 d4 d3 d2 d3
s3  d2 d5
s4  d1 d1    
s5  d6 

The order of the s_ is not important, the order of the d_ is.
Can you suggest a simple and fast approach to do it (because the list is large), maybe in awk?

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

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

发布评论

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

评论(4

幸福不弃 2024-12-27 12:53:15

给你:

 awk '{ ss[$1]++ ; ds[$1 NR]=$2 } 
      END { for ( e in ss ) 
                { a=e 
                  for (i=1;i<=NR;i++) 
                      { a=a " " ds[e i] }  
                  printf("%s\n",gensub("  +"," ","g",a))
                }
      }' INPUTFILE

HTH

Here you go:

 awk '{ ss[$1]++ ; ds[$1 NR]=$2 } 
      END { for ( e in ss ) 
                { a=e 
                  for (i=1;i<=NR;i++) 
                      { a=a " " ds[e i] }  
                  printf("%s\n",gensub("  +"," ","g",a))
                }
      }' INPUTFILE

HTH

财迷小姐 2024-12-27 12:53:15

这将保证键和值的顺序:

awk 'END {
  for (i = 0; ++i <= c;)
    print idx[i], s[idx[i]]
  }
{
  s[$1] = s[$1] ? s[$1] OFS $2 : $2
  t[$1]++ || idx[++c] = $1
  }' infile

This would guarantee the order of both keys and values:

awk 'END {
  for (i = 0; ++i <= c;)
    print idx[i], s[idx[i]]
  }
{
  s[$1] = s[$1] ? s[$1] OFS $2 : $2
  t[$1]++ || idx[++c] = $1
  }' infile
╄→承喏 2024-12-27 12:53:14

我会使用关联数组来记住“sX”,然后对该值进行字符串连接。

BEGIN {
print "ID\tList\n";
}
{
id[$1]=id[$1] $2;
}
END{
for (var in id)
    print var,"\t",id[var];
}

I would use an associative array to memorize the "sX" and then do string concatenation on the value.

BEGIN {
print "ID\tList\n";
}
{
id[$1]=id[$1] $2;
}
END{
for (var in id)
    print var,"\t",id[var];
}
極樂鬼 2024-12-27 12:53:14

也许是这样的(对于命令行):

awk '{ vals[$1] = vals[$1] " " $2 }; END { for (key in vals) { print key,vals[key] }}' list

格式化为 awk 脚本更漂亮:

{ vals[$1] = vals[$1] " " $2 }
END {
    for (key in vals) {
        print key,vals[key]
    }
}

它的作用是通过第一个值的索引存储一个包含右侧渐进值的字符串。因此,每次它找到一个,都会将其连接到该字符串的末尾。最后,它打印出每一对。

Something like this, perhaps (for the command line):

awk '{ vals[$1] = vals[$1] " " $2 }; END { for (key in vals) { print key,vals[key] }}' list

Formatted prettier as an awk script:

{ vals[$1] = vals[$1] " " $2 }
END {
    for (key in vals) {
        print key,vals[key]
    }
}

What this does is store, by index of the first values, a string that contains the progressive values on the right side. So each time it finds one, it concatenates it to the end of that string. Then at the end, it prints each pair out.

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