zeuux

zeuux的博客

他的个人主页  他的博客

★ ☆ ★ 小弟有个问题求大家解答 ,计算文中单词总数?★ ☆ ★

zeuux  2012年04月24日 星期二 21:17 | 2155次浏览 | 16条评论

★ ☆ ★ 小弟有个问题求大家解答 ,计算文中单词总数?★ ☆ ★

有这么一个文件 test.txt,内容如下:
--------------------------------------------------------------------
freebsd is a strong man, linux is a virgin(im a freebsd user now:)
but freebsdis better, if you say
"i love Michael Jackson",i think you must be a freebsd user.
it is because MJ is a member of freebsdcommuity.
and why? cuz MJ had donated $1000,000 to freebsd community.
wow ,here you go

Love life, use freebsd now! And you must have a better life in Africa..
--------------------------------------------------------------------
现在需要计算出 test.txt 中 单词 freebsd 的总数!
> grep -c -w 'freebsd' < test.txt
4
我目前只能做到上面这个样子,但是 grep 是以行来计算的,所以 第一行中有2个 freebsd
计数结果是1 ,不知道 应该怎么解决呢?


评论

我的评论:

发表评论

请 登录 后发表评论。还没有在Zeuux哲思注册吗?现在 注册 !
贾志龙

回复 贾志龙  2012年04月28日 星期六 19:20

awk '{sum += gsub("freebsd", "frebsd")}END{print sum}' test.txt

0条回复

周正

回复 周正  2012年04月25日 星期三 21:10

第三种:
awk 'BEGIN {RS="dn" } {gsub("freebsd","freebsd\n") ;print $0 }' test.txt|grep -c -w "freebsd"

1条回复

周正

回复 周正  2012年04月25日 星期三 21:03

另一种:
perl -p -e 's/freebsd/freebsd\n/' test.txt | grep -c -w 'freebsd'

0条回复

周正

回复 周正  2012年04月25日 星期三 10:55

sed 's/freebsd/freebsd\n/g' text.txt |grep -c -w 'freebsd'
用sed把同在一行中的freebsd拆开

6条回复

徐继哲

回复 徐继哲  2012年04月24日 星期二 22:40

$ cat test.txt | grep -o freebsd | wc -l

4条回复

  • zeuux

    回复 zeuux  2012年04月25日 星期三 05:36

    呵呵,多谢!
    ------------------------------------
    > grep -o -w 'freebsd' < bb | wc -l
    5
    ------------------------------------
    ‘-w
    ‘--word-regexp’
    Select only those lines containing matches that form whole words. The test
    is that the matching substring must either be at the beginning of the line, or
    preceded by a non-word constituent character. Similarly, it must be either at
    the end of the line or followed by a non-word constituent character. Wordconstituent
    characters are letters, digits, and the underscore.

    ‘-o’ (man 里面缺少后面半句, 不然就知道用它了。。悲剧!我是FreeBSD系统!)
    ‘--only-matching’
    Print only the matched (non-empty) parts of matching lines, with each such
    part on a separate output line.
    ------------------------------------

    3条回复

暂时没有评论

Zeuux © 2024

京ICP备05028076号