awk 列出元素
我有一个这样的列表:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
给你:
HTH
Here you go:
HTH
这将保证键和值的顺序:
This would guarantee the order of both keys and values:
我会使用关联数组来记住“sX”,然后对该值进行字符串连接。
I would use an associative array to memorize the "sX" and then do string concatenation on the value.
也许是这样的(对于命令行):
格式化为 awk 脚本更漂亮:
它的作用是通过第一个值的索引存储一个包含右侧渐进值的字符串。因此,每次它找到一个,都会将其连接到该字符串的末尾。最后,它打印出每一对。
Something like this, perhaps (for the command line):
Formatted prettier as an awk script:
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.