曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 5467|回复: 0
打印 上一主题 下一主题

统计一篇文章里同一个单词的个数

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34395
跳转到指定楼层
楼主
发表于 2010-9-3 00:21:31 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
假设一文件 1.txt 的内容为:
A native of Honolulu, Hawaii, Obama is a graduate of Columbia University and Harvard Law School, where he was the president of the Harvard Law Review. He was a community organizer in Chicago before earning his law degree. He worked as a civil rights attorney in Chicago and taught constitutional law at the University of Chicago Law School from 1992 to 2004.

Obama served three terms in the Illinois Senate from 1997 to 2004. Following an unsuccessful bid against a Democratic incumbent for a seat in the U.S. House of Representatives in 2000, he ran for United States Senate in 2004. Several events brought him to national attention during the campaign, including his victory in the March 2004 Democratic primary and his keynote address at the Democratic National Convention in July 2004. He won election to the U.S. Senate in November 2004. His presidential campaign began in February 2007, and after a close campaign in the 2008 Democratic Party presidential primaries against Hillary Rodham Clinton, he won his party's nomination. In the 2008 general election, he defeated Republican nominee John McCain and was inaugurated as president on January 20, 2009.

As president, Obama signed economic stimulus legislation in the form of the American Recovery and Reinvestment Act in February 2009. Other domestic policy initiatives include the Patient Protection and Affordable Care Act, a major piece of health care reform legislation which he signed into law in March 2010, and the Dodd-Frank Wall Street Reform and Consumer Protection Act, which forms part of his financial regulatory reform efforts, which he signed in July 2010. In foreign policy, Obama gradually withdrew combat troops from Iraq, increased troop levels in Afghanistan, and signed an arms control treaty with Russia. On October 8, 2009, Obama was named the 2009 Nobel Peace Prize laureate.
找出这篇短文里包含 Obama 的单词的个数:
tr -s ' ' '\n' < 1.txt | grep -c 'Obama'
上面命令的意思是,1.txt 的文字流输入到 tr 命令,然后该命令使用 -s 参数对于所有重复出现的空格符替换为换行符。也就是说,文章单词是每个一行的出现,如:
A
native
of
Honolulu
然后,grep 命令的 -c 参数,只要发现本行匹配 Obama ,就会进行统计。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2025-5-4 05:20 , Processed in 0.060919 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表