Chinaunix

标题: 重复字符串 [打印本页]

作者: 1cpuer    时间: 2017-11-22 16:17
标题: 重复字符串
this is tecmint, where you get the best good tutorials, how to's, guides, tecmint.                                                                                       
this are is good.
good the apple ''.

重复字符串
this :2
tecmint :2
good : 3
......



作者: nomethod    时间: 2017-11-22 16:25
  1. awk -vRS='[^A-z]' 'NF{a[$0]++}END{for(i in a)print i":",a[i]}' a
  2. tutorials: 1
  3. you: 1
  4. best: 1
  5. this: 2
  6. apple: 1
  7. good: 3
  8. get: 1
  9. the: 2
  10. to: 1
  11. guides: 1
  12. tecmint: 2
  13. how: 1
  14. where: 1
  15. are: 1
  16. s: 1
  17. is: 2
复制代码

作者: 1cpuer    时间: 2017-11-22 17:28
本帖最后由 1cpuer 于 2017-11-22 17:41 编辑

回复 2# nomethod

NF{a[$0]++}
这个地方不大懂
是不是数组 a 赋值给NF

# 只打印重复的再写一个

作者: wh7211    时间: 2017-11-22 19:59
本帖最后由 wh7211 于 2017-11-22 20:01 编辑

回复 1# 1cpuer


  1. awk '{for(i=1;i<=NF;i++){gsub("[\047,.]","",$i);if($i){a[$i]++}}}END{for(j in a){print j": "a[j]}}' file
复制代码

输出:
tos: 1
tutorials: 1
you: 1
best: 1
this: 2
apple: 1
good: 3
get: 1
the: 2
guides: 1
tecmint: 2
how: 1
where: 1
are: 1
is: 2
作者: 1cpuer    时间: 2017-11-22 21:45
回复 4# wh7211

写的可以2例多用到了类似

{for(j in a){print j": "a[j]}


只有1个词的不重复的不要打印行不


作者: jason680    时间: 2017-11-22 21:55
回复 3# 1cpuer

http://bbs.chinaunix.net/thread-2309494-1-1.html

作者: 1cpuer    时间: 2017-11-23 07:25
this: 2
good: 3
the: 2
tecmint: 2
is: 2

想要如上结果的话怎么写
作者: 本友会机友会摄友会    时间: 2017-11-23 11:56
提示: 作者被禁止或删除 内容自动屏蔽
作者: wh7211    时间: 2017-11-23 14:06
回复 5# 1cpuer


  1. awk '{for(i=1;i<=NF;i++){gsub("[\047,.]","",$i);if($i){a[$i]++}}}END{for(j in a){if(a[j]>1){print j": "a[j]}}}' file
复制代码

输出:
this: 2
good: 3
the: 2
tecmint: 2
is: 2

作者: 1cpuer    时间: 2017-11-23 15:21
回复 9# wh7211

领悟了
if(a[j]>1)
我是用 sed '/.*1$/d'
行未 1 的删或不显
亦可用 gsub 表示
只怪自己 awk 太差了

作者: 龙牙地主天    时间: 2017-11-23 16:27
  1. awk '{gsub(/\W/," ") ;for(i=1;i<=NF;i++) a[$i]++}END{for(j in a) if(a[j]>1) print j":"a[j]}' file
复制代码





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2