我有一个包含数字列表的input.txt文件:
1719
194
1719
1719
194
1135
194我想使用grep管道创建一个output.txt,以便按照出现次数的升序对它们进行排序,即:
194: 3 times
1719: 2 times
1135: 1 time.有什么建议吗?
发布于 2011-08-02 18:20:30
假设数字在6910460.txt中,不包含空行:
$ cat 6910460.txt | sort | uniq -c | sort -nr
3 194
2 1719
1 1135或者,如果您还需要文本"times“,您可以附加一个awk命令:
$ cat 6910460.txt | sort | uniq -c | sort -nr | \
awk 'BEGIN {FS=OFS=" "} \
{temp=$2; $2=$1; $1=temp} {printf "%4i %4i time(s)\n", $1, $2}'它将打印:
194 3 time(s)
1719 2 time(s)
1135 1 time(s)发布于 2011-08-02 18:45:48
awk '
{count[$0]++}
END {for (n in count) {print n ": " count[n] " times"}}
' file |
sort -nr -k2发布于 2011-08-02 18:21:44
echo "1719
194
1719
1719
194
1135
194" | sort -n | uniq -c
3 194
1 1135
3 1719这就足够了吗,或者您可以自己交换值吗?
https://stackoverflow.com/questions/6910460
复制相似问题