我只想找到重复的单词并计算它们,但忽略那些唯一的单词(非重复的单词)

我只想找到重复的单词并计算它们,但忽略那些唯一的单词(非重复的单词)

这是这样的命令:

echo "zebra ant spider spider ant zebra ant dog shark" | xargs -n1 | sort | uniq -c | xargs

结果:

3 ant 1 dog 1 shark 2 spider 2 zebra

我想忽略唯一的单词(狗和鲨鱼,因此它们不是重复的单词)并仅查找具有计数的重复单词。

答案1

$ echo "zebra ant spider spider ant zebra ant dog shark" |\
  fmt -1 | sort | uniq -cd | xargs echo
3 ant 2 spider 2 zebra

答案2

不需要xargs

echo $(printf '%s\n' zebra ant spider spider ant zebra ant dog shark | 
       sort | uniq -dc)

输出:

3 ant 2 spider 2 zebra

答案3

使用 gnu awk 进行 PROCINFO :

echo "zebra ant spider spider ant zebra ant dog shark" |
awk '
  BEGIN { PROCINFO[ "sorted_in" ] = "@ind_str_asc" }
  { for ( i = 1 ; i <= NF ; i++ ) a[$i]++ }
  END {
    for ( i in a ) if( a[i] > 1 ) {
      b = b c a[i] " " i
      c = " "
    }
    print b
  }'

答案4

我已经通过以下方法完成了

命令

echo "zebra ant spider spider ant zebra ant dog shark" | sed "s/ /\n/g"| awk '{a[$1]++}END{for(x in a) print x,a[x]}'| awk '$2 != "1" {print $0}'

输出

echo "zebra ant spider spider ant zebra ant dog shark" | sed "s/ /\n/g"| awk '{a[$1]++}END{for(x in a) print x,a[x]}'| awk '$2 != "1" {print $0}'

zebra 2
ant 3
spider 2

相关内容