有一个文件2.txt 内容如下
That is all right, this is a great job.
如何计算这个文件中单词“is“出现的次数。
像grep ' is ' 2.tx5 -o |wc -l 这样的就不要了,因为“空格ls空格“这种方法来匹配一个单词太笨了。
honbj 回复于:2005-08-29 14:09:16
引用:原帖由 "Axin" 发表: 有一个文件2.txt 内容如下
That is all right, this is a great job.
如何计算这个文件中单词“is“出现的次数。
像grep ' is ' 2.tx5 -o |wc -l 这样的就不要了,因为“空格ls空格“这种方法来匹配一个单..........
:em06:
waker 回复于:2005-08-29 14:42:23
grep -e '\bis\b' -o
不过不能处理连字符的情况
Axin 回复于:2005-08-29 15:03:38
grep中-w 参数本来是可以比较好的来区分单词的。
如果is是一句话中最后一个词呢? 空格is空格就匹配不上了。
dradhzn 回复于:2005-08-29 15:18:05
cat file | xargs -n1 | sort | uniq -c
Axin 回复于:2005-08-29 15:47:33
引用:原帖由 "dradhzn"]cat file | xargs -n1 | sort | uniq -c 发表:
也好像没有解决如果is是一句话最后一个单词的问题。
waker 回复于:2005-08-29 15:54:55
xargs -n1 <tf|grep '^is$'
Axin 回复于:2005-08-29 17:10:25
考虑了好长时间,觉得像[^a-zA-Z]is[^a-zA-Z]这样匹配可能会好点。
angleeye 回复于:2005-08-30 09:58:36
引用:原帖由 "Axin"]这样匹配可能会好点。 发表:
grep "is" -o [-w]
不就对了吗?搞那么多?
echo "he is a good man , is and is" | grep -o "is"
is
is
is
3个
newgame8061 回复于:2005-08-30 10:14:01
xargs -n1 <3|grep -w is|wc -l
Axin 回复于:2005-08-30 11:48:57
引用:原帖由 "angleeye" 发表: 投粤寺穑扛隳敲炊啵
echo "he is a good man , is and is" | grep -o "is"
is
is
is
3个
你试试这个
echo "This a good man , is and is" | grep -o "is"
angleeye 回复于:2005-08-30 13:07:58
引用:原帖由 "Axin" 发表:
你试试这个
echo "This a good man , is and is" | grep -o "is"
明白楼主了, :D
echo "This a good man , is and is" | grep -e "\<is\>" --color=always
这个好象可以, :em02:
網中人 回复于:2005-08-30 13:39:58
try this:
cat file | tr ' ' '\n' | grep -w is | wc -l
or even better:
cat file | xargs -n1 | grep -wc is
yearnx 回复于:2005-08-30 14:15:53
这个问题也困扰了我很长时间,发现如果用sed/awk好像都无法解决,不过perl应该可以吧?
welcome008 回复于:2005-08-30 15:44:31
如果单词不跨行的话:
awk '{for(i=1;i<=NF;i++) print $i}' filename|grep is|wc -l好像也可以。
Axin 回复于:2005-08-30 16:01:47
引用:原帖由 "angleeye" 发表:
明白楼主了, :D
echo "This a good man , is and is" | grep -e "\<is\>" --color=always
这个好象可以, :em02:
是的,我查过正则式,发现上面应该是比较有效的方法。
'\bis\b'好像也行。
waker 回复于:2005-08-30 16:06:32
\b与\<>解决不了
there is is-
land的问题
r00to 回复于:2005-08-30 16:27:10
这样行不呢?
cat "That is all right, this is a great job.">is
cat is |xarg -n1 |awk '{if($1~/is/) print $1}' |awk '{print NR" "$1}
Axin 回复于:2005-08-30 16:31:33
xarg -n1 这个语句受到很多限制。比如里面有引号啊什么的时候。。
Axin 回复于:2005-08-30 16:40:15
查找jim的时候,jim's这个算不算?
林子 回复于:2005-08-30 16:44:25
grep -c " is"
Axin 回复于:2005-08-30 16:46:19
引用:原帖由 "林子"]grep -c " is" 发表:
is如果是某行的第一个,你这个就不行。
还有issue
林子 回复于:2005-08-30 16:57:54
grep -c "is "
wayy2008 回复于:2005-08-30 17:06:41
引用:原帖由 "waker" 发表: \b与\<>解决不了
there is is-
land的问题
嗯,老大就是老大,思考的就是全面. :em03:
Axin 回复于:2005-08-30 17:09:49
引用:原帖由 "林子"]grep -c "is " 发表:
"is" is a verb, that is.
honbj 回复于:2005-08-30 17:19:34
引用:原帖由 "網中人" 发表: try this:
cat file | tr ' ' '\n' | grep -w is | wc -l
cat file | xargs -n1 | grep -wc is
To this file
cat file
this is an is but not isa is-
cat file | tr ' ' '\n' | grep -w is | wc -l
输出是 :3
cat file | xargs -n1 | grep -wc is
输出是 :3
是不是应该是
cat file | xargs -n1 | grep '^is$' -c
得到正确的输出:2
網中人 回复于:2005-08-30 22:25:08
嗯... 加上 ^ $ 更精確.
因為 word 的界定與 field 的界定是不用的.
若從 RE 來解:
expand file | egrep -o ' is |^is | is$|^is$' | wc -l
biary 回复于:2005-08-31 09:26:14
但是考虑到"is-"这种情形,不知道大家有没有考虑"is."这种情况,当is是一句的最后一个单词时,后面应该跟句号,这时候也应计数。
另外, solaris上grep/egrep没有-o选项,郁闷... :cry: :em10:
zhuzhzh 回复于:2005-08-31 10:29:22
这个只能单独匹配,然后和^is$匹配的加起来
引用:原帖由 "biary" 发表: 但是考虑到"is-"这种情形,不知道大家有没有考虑"is."这种情况,当is是一句的最后一个单词时,后面应该跟句号,这时候也应计数。
另外, solaris上grep/egrep没有-o选项,郁闷... :cry: :em10:
yearnx 回复于:2005-08-31 14:42:12
我中午的时候用这个可以做了:
sed 's/ /\n/g' file | grep -c "^is&"
dinner3000 回复于:2005-08-31 20:30:10
awk也可以:
1 echo "\"Is there a tree? Yeah, there is a tree. How green the tree is.\""
2 echo "Is there a tree? Yeah, there is a tree. How green the tree is." \
3 |awk -F" " '
4 BEGIN{
5 count=0;
6 }
7 {
8 for(i=0;i<NF;i++){
9 if($i~/Is/ || $i~/is/){
10 if(length($i)==2){
11 count++;
12 }else if(length($i==3)){
13 if(substr($i,1,1)!~/[a-zA-Z]/ || substr($i,3,1)!~/[a-zA-Z]/){
14 count++;
15 }
16 }else{
17 continue;
18 }
19 }else{
20 continue;
21 }
22 }
23 }
24 END{
25 printf("There are %d \"is\" in this text.\n",count);
26 }
27 '
Axin 回复于:2005-09-01 08:22:35
引用:原帖由 "yearnx" 发表: 我中午的时候用这个可以做了:
sed 's/ /\n/g' file | grep -c "^is&"
这个显然不行。
“is” 这个单词你肯定抓不住。
有一个问题:什么才是一个单词的标准?比如you're 这个简写有没有匹配you这个单词?
对于Unix本身的处理方式来说,他算是匹配了。
还有one-and-a-half 这个怪物也算匹配了单词and。
我们自己的标准又是?
網中人 回复于:2005-09-01 09:58:14
摟主連單詞的判定都沒清楚, 那, 再設計怎麼寫命令, 都是多餘的...
honbj 回复于:2005-09-01 10:04:15
引用:原帖由 "網中人"]摟主連單詞的判定都沒清楚, 那, 再設計怎麼寫命令, 都是多餘的... 发表:
同意 如果没有确定标准 任何解法都是徒劳 觉得有些时候是方法而不是知识出了问题
yearnx 回复于:2005-09-01 10:05:08
我贴的sed办法,是针对你给出的文档的。
引用:原帖由 "Axin" 发表:
这个显然不行。
“is” 这个单词你肯定抓不住。
有一个问题:什么才是一个单词的标准?比如you're 这个简写有没有匹配you这个单词?
对于Unix本身的处理方式来说,他算是匹配了。
还有one-and-a-hal..........
不过你提出的问题很对,我觉得解决这种文档的最好办法就是逐个排除特殊字符了。
Axin你有什么好办法么?
Axin 回复于:2005-09-01 11:38:08
我没有什么好办法。
我觉得可能还是用作\bis\b过滤,
如果觉得is-xx这样的不算的话,那就再用grep -v 把这些带"-"的再过滤掉。。
and so on...
|