我的文件是sort -nr
命令
10000 a, b, c, d
110 c, d, e, f
11 e, f, e, d
来自包含许多行的文件,如下所示
a, b, c, d
a, b, c, d
a, b, c, d
a, b, c, d
a, b, c, d
c, d, e, f
e, f, e, d
然后应用此命令cat file | sort | uniq -c | sort -nr
。 (我希望你能得到这张照片)。
我想将结果更改为
10000, a, b, c, d
110, c, d, e, f
11, e, f, e, d
我正在寻找一个单行内联unix命令来清理前面的空间填充并将第一个空格更改为逗号。
答案 0 :(得分:1)
如果你这样使用awk
该怎么办?
$ awk '{a[$0]++} END{for (i in a) print a[i], i}' a | sort -nr
5 a, b, c, d
1 e, f, e, d
1 c, d, e, f
这样你可以指出计数器和线本身之间的分隔符:
$ awk '{a[$0]++} END{for (i in a) print a[i], i}' OFS="->" a | sort -nr
5->a, b, c, d ^^^^^^^^
1->e, f, e, d
1->c, d, e, f
{a[$0]++}
),$0
,跟踪它出现的次数。这是使用包含a[]
。a[line]=times
完成的
END{for (i in a) print a[i], i}
完成处理文件后,打印计数器+行。OFS="whatever"
表示适用的字段分隔符。使用print
时,在这种情况下。如果您仍想使用sort
,请使用sed
执行更改:
$ sort a | uniq -c | sort -nr | sed -r 's/^[ ]*([0-9]*) /\1 -> /'
5 -> a, b, c, d
1 -> e, f, e, d
1 -> c, d, e, f
这会捕获前导空格后的第一个数字块,并将其与->
一起写回。