文本处理课后小练习

1、找出ifconfig命令结果中本机的所有IPv4地址
[root@wzc date]# ifconfig |grep -E -o  '(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'
10.1.253.22
255.255.0.0
10.1.255.255
127.0.0.1
255.0.0.0
192.168.122.1
255.255.255.0
192.168.122.255
2、查出分区空间使用率的最大百分比值
[root@wzc date]# df | tr -s ' '|cut -d' ' -f5|sort -n|tail -1|tr -d %
51
3、查出用户UID最大值的用户名、 UID及shell类型
[root@wzc date]# getent passwd |cut -d: -f1,3,7|sort -rnt: -k 2|head -n1
nfsnobody:65534:/sbin/nologin
4、查出/tmp的权限,以数字方式显示
[root@wzc date]# stat /tmp/|head -n4|tail -n1|cut -d/ -f1|cut -d\( -f2
1777
5、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序
[root@wzc date]# netstat -tn |cut -d: -f2|tr -s ' ' ':' |cut -d: -f2| sort |uniq -c|head -n1
      1 10.1.250.38
6、显示/proc/meminfo文件中以大小s开头的行;(要求:使用两种方式)
[root@wzc date]# grep -E '^(S|s)' /proc/meminfo ;grep '^[sS]' /proc/meminfo
SwapCached:          100 kB
SwapTotal:       3905532 kB
SwapFree:        3905116 kB
Shmem:             10160 kB
Slab:             140636 kB
SReclaimable:      79632 kB
SUnreclaim:        61004 kB
SwapCached:          100 kB
SwapTotal:       3905532 kB
SwapFree:        3905116 kB
Shmem:             10160 kB
Slab:             140636 kB
SReclaimable:      79632 kB
SUnreclaim:        61004 kB
7、显示/etc/passwd文件中不以/bin/bash结尾的行
[root@wzc date]# grep -v '/bin/bash$' /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8、显示用户rpc默认的shell程序
[root@wzc date]# getent passwd |grep '^rpc\b'|cut -d: -f7
/sbin/nologin
9、找出/etc/passwd中的两位或三位数
[root@wzc date]# grep '\<[[:digit:]]\{2,3\}\>' /etc/passwd
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
10、显示/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面存非空白字符的行
[root@wzc date]# grep "^[[:space:]]\+[^[:space:]].*" /etc/grub2.cfg 
  load_env
   set default="${next_entry}"
   set next_entry=
   save_env next_entry
   set boot_once=true
   set default="${saved_entry}"
  menuentry_id_option="--id"
  menuentry_id_option=""
  set saved_entry="${prev_saved_entry}"
11、找出"netstat -tan"命令的结果中以'LISTEN'后跟0、 1或多个空白字符结尾的行
[root@wzc date]# netstat -tan |grep "\bLISTEN[[:space:]]*$"
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:631                 :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN
12、添加用户bash、 testbash、 basher以及nologin(其shell为/sbin/nologin),而后找出/etc/passwd文件中用户名同shell名的行
[root@wzc date]# useradd bash
[root@wzc date]# useradd testbash
[root@wzc date]# useradd basher
[root@wzc date]# useradd nologin -s /sbin/nologin 
[root@wzc date]#  getent passwd |grep "^\(\b.*\b\).*\<\1\>$"
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
bash:x:1017:1026::/home/bash:/bin/bash
nologin:x:1020:1029::/home/nologin:/sbin/nologin
13、显示当前系统root、 mage或wang用户的UID和默认shell
[root@wzc date]# getent passwd|grep -e "^root\b" -e "^mage\b" -e "^wang\b"|cut -d: -f3,7
0:/bin/bash
1009:/bin/bash
1010:/bin/bash
14、找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行
[root@wzc date]# grep -E "^([[:alpha:]_])+\(\).*" /etc/rc.d/init.d/functions 
checkpid() {
__pids_var_run() {
__pids_pidof() {
daemon() {
killproc() {
pidfileofproc() {
pidofproc() {
status() {
echo_success() {
echo_failure() {
echo_passed() {
echo_warning() {
update_boot_stage() {
15、使用egrep取出/etc/rc.d/init.d/functions中其基名
[root@wzc date]# echo /etc/rc.d/init.d/functions |egrep -o "[^/]+$"
functions
16、使用egrep取出上面路径的目录名
[root@wzc date]# echo "/etc/rc.d/init.d/functions" | egrep -o "^.*/"
/etc/rc.d/init.d/
17、统计以root身份登录的每个远程主机IP地址的登录次数
[root@wzc date]# last |grep root|tr -s " "|cut -d" " -f3|grep -v ":"|uniq -c|tail -n1
     23 10.1.250.38
18、利用扩展正则表达式分别表示0-9、 10-99、 100-199、200-249、 250-255
[root@localhost ~]# 0-9:[0-9]; 10-99:[1-9][0-9]; 100-199:[1-9][0-9]{2}; 200-249:2[0-4][0-9]; 250-255:25[0-5]
19、用正则表达式表示手机号(11 13 17 15  18 )
[root@wzc date]# cat xiaomi 
11568973427
13546789524
15789342569
17789828269
18217968392
[root@wzc date]# egrep -o "1[13578][0-9]{9}" xiaomi
11568973427
13546789524
15789342569
17789828269
18217968392
22、使用cut把本机ip地址提取出来
  centos7版本
[root@wzc date]# ifconfig |head -n2 |tail -n1|cut -d. -f1-4|cut -dt -f2|cut -dn -f1
 10.1.253.22
  centos6版本
[root@localhost ~]# ifconfig |head -n 2|tail -n 1|cut -d: -f2|cut -dB -f1
10.1.253.24

原创文章,作者:RecallWzc,如若转载,请注明出处:http://www.178linux.com/31083

(0)
RecallWzcRecallWzc
上一篇 2016-08-08 16:05
下一篇 2016-08-08 16:05

相关推荐

  • 文件相关命令

    1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。 cp:copy 源文件:目标文件 单源复制:cp [OPTION]… [-T] SOURCE DEST 如果DEST不存在,则事先创建此文件,并复制源文件的数据流至DEST中 如果DEST存在: 如果DEST是非目录文件:则覆盖目标文件 如果DEST是目录文件:则先在D…

    Linux干货 2016-09-23
  • N25第六周作业

    vim 是vi编辑的升级版,同样有三种工作模式:编辑、命令和一般 模式 #:指定行号位置 #,#:指定多少行到多少行 #,+#:指定范围,多少行开始,加#行 $:最后一行 .:当前行 .,$-1,当前行到倒数第二行 1,$:全文 %:全文 /从上往下,?:从下往上 搜索 /pattern/:从光标所在处起始向文件尾部第一次被模式所匹配到的行 s/要查找的内容…

    Linux干货 2017-01-11
  • varnish学习总结

    什么是web cache?   Web缓存是指一个Web资源(如html页面,图片,js,数据等)存在与Web服务器和客户端(浏览器)直接的副本。缓存会根据进来的请求保存输出内容的副本;当下一个请求到来的时候,如果是相同的URL,缓存会根据缓存机制决定是直接使用副本响应访问请求还是向源服务器再次发送请求。       …

    Linux干货 2015-07-15
  • grep是个什么鬼

    正则表达式   一个正则表达式是一个描述了一个字符串集合的模式。正则表达式的构造类似于算术表达式,使用各种各样的操作符来将更小的表达式连在一起。 Grep能理解两种不同版本的正则表达式语法:“basic”和“extended”。在GNU grep中,两种语法可以实现的功能是没有区别的。在其他实现中,基本(basic)正则表达式表达能力要弱一点。下面…

    Linux干货 2017-04-17
  • LVM(逻辑卷)(logical volume manager),快照卷

    逻辑卷的创建,扩展,缩减,迁移,删除。快照卷的创建

    Linux干货 2017-12-11