排序方式使用awk字段长度...不工作 [英] sort by field length with awk... not working

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

问题描述

我有我需要的第一个字段的长度订购制表符分隔的文件。我发现了一行应该做的,对我的样本,但它给人非常奇怪的结果:

I have a tab-separated file I need to order by the length of the first field. I've found samples of a line that should do that for me, but it's giving very strange results:

awk -F\t '{print length($1) " " $0|"sort -rn"}' SpanishGlossary.utf8 | sed 's/^.[^>]*>/>/' > test.tmp

...给出了这样的(几重presentative样本 - 这是一个非常长的文件):

... gives this (several representative samples -- it's a very long file):

56 cafés especiales y orgánicos special and organic coffees
56 amplia experiencia gerencial broad managerial experience
55 una fundada confianza en que a well-founded confidence that
55 Servicios de Desarrollo Empresarial  Business Development Services
...
6 son estas are these
6 son entregadas a  are given to
6 son determinantes para    are crucial for
6 son autolimitativos   are self-limiting
...
0 tal grado de  such a degree of
0 tales such
0 tales propósitos  such purposes
0 tales principios  such principles
0 tales o cuales    this or that

这导致数量应该是第一个字段的长度,但它显然不是。我不知道的什么的那个的计数。

That leading number should be the length of the first field, but it's obviously not. I don't know what that's counting.

我是什么做错了吗?谢谢你。

What am I doing wrong? Thanks.

推荐答案

试试这个:

awk '$0=length($1) FS $0' file | sort -nr | sed -r 's/^\S*\s//'

测试:

kent$  cat f
as foo
a foo
aaa foo
aaaaa foo
aaaa foo

kent$  awk '$0=length($1) FS $0' f|sort -nr|sed -r 's/^\S*\s//'
aaaaa foo
aaaa foo
aaa foo
as foo
a foo

在这里我用空间(默认值)作为awk的 FS ,如果你需要在设置页添加 -F'\\ t'

添加一个AWK(GNU AWK)为@Jaypal只有一个内胆,

add one awk (gnu awk) only one-liner for @Jaypal,

我提到的gawk的,因为它有ASORT和asorti,我们可以使用排序。

I mentioned gawk, because it has asort and asorti which we could use for sorting.

我也改变了输入文件中添加一些相同的长度( $ 1 )线。

also I changed the input file to add some same length ($1) lines.

更好@ val_num_asc说明 asorti(A,B, ...)

kent$  cat f
as foo
a foo
aaa foo
ccc foo
aaaaa foo
bbbbb foo
aaaa foo

kent$  awk '{a[length($1)"."NR]=$0}END{asorti(a,b);for(i=NR;i>0;i--)print a[b[i]]}' f
bbbbb foo
aaaaa foo
aaaa foo
ccc foo
aaa foo
as foo
a foo

这篇关于排序方式使用awk字段长度...不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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