首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 互联网 >

用Google AJAX Search API对互联网下Linux命令出现次数排名

2012-08-07 
用Google AJAX Search API对互联网上Linux命令出现次数排名用Google AJAX Search API对互联网上Linux命令

用Google AJAX Search API对互联网上Linux命令出现次数排名
用Google AJAX Search API对互联网上Linux命令出现次数排名

本文链接:http://codingstandards.iteye.com/blog/798861? (转载请注明出处)

基本思路

  最近开始编写《我使用过的Linux命令系列》博文,忽然想到一个问题,在互联网上到底哪些Linux命令更热一些,或者介绍的文章多些。基本思路如下:

  首先确定有哪些命令,为了得到尽可能全面的命令列表,我用VMWare把RHEL5.5进行了完整的安装,安装好之后利用Bash提供的命令补全功能,得到了命令列表,见附件rhel55.txt,具体做法是在Bash命令行上直接按TAB键然后不断按空白键列出所有命令。

  然后得到每个命令出现的次数,开始时我做了一下尝试,在Google上手工输入了一些命令,关键字选择“linux 命令”的形式。比如adduser命令采用的关键字就是“linux adduser”,可以得到有255,000条结果,中文页面有137,000条结果。但是这样手工一条条做,显然工作量太大,因此想到了Google Search API,在其上注册了一个账号,然后编写Bash脚本测试。经过一些尝试,发现这种方式还是可行的:对每个命令进行遍历,执行Google搜索得到估计数量,输出到文件中。关于Google AJAX Search API在Linux命令行下的使用方法详见这里。

  最后将得到的命令统计数量文件进行排序,就大功告成了。

Bash脚本

Bash脚本文件 estimate.sh

#!/bin/sh# usage: google_search <STR># Google搜索google_search(){REF="http://codingstandards.iteye.com/"KEY="ABQIAAAAHg_ENG5Yq9pOZd19v64gyxTMcdcN4KfyGCBxustvF1FXdNe4WBQOej_ZiBgIK6-a4M3hTxcVfSkt2g"STR="$1"# 采用网页搜索#curl -e $REF "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=$STR&hl=zh-CN" 2>/dev/null# 采用博客搜索#curl -e $REF "http://ajax.googleapis.com/ajax/services/search/blogs?v=1.0&q=$STR&hl=zh-CN" 2>/dev/null#curl --retry 5 -e $REF "http://ajax.googleapis.com/ajax/services/search/blogs?v=1.0&q=$STR&hl=zh-CN" 2>/dev/nullcurl --retry 5 -e "$REF" "http://ajax.googleapis.com/ajax/services/search/blogs?v=1.0&q=$STR&hl=zh-CN&key=$KEY" 2>/dev/null}# usage: get_estimate_count <CMD># 得到指定命令的Google搜索结果的估计数量get_estimate_count(){CMD=$1# 得到google搜索的结果#RESPONSE_DATA=$(google_search "Linux+%2B$CMD+%2B命令")RESPONSE_DATA=$(google_search "Linux+%2B"$CMD命令"")#echo $RESPONSE_DATAecho $RESPONSE_DATA >temp/$CMD.json# 将"estimatedResultCount":"左边的部分截掉ESTIMATE_COUNT=${RESPONSE_DATA#*"estimatedResultCount":"}#echo $ESTIMATE_COUNT# 有时候会没有结果,比如对add_jar3进行搜索时if [ "$RESPONSE_DATA" == "$ESTIMATE_COUNT" ]; then#echo -# 不输出任何东西returnfi# 将"右边的部分截掉ESTIMATE_COUNT=${ESTIMATE_COUNT%%"*}#echo $ESTIMATE_COUNTecho $ESTIMATE_COUNT}#get_estimate_count add_jar3#get_estimate_count sesearch#get_estimate_count in# usage: proc_cmdproc_cmd(){CMD=$1NUM=$(get_estimate_count $CMD)# 发现有时候并不能得到数值,因此只有返回数值的时候才打印信息if [ "$NUM" ]; thenprintf "%-36s\t%12s\n"$CMD$NUMelseecho $CMD >&2echo $CMD >>failed.datfi}# usage: estimate_file <FILE># 对指定文件中的命令进行Google搜索,显示成“命令 数量”的格式estimate_file(){FILE=$1MAX_PROCESSES=1CUR_PROCESSES=0for CMD in $(cat $FILE)do# 没有处理过的命令才进行搜索if ! grep -q "$CMD " estimate.dat; thenproc_cmd $CMD &CUR_PROCESSES=$[CUR_PROCESSES+1]if [ "$CUR_PROCESSES" == "$MAX_PROCESSES" ]; thenwaitCUR_PROCESSES=0fifidoneif [ "$CUR_PROCESSES" != "0" ]; thenwaitfi}# usage: estimate_files <FILE1> <FILE2> ...# 对多个文件进行数量统计estimate_files(){for FILE in "$@"doestimate_file $FILEdone}# usage:mainmain(){# 对当前目录下的所有txt文件进行处理#estimate_files ?.txt | tee estimate.dat#estimate_files "$@" | tee estimate.datestimate_files "$@" | tee -a estimate.dat# 按照数量倒序排列sort -k 2 -n -r estimate.dat >estimate_sorted.dat}if [ "reset" == "$1" ]; thenrm -rf temprm -f failed.datrm -f estimate.dat estimate_sorted.datelserm -f failed.datmkdir -p temp#main ?.txt#main rhel55.txtmain "$@"fi
?

使用方式:./estimate.sh <command-file>

比如:./estimate.sh rhel55.txt

这个需要很长时间运行,我是使用screen命令来运行的。

运行结果

排在前100位的Linux命令统计数据如下

?

数据文件 estimate_sort.dat

for                                     14717552php                                      3633635POST                                     3500295if                                       3105116free                                     3077812GET                                      2754997Mail                                     2546814X                                        2354642c++                                      2078740install                                  1663794then                                     1496335look                                     1382716do                                       1382077red                                      1297329while                                    1296227r++                                      1278640ip                                       1159486g++                                      1090350co                                       1072202jobs                                     1064459screen                                   1061419case                                     1038589command                                  1020028complete                                  969708touch                                     939152times                                     929702true                                      922490done                                      881188chat                                      843574else                                      837265talk                                      830200patch                                     804871fi                                        734431spam                                      703822open                                      703104break                                     699024until                                     694978continue                                  687704python                                    670149groups                                    670066wish                                      658274HEAD                                      649301star                                      623613return                                    595490du                                        595178function                                  583288clear                                     557484history                                   553110compare                                   531711amd                                       519778join                                      504890cut                                       490316ruby                                      482702sudo                                      481834firefox                                   448990apt                                       432220replace                                   431444export                                    425227pr                                        422921reboot                                    411582bash                                      409009ar                                        408693ac                                        408108clock                                     406453kill                                      399793reset                                     390244import                                    382678wall                                      382328crash                                     381835states                                    361991exit                                      356256gcc                                       344048newer                                     333310factor                                    319654column                                    312702spent                                     301818ci                                        290611paste                                     286709accept                                    280039grub                                      275932false                                     274413arch                                      273525flex                                      269829flex++                                    268928sort                                      266867weather                                   259220ab                                        248634nl                                        244664chmod                                     226770osx                                       226762cancel                                    223539fc                                        219973telnet                                    216743batch                                     215047units                                     207773yum                                       200619alternatives                              193568wget                                      190392emacs                                     190248tux                                       189157

?

不足之处

  在使用Google Search API的进行网页数量估算的时候,发现数量不太准确,比如直接使用Google搜索得到的数量与通过Google Search API得到的数量不一致,差别还蛮大。后来在Google上搜索一下,发现很多人提到了这个问题,说得到的estimatedResultCount并不准确。

  还有就是,通过关键字“linux 命令”的方式得到的搜索结果中,并不是所有的页面都是来关于这个Linux命令的,比如通过"linux complete"这个关键字搜索到的主要是介绍Linux参考大全的页面。这也是美中不足的地方。后来我还尝试过使用"linux adduser命令"这种形式的关键字,或者不采用网页搜索而采用博客搜索。都无法彻底解决这个问题。

  列位看官,如果您有什么好主意,请留下您的想法。

?

1 楼 superlittlefish 2011-07-02   牛,拜读了. 2 楼 codingstandards 2011-07-04   superlittlefish 写道牛,拜读了.
谢谢支持!

热点排行