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

这一生的挚爱 提交于 2019-12-03 22:43:09

统计/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 -c
     21 a
      5 A
      1 aA
      1 abnormally
      5 action
      1 active
      1 ActiveState
      1 adjust
      1 alive
      2 all
      1 already
      2 and
      1 And
      3 any
      1 anywhere
      1 Apply
      1 are
      1 arg
      2 at
      1 avoid
      7 awk
      8 b
      1 backup
      2 bak
     29 base
      2 basename
      2 bash
      7 be
      6 BEGIN
      1 bg
     13 bin
     11 binary
      1 bit
      2 booleans
      1 boot
     21 BOOTUP
      2 break
      3 but
      1 by
      6 c
      1 caller
      3 can
      8 case
      1 cat
      1 centi
      2 cgexec
      6 cgroup
      1 Cgroups
      4 check
      2 Check
      2 checkpid
      1 cmdline
      1 code
     13 color
      2 COLUMNS
      3 command
      1 condrestart
      5 conf
      2 configuration
      1 confusing
      1 consoletype
      4 CONSOLETYPE
      2 contains
      9 continue
      1 Convert
      1 core
      4 corelimit
      1 could
      1 current
     27 d
      5 daemon
      1 daemons
      3 dead
      1 debug
      1 deci
      1 declared
      1 default
     11 delay
      1 deleted
      1 dependencies
      1 details
     17 dev
      1 dies
      1 directory
      1 dirname
     10 do
      1 doc
      1 doesn
     10 done
      2 due
      1 dump
      6 e
     49 echo
      1 Echo
      3 eE
      1 elif
      9 else
      5 en
      1 END
      5 eq
      1 errors
      8 esac
     16 etc
      2 Evaluate
      1 exe
      3 exists
      4 exit
      1 exited
      1 export
      1 expression
      1 eyes
     12 f
      1 fail
      1 failed
      1 FAILED
      7 failure
      4 fF
     51 fi
     23 file
      3 files
      1 filter
      2 find
      1 Find
      2 First
      2 foo
     12 for
      5 force
      2 found
      1 from
      1 fsck
      4 function
      2 functions
      1 further
      1 g
      1 G
      1 generated
      1 Get
      4 gotbase
      1 graphical
      3 grep
      1 h
      2 had
      1 have
      2 honor
      5 i
     60 if
      1 ignore
      1 IGNORECASE
      1 ignored
     25 in
      1 including
      1 Inform
      6 init
      1 initialize
      2 initscripts
      1 installed
      2 insufficient
      1 into
     11 is
      6 it
      1 its
      1 keyword
      4 kill
      1 Kill
      1 KILL
      8 killlevel
      4 killproc
      1 ksh
      1 l
      1 lang
      1 let
      1 lets
      2 level
      3 lib
      7 line
      1 little
      1 lL
      1 LoadState
     37 local
      1 locale
      2 lock
      1 locked
      5 Log
      1 LOGLEVEL
      2 Look
      1 looks
      1 Looks
     10 LSB
      2 lt
      3 m
      1 make
      1 Make
      1 matching
      1 may
      1 micro
      1 mili
      1 most
     38 n
      1 nano
      5 ne
      7 nice
      4 nicelevel
      3 NICELEVEL
      3 nN
      1 NOLOCALE
      6 not
     14 null
     13 o
      7 of
      2 OK
      2 old
      1 on
      5 only
      3 oO
      2 option
      3 options
      3 or
      2 orig
      1 otherwise
      2 our
      1 out
      1 output
      1 Output
     24 p
      2 passed
      1 PASSED
      1 path
      2 PATH
     46 pid
     11 pidfile
      2 pidfileofproc
      4 pidof
      2 pidofproc
      3 pids
      1 PIDs
      1 piko
      1 plural
      4 plymouth
      5 PPID
      7 printf
      1 privilege
      1 privileges
      7 proc
      1 processes
      1 profile
      8 prog
     15 program
      5 Program
      1 propagating
      3 q
      8 r
     11 rc
     19 RC
      2 read
      1 Read
      1 readlink
      1 reality
      1 recognizes
      4 reload
      2 Reloading
      1 RemainAfterExit
      4 remaining
      1 Remove
      1 requested
      3 restart
      1 Restarting
      3 ret
     54 return
      1 returns
      1 Returns
      8 retval
      1 rm
      1 rpm
      2 rpmnew
      2 rpmorig
      2 rpmsave
      1 rR
      1 RS
      8 run
      1 Run
      8 running
      1 runuser
     12 s
      1 S
      1 same
      2 sane
      2 Save
      3 sbin
      1 screen
      1 script
      1 scripts
      1 search
      1 second
      2 seconds
      2 sed
      2 See
      1 seem
      1 self
      3 serial
      6 service
      3 set
      2 Set
      1 settings
      1 sh
      1 share
      1 shell
      1 Shell
     18 shift
      2 should
      4 show
      4 shutdown
      2 shvar
      2 signal
      3 sleep
      1 smhd
      1 some
      3 something
      1 sourcing
      1 specified
      2 sS
      3 start
      1 Starting
      2 startup
      9 stat
      1 state
      6 status
      3 stderr
      4 stime
      2 stop
      1 stopped
      1 Stopping
      5 STRING
      2 strstr
      2 style
      1 SubState
      3 subsys
      1 succeeded
      5 success
      2 sure
      5 syntax
      7 sysconfig
     15 sysctl
      1 system
     11 systemctl
      8 systemd
      2 t
      1 TERM
      3 test
      5 Test
      1 TEXTDOMAIN
      5 that
     13 the
     52 then
      2 they
      1 this
      2 This
      1 time
     10 to
      9 try
      2 tT
      1 txt
      1 ulimit
      2 umask
      1 unit
      1 unknown
      1 unless
      1 unset
      2 up
      1 update
      9 Usage
      1 use
      3 used
      1 Useful
      9 user
      1 using
      5 usr
      1 uU
      2 v
      1 value
      7 var
      6 verbose
      4 via
      3 warning
      1 WARNING
      1 We
      1 where
      1 whether
      3 while
      1 width
      1 will
      2 with
     14 x
      1 xcondrestart
      1 xforce
      1 xreload
      1 xrestart
      1 xstart
      1 xstop
      1 xtry
      3 yes
      2 yY
     28 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 -c
    956 
     21 a
      5 A
      1 aA
      1 abnormally
      5 action
      1 active
      1 ActiveState
      1 adjust
      1 alive
      2 all
      1 already
      2 and
      1 And
      3 any
      1 anywhere
      1 apply
      1 Apply
      1 are
      3 arg
      2 at
      1 avoid
      7 awk
      8 b
      1 backup
      2 bak
     38 base
      2 basename
      2 bash
      7 be
      6 BEGIN
      1 bg
     13 bin
     11 binary
      1 bit
      2 booleans
      2 boot
     21 BOOTUP
      2 break
      3 but
      1 by
      6 c
      1 caller
      3 can
      8 case
      1 cat
      1 centi
      2 cgexec
      6 cgroup
      3 CGROUP
      1 Cgroups
      4 check
      2 Check
      2 checkpid
      5 checkpids
      1 cmdline
      2 code
      8 COL
     13 color
      2 COLUMNS
      3 command
      1 condrestart
      5 conf
      2 configuration
      1 confusing
      1 consoletype
      4 CONSOLETYPE
      2 contains
      9 continue
      1 convert
      1 Convert
      1 core
      1 COREFILE
      4 corelimit
      1 could
      1 current
     27 d
      5 daemon
      4 DAEMON
      1 daemons
      3 dead
      1 debug
      1 deci
      1 declared
      1 default
     11 delay
      1 deleted
      1 dependencies
      1 DEPENDENCIES
      1 details
     17 dev
      1 dies
      3 dir
      1 directory
      1 dirname
      1 discard
     10 do
      1 doc
      1 doesn
     10 done
      2 due
      1 dump
      6 e
     57 echo
      1 Echo
      3 eE
      1 elif
      9 else
      5 en
      1 END
      5 eq
      1 errors
      8 esac
     16 etc
      2 Evaluate
      1 exe
      3 exists
      4 exit
      1 exited
      1 export
      1 expression
      1 eyes
     18 f
      2 fail
      1 failed
      1 FAILED
      9 failure
      3 FAILURE
      1 false
      4 fF
     46 fi
     58 file
      4 files
      1 filter
      2 find
      1 Find
      2 First
      2 foo
     12 for
      5 force
      2 found
      1 from
      1 fsck
      4 function
      2 functions
      1 further
      1 g
      1 G
      1 generated
      1 Get
      4 gotbase
      1 graphical
      3 grep
      1 h
      2 had
      1 have
      2 honor
      6 i
     60 if
      1 ignore
      1 IGNORE
      1 IGNORECASE
      7 ignored
     25 in
      1 including
      1 Inform
      6 init
      1 initialize
      2 initscripts
      1 installed
      2 insufficient
      1 into
     18 is
      6 it
      1 its
      1 keyword
     34 kill
      1 Kill
      1 KILL
      8 killlevel
      4 killproc
      1 ksh
      1 l
      1 lang
      3 LANGSH
      1 let
      1 lets
      2 level
      3 lib
      1 LIMIT
      7 line
     16 list
      1 little
      1 lL
      1 LoadState
     37 local
      1 locale
      8 lock
      1 locked
      5 Log
      1 LOGLEVEL
      2 Look
      1 looks
      1 Looks
     10 LSB
      2 lt
      7 m
      1 make
      1 Make
      1 matching
      1 may
      1 micro
      1 mili
      1 most
      6 MOVE
     39 n
      1 nano
      5 ne
      7 nice
      4 nicelevel
      3 NICELEVEL
      3 nN
      1 NOLOCALE
      6 NORMAL
      6 not
     14 null
     13 o
      7 of
      2 OK
      2 old
      1 on
      5 only
      3 oO
      2 option
      3 options
      3 or
      2 orig
      1 otherwise
      2 our
      1 out
      1 output
      1 Output
     24 p
      4 passed
      1 PASSED
      1 path
      2 PATH
     73 pid
     11 pidfile
      2 pidfileofproc
      8 pidof
      2 pidofproc
     21 pids
      1 PIDs
      1 piko
      1 plural
      4 plymouth
      5 PPID
      7 printf
      1 privilege
      1 privileges
      8 proc
      1 processes
      1 profile
      8 prog
     15 program
      5 Program
      1 propagating
      3 q
      8 r
     11 rc
     19 RC
      2 read
      1 Read
      1 readlink
      1 reality
      1 recognizes
      2 redirect
      1 REDIRECT
      4 reload
      2 Reloading
      1 RemainAfterExit
      4 remaining
      1 Remove
      1 requested
      2 RES
      3 restart
      1 Restarting
      3 ret
     54 return
      1 returns
      1 Returns
      8 retval
      1 rm
      1 rpm
      2 rpmnew
      2 rpmorig
      2 rpmsave
      1 rR
      1 RS
     14 run
      1 Run
      8 running
      1 runuser
     12 s
      1 S
      1 same
      2 sane
      2 Save
      3 sbin
      1 screen
      1 script
      1 scripts
      1 search
      1 sec
      1 second
      2 seconds
      3 sed
      2 See
      1 seem
      1 self
      3 serial
      6 service
      3 set
      2 Set
     16 SETCOLOR
      1 settings
      1 sh
      1 share
      1 shell
      1 Shell
     18 shift
      2 should
      4 show
      4 shutdown
      2 shvar
      2 signal
      1 SKIP
      3 sleep
      1 smhd
      1 some
      3 something
      3 SOURCED
      1 sourcing
      1 specified
      2 sS
      1 stage
      3 start
      1 Starting
      2 startup
      9 stat
      1 state
      6 status
      3 stderr
     13 stime
      2 stop
      1 stopped
      1 Stopping
      5 STRING
      2 strstr
      2 style
      1 SubState
      3 subsys
      1 succeeded
      7 success
      3 SUCCESS
      2 sure
      5 syntax
      7 sysconfig
     16 sysctl
      1 system
     16 systemctl
      2 SYSTEMCTL
      8 systemd
      2 t
      7 term
      1 TERM
      3 test
      5 Test
      1 TEXTDOMAIN
      5 that
     13 the
     52 then
      2 they
      1 this
      2 This
      1 time
     10 to
      6 TO
      1 true
      9 try
      2 tT
      1 txt
      1 ulimit
      2 umask
      1 unit
      1 unknown
      1 unless
      1 unset
      2 up
      2 update
      9 Usage
      4 use
      3 used
      1 Useful
      9 user
      1 using
      5 usr
      1 uU
      2 v
      1 value
     13 var
      6 verbose
      4 via
      5 warning
      5 WARNING
      1 We
      1 where
      1 whether
      3 while
      1 width
      1 will
      2 with
     14 x
      1 xcondrestart
      1 xforce
      1 xreload
      1 xrestart
      1 xstart
      1 xstop
      1 xtry
      3 yes
      2 yY
     28 z

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

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

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

 

 

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!