使用awk来排序字段,并安排 [英] Using awk to sort fields and arrange

查看:111
本文介绍了使用awk来排序字段,并安排的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着在此刻学习AWK,我想做一个特定的任务。我的问题是在范围上一个$ P $类似pviously贴(使用awk来转列排),但不会为我的数据相当的工作。我一直在试图找出原因和IM确保其相当简单。

我有一个制表符分隔表中大量的数据只有两个字段(下面的示例):

  1101 \\ t7778
1101 \\ t7755
1101 \\ t8889
1101 \\ t6789
2300 \\ t122​​0
4000 \\ t2333
4000 \\ t7555
4000 \\ T9000
4000 \\ t1111

和我要结束了追加的第二个字段到行场比赛时。所需的输出是:

  1101 \\ t7778 \\ t7755 \\ t8889 \\ t6789
2300 \\ t122​​0
4000 \\ t2333 \\ t7555 \\ T9000 \\ t1111

如果可能的话,我还想得到命令中所有部件的交代,所以我可以在未来的理解。先谢谢了。


解决方案

 的awk'{名单[$ 1] =列表[$ 1]\\ t的$ 2}
     END {为(我的列表)的printf%s%S \\ n,我,列表[我]}'数据

第一行增加了一个选项卡,第二场到清单按 $ 1 索引元素。第二行打印出项和值的累积列表。

示例输出:

  1101 7778 7755 8889 6789
4000 2333 7555 9000 1111
2300 1220

如果你想在第一列排序,可以通过管道输出排序-n 。如果你有GNU AWK ,您可以调查内置的排序功能也:

 的/ usr / GNU /斌/的awk'{名单[$ 1] =列表[$ 1]\\ t的$ 2}
                  END {N = asorti(表,索引);
                        对于(i = 1; I< = N;我++)
                            printf的%s%S \\ n,索引[I],列表[索引[I]
                      }数据

排序输出:

  1101 7778 7755 8889 6789
2300 1220
4000 2333 7555 9000 1111

Im trying to learn awk at the moment and I want to do a specific task. My question is similar in scope to one previously posted(Using awk to transpose column to row), but wouldn't quite work for my data. I have been trying to work out why and im sure its quite simple.

I have large data in a tab delimited table with only two fields (example below):

1101\t7778
1101\t7755
1101\t8889
1101\t6789
2300\t1220
4000\t2333
4000\t7555
4000\t9000
4000\t1111

and I want to end up appending the second field onto a row when the field matches. The desired output would be:

1101\t7778\t7755\t8889\t6789
2300\t1220
4000\t2333\t7555\t9000\t1111

If possible, Id like to get an explaination of all the parts within the command so I can understand it in the future. Thanks in advance.

解决方案

awk '    { list[$1] = list[$1] "\t" $2 }
     END { for (i in list) printf "%s%s\n", i, list[i] }' data

The first line adds a tab and the second field to the list element indexed by $1. The second line prints out the key and the accumulated list of values.

Sample output:

1101    7778    7755    8889    6789
4000    2333    7555    9000    1111
2300    1220

If you want the first column sorted, you can pipe the output through sort -n. If you have GNU awk, you can investigate the built-in sort function too:

/usr/gnu/bin/awk '    { list[$1] = list[$1] "\t" $2 }
                  END { n = asorti(list, indexes);
                        for (i = 1; i <= n; i++)
                            printf "%s%s\n", indexes[i], list[indexes[i]]
                      }' data

Sorted output:

1101    7778    7755    8889    6789
2300    1220
4000    2333    7555    9000    1111

这篇关于使用awk来排序字段,并安排的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆