统计/etc/init.d/functions文件中每个单词的出现次数,并排序(用grep和sed两种方法分别实现)

方法一:grep实现

grep -o "\<[[:alpha:]]\+\>" /etc/init.d/functions |sort |uniq -c
grep -o '[[:alpha:]]\+' /etc/init.d/functions | sort |uniq -c
[root@centos7 data]# grep  -o "\<[[:alpha:]]\+\>" /etc/init.d/functions |sort |uniq -c21 a5 A1 aA1 abnormally5 action1 active1 ActiveState1 adjust1 alive2 all1 already2 and1 And3 any1 anywhere1 Apply1 are1 arg2 at1 avoid7 awk8 b1 backup2 bak29 base2 basename2 bash7 be6 BEGIN1 bg13 bin11 binary1 bit2 booleans1 boot21 BOOTUP2 break3 but1 by6 c1 caller3 can8 case1 cat1 centi2 cgexec6 cgroup1 Cgroups4 check2 Check2 checkpid1 cmdline1 code13 color2 COLUMNS3 command1 condrestart5 conf2 configuration1 confusing1 consoletype4 CONSOLETYPE2 contains9 continue1 Convert1 core4 corelimit1 could1 current27 d5 daemon1 daemons3 dead1 debug1 deci1 declared1 default11 delay1 deleted1 dependencies1 details17 dev1 dies1 directory1 dirname10 do1 doc1 doesn10 done2 due1 dump6 e49 echo1 Echo3 eE1 elif9 else5 en1 END5 eq1 errors8 esac16 etc2 Evaluate1 exe3 exists4 exit1 exited1 export1 expression1 eyes12 f1 fail1 failed1 FAILED7 failure4 fF51 fi23 file3 files1 filter2 find1 Find2 First2 foo12 for5 force2 found1 from1 fsck4 function2 functions1 further1 g1 G1 generated1 Get4 gotbase1 graphical3 grep1 h2 had1 have2 honor5 i60 if1 ignore1 IGNORECASE1 ignored25 in1 including1 Inform6 init1 initialize2 initscripts1 installed2 insufficient1 into11 is6 it1 its1 keyword4 kill1 Kill1 KILL8 killlevel4 killproc1 ksh1 l1 lang1 let1 lets2 level3 lib7 line1 little1 lL1 LoadState37 local1 locale2 lock1 locked5 Log1 LOGLEVEL2 Look1 looks1 Looks10 LSB2 lt3 m1 make1 Make1 matching1 may1 micro1 mili1 most38 n1 nano5 ne7 nice4 nicelevel3 NICELEVEL3 nN1 NOLOCALE6 not14 null13 o7 of2 OK2 old1 on5 only3 oO2 option3 options3 or2 orig1 otherwise2 our1 out1 output1 Output24 p2 passed1 PASSED1 path2 PATH46 pid11 pidfile2 pidfileofproc4 pidof2 pidofproc3 pids1 PIDs1 piko1 plural4 plymouth5 PPID7 printf1 privilege1 privileges7 proc1 processes1 profile8 prog15 program5 Program1 propagating3 q8 r11 rc19 RC2 read1 Read1 readlink1 reality1 recognizes4 reload2 Reloading1 RemainAfterExit4 remaining1 Remove1 requested3 restart1 Restarting3 ret54 return1 returns1 Returns8 retval1 rm1 rpm2 rpmnew2 rpmorig2 rpmsave1 rR1 RS8 run1 Run8 running1 runuser12 s1 S1 same2 sane2 Save3 sbin1 screen1 script1 scripts1 search1 second2 seconds2 sed2 See1 seem1 self3 serial6 service3 set2 Set1 settings1 sh1 share1 shell1 Shell18 shift2 should4 show4 shutdown2 shvar2 signal3 sleep1 smhd1 some3 something1 sourcing1 specified2 sS3 start1 Starting2 startup9 stat1 state6 status3 stderr4 stime2 stop1 stopped1 Stopping5 STRING2 strstr2 style1 SubState3 subsys1 succeeded5 success2 sure5 syntax7 sysconfig15 sysctl1 system11 systemctl8 systemd2 t1 TERM3 test5 Test1 TEXTDOMAIN5 that13 the52 then2 they1 this2 This1 time10 to9 try2 tT1 txt1 ulimit2 umask1 unit1 unknown1 unless1 unset2 up1 update9 Usage1 use3 used1 Useful9 user1 using5 usr1 uU2 v1 value7 var6 verbose4 via3 warning1 WARNING1 We1 where1 whether3 while1 width1 will2 with14 x1 xcondrestart1 xforce1 xreload1 xrestart1 xstart1 xstop1 xtry3 yes2 yY28 z

方法二:sed实现

下面命令,这样做,居然还会有空行~~~~

sed -n 's/[^[:alpha:]]\+/\n/gp' /etc/init.d/functions |sort |uniq -c

第二个命令删掉空行

cat /etc/init.d/functions |sed -n 's/[^[:alpha:]]\+/\n/gp' |sed '/^$/d' |sort |uniq -c |sort -n |wc -l

第三个命令。

sed -n 's/[^[:alpha:]]\+/\n/gp' /etc/init.d/functions |sed '/^$/d' /etc/init.d/functions |sort |uniq -c |wc -l

看看这三个命令执行的结果是什么——统计总行数

凌乱,第三行的结果和前面两个不一样,418行。。。。。

执行结果:

[root@centos7 data]# sed -n 's/[^[:alpha:]]\+/\n/gp' /etc/init.d/functions |sort |uniq -c956 21 a5 A1 aA1 abnormally5 action1 active1 ActiveState1 adjust1 alive2 all1 already2 and1 And3 any1 anywhere1 apply1 Apply1 are3 arg2 at1 avoid7 awk8 b1 backup2 bak38 base2 basename2 bash7 be6 BEGIN1 bg13 bin11 binary1 bit2 booleans2 boot21 BOOTUP2 break3 but1 by6 c1 caller3 can8 case1 cat1 centi2 cgexec6 cgroup3 CGROUP1 Cgroups4 check2 Check2 checkpid5 checkpids1 cmdline2 code8 COL13 color2 COLUMNS3 command1 condrestart5 conf2 configuration1 confusing1 consoletype4 CONSOLETYPE2 contains9 continue1 convert1 Convert1 core1 COREFILE4 corelimit1 could1 current27 d5 daemon4 DAEMON1 daemons3 dead1 debug1 deci1 declared1 default11 delay1 deleted1 dependencies1 DEPENDENCIES1 details17 dev1 dies3 dir1 directory1 dirname1 discard10 do1 doc1 doesn10 done2 due1 dump6 e57 echo1 Echo3 eE1 elif9 else5 en1 END5 eq1 errors8 esac16 etc2 Evaluate1 exe3 exists4 exit1 exited1 export1 expression1 eyes18 f2 fail1 failed1 FAILED9 failure3 FAILURE1 false4 fF46 fi58 file4 files1 filter2 find1 Find2 First2 foo12 for5 force2 found1 from1 fsck4 function2 functions1 further1 g1 G1 generated1 Get4 gotbase1 graphical3 grep1 h2 had1 have2 honor6 i60 if1 ignore1 IGNORE1 IGNORECASE7 ignored25 in1 including1 Inform6 init1 initialize2 initscripts1 installed2 insufficient1 into18 is6 it1 its1 keyword34 kill1 Kill1 KILL8 killlevel4 killproc1 ksh1 l1 lang3 LANGSH1 let1 lets2 level3 lib1 LIMIT7 line16 list1 little1 lL1 LoadState37 local1 locale8 lock1 locked5 Log1 LOGLEVEL2 Look1 looks1 Looks10 LSB2 lt7 m1 make1 Make1 matching1 may1 micro1 mili1 most6 MOVE39 n1 nano5 ne7 nice4 nicelevel3 NICELEVEL3 nN1 NOLOCALE6 NORMAL6 not14 null13 o7 of2 OK2 old1 on5 only3 oO2 option3 options3 or2 orig1 otherwise2 our1 out1 output1 Output24 p4 passed1 PASSED1 path2 PATH73 pid11 pidfile2 pidfileofproc8 pidof2 pidofproc21 pids1 PIDs1 piko1 plural4 plymouth5 PPID7 printf1 privilege1 privileges8 proc1 processes1 profile8 prog15 program5 Program1 propagating3 q8 r11 rc19 RC2 read1 Read1 readlink1 reality1 recognizes2 redirect1 REDIRECT4 reload2 Reloading1 RemainAfterExit4 remaining1 Remove1 requested2 RES3 restart1 Restarting3 ret54 return1 returns1 Returns8 retval1 rm1 rpm2 rpmnew2 rpmorig2 rpmsave1 rR1 RS14 run1 Run8 running1 runuser12 s1 S1 same2 sane2 Save3 sbin1 screen1 script1 scripts1 search1 sec1 second2 seconds3 sed2 See1 seem1 self3 serial6 service3 set2 Set16 SETCOLOR1 settings1 sh1 share1 shell1 Shell18 shift2 should4 show4 shutdown2 shvar2 signal1 SKIP3 sleep1 smhd1 some3 something3 SOURCED1 sourcing1 specified2 sS1 stage3 start1 Starting2 startup9 stat1 state6 status3 stderr13 stime2 stop1 stopped1 Stopping5 STRING2 strstr2 style1 SubState3 subsys1 succeeded7 success3 SUCCESS2 sure5 syntax7 sysconfig16 sysctl1 system16 systemctl2 SYSTEMCTL8 systemd2 t7 term1 TERM3 test5 Test1 TEXTDOMAIN5 that13 the52 then2 they1 this2 This1 time10 to6 TO1 true9 try2 tT1 txt1 ulimit2 umask1 unit1 unknown1 unless1 unset2 up2 update9 Usage4 use3 used1 Useful9 user1 using5 usr1 uU2 v1 value13 var6 verbose4 via5 warning5 WARNING1 We1 where1 whether3 while1 width1 will2 with14 x1 xcondrestart1 xforce1 xreload1 xrestart1 xstart1 xstop1 xtry3 yes2 yY28 z

这两个命令执行并比对之后:

哈哈哈,两种用法结果居然不一样~~~~~~

我觉得是因为sed用法,遇到不包括单词的就换行,导致行数比grep用法的多~~~~~

第四周作业——统计/etc/init.d/functions文件中每个单词的出现次数,并排序(用grep和sed两种方法分别实现)相关推荐

  1. 调用MapReduce对文件中各个单词出现的次数进行统计

    调用MapReduce对文件中各个单词出现的次数进行统计 实验配置:环境:Hadoop | 软件:Eclipse | Ubuntu系统 | ------------------------------ ...

  2. python统计文件中每个单词出现的次数_Python统计单词出现的次数

    题目: 统计一个文件中每个单词出现的次数,列出出现频率最多的5个单词. 前言: 这道题在实际应用场景中使用比较广泛,比如统计历年来四六级考试中出现的高频词汇,记得李笑来就利用他的编程技能出版过一本背单 ...

  3. python统计文件中每个单词出现的次数_python统计文本中每个单词出现的次数

    .python统计文本中每个单词出现的次数: #coding=utf-8 __author__ = 'zcg' import collections import os with open('abc. ...

  4. 统计文件中每个单词出现的次数

    tr -s "[ \n]" <file| tr " " "\n"|tr -dc "[a-z][A-Z]\n"|so ...

  5. 请将第4章例4-6中的问卷调查结果用文本文件result保存, 并编写程序读该文件然后统计各评语出现的次数,再将最终统计结果追加至esultxt文件中

    1.请将第4章例4-6中的问卷调查结果用文本文件"result"保存, 并编写程序读该文件然后统计各评语出现的次数,再将最终统计结果追加至"esultxt"文件 ...

  6. Java统计1到300_java程序员的从0到1:统计某字符串在某文件中出现的次数(面试题)...

    目录: 1. 编程题目 2. 方法一 3. 方法二 4. 方法三 5. 方法四 6. 总结 正文: 1. 编程题目 写一个方法,输入一个文件名和一个字符串,统计这个字符串在这个文件中出现的次数. 2. ...

  7. 多元统计分析matlab,MATLAB的统计工具箱中的多元统计分析中提供了聚类分析的两种方法...

    MATLAB的统计工具箱中的多元统计分析中提供了聚类分析的两种方法: 1.层次聚类hierarchical clustering 2.k-means聚类 这里用最简单的实例说明以下层次聚类原理和应用发 ...

  8. python统计中文字符的个数_python统计中文字符数量的两种方法

    方法一: def str_count(str): '''找出字符串中的中英文.空格.数字.标点符号个数''' count_en = count_dg = count_sp = count_zh = c ...

  9. Python案例:两种方法实现词频统计

    Python案例:两种方法实现词频统计 一.利用字典实现词频统计 1.编写源代码 2.查看运行结果 二.利用collections的Counter模块实现词频统计 <

最新文章

  1. NFV — 安全策略
  2. UA MATH571B 试验设计III 单因素试验设计1
  3. java checked异常有那些,java checked exceptions
  4. 判断scrollview是否滚动到了底部
  5. easyui 插入中间行
  6. TortoiseGit上传项目到GitHub
  7. 面试精讲之面试考点及大厂真题 - 分布式专栏 11 Redis热点key大Value解决方案
  8. pythonread读取怎么是乱码_python中如何读写文件不乱码
  9. 计算机表格乘法表,教你用Excel制作乘法表,方法奉上
  10. python--关于if __name__==__main__的解释
  11. Sublime Text4添加配色主题
  12. mysql查询加伪列_SQL Server数据库中伪列及伪列的含义详解
  13. Toxophily(hdu2298三分+二分)
  14. 触发器的创建及相关知识
  15. 计算机仿真技术交通灯设计,交通灯的设计心得体会总结
  16. AVC与HEVC的差异-帧内预测
  17. 国外计算机论文翻译,计算机论文外文翻译
  18. carla--使用python进行carla交互仿真
  19. 家庭宽带服务器有什么作用,服务器用的宽带和家用宽带有什么区别?
  20. ubuntu设置网卡默认启动_Ubuntu18.04 配置网卡上网

热门文章

  1. 了解CV和RoboMaster视觉组(四)视觉组使用的硬件
  2. visio中将图片改为平行四边形_visio中怎么画平行四边形并图色呢?
  3. floodfill算法
  4. 滚动条分页/滚动条事件触发
  5. 用python给女朋友画素描_用Python来给你的女朋友画一幅素描画!女朋友我就不发了!...
  6. 【字符统计】统计输入的文章中文字、数字、空格和特殊字符的个数并打印的简单python程序
  7. 主板的各个部分及其功能
  8. 51单片机测量脉冲频率C程序及protues仿真结果精确
  9. 基于STM32设计的老人防摔倒报警设备(OneNet)
  10. OFFICE 2016 安装和使用心得