8.5-文本处理工具(作业篇)

 1、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序

[root@localhost ~]# netstat -nt
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State      
tcp        0      0 10.1.252.66:22              10.1.252.7:61217            ESTABLISHED 
tcp        0      0 10.1.252.66:22              10.1.252.7:61133            ESTABLISHED 
[root@localhost ~]# netstat -nt | tail -n +3 | tr -s ' ' | cut -d' ' -f4 | sort | uniq -c | sort -nr
      2 10.1.252.66:22

2、查出/tmp的权限,以数字方式显示

[root@localhost ~]# stat /tmp/ | tail -n +4 | head -1 | cut -d'/' -f1 | cut -d'(' -f2 
1777
[root@localhost ~]# stat -c %a /tmp
1777

3、查出用户UID最大值的用户名、 UID及shell类型

[root@localhost ~]# sort -t':' -k3 -n /etc/passwd | tail -1 | cut -d':' -f1,3,7
nfsnobody:65534:/sbin/nologin

4、取本机ip地址

[root@localhost ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:71:3A:3A  
          inet addr:10.1.252.66  Bcast:10.1.255.255  Mask:255.255.0.0
          inet6 addr: fe80::20c:29ff:fe71:3a3a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3567 errors:0 dropped:0 overruns:0 frame:0
          TX packets:683 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:326443 (318.7 KiB)  TX bytes:77640 (75.8 KiB)
[root@localhost ~]# ifconfig|head -2|tail -1|tr -s " "|cut -d" " -f3|cut -d: -f2
10.1.252.66

5、取各分区利用率的数值

[root@localhost ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda3       19276064 3716892  14573316  21% /
tmpfs             502068       0    502068   0% /dev/shm
/dev/sda1         194241   34091    149910  19% /boot
[root@localhost ~]# df | tr -s ' ' | cut -d" " -f5| tail -n +2
21%
0%
19%

6、统计/etc/init.d/functions 文件中每个单词出现的次数,并按频率从高到低显示

[root@localhost ~]# cat /etc/rc.d/init.d/functions | tr -c '^[:alpha:]' '\n' | sort | uniq -c | sort -n -r
   9294 
     83 if
     77 then
     75 pid
     73 echo
     72 fi
     61 return
     57 dev
     54 file
     50 n
     46 local
     42 kill
     39 z
     36 base

7、/etc/rc.d/init.d/functions或/etc/rc.d/init.d/functions/"  取目录名

[root@localhost ~]# echo "/etc/rc.d/init.d/functions/" |sed 's@[^/]\+/\?$@@'
/etc/rc.d/init.d/
[root@localhost ~]# echo "/etc/rc.d/init.d/functions"  |sed 
/etc/rc.d/init.d/

8、正则表达式表示身份证号

[root@localhost ~]# cat a.txt | grep -E --color "\<[0-9]{18}\>|\<[0-9]{15}\>"

001.png

9、正则表达式表示手机号

[root@localhost ~]# grep --color  -E "\<1[34578][0-9]{9}\>" a.txt

 

002.png

10、正则表达式表示邮箱

[root@localhost ~]# cat a.txt |grep -E "\<[[:alnum:]]+@[[:alnum:]]*\.[[:alpha:]]+\>"

009.png

11、正则表达式表示QQ号

cat a.txt |grep -E "[1-9][0-9]{4,11}"

003.png

12、显示/proc/meminfo文件中以大小s开头的行; (要求:使用两种方式)

[root@localhost ~]# grep -i "^s" /proc/meminfo
[root@localhost ~]# grep "^[sS]" /proc/meminfo
[root@localhost ~]# grep "^s\|^S" /proc/meminfo

003.png

13、显示/etc/passwd文件中不以/bin/bash结尾的行

[root@localhost ~]# 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
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
...

14、显示用户rpc默认的shell程序

[root@localhost ~]# grep "^rpc:" /etc/passwd | cut -d ":" -f7
/sbin/nologin
[root@localhost ~]# getent passwd rpc |cut -d: -f7
/sbin/nologin

15、找出/etc/passwd中的两位或三位数

[root@localhost ~]# grep -E "\<[1-9][0-9]?[0-9]\>" /etc/passwd

004.png

16、显示/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面存非空白字符的行

[root@localhost ~]# grep -E "^[[:space:]]+[^[:space:]]" /etc/grub2.cfg

005.png

17、找出“netstat -tan”命令的结果中以‘LISTEN’后跟任意多个空白字符结尾的行

[root@localhost ~]# netstat -tan | grep -E "LISTEN[[:space:]]+$"

007.png

18、添加用户bash、 testbash、 basher以及nologin(其shell为/sbin/nologin),而后找出/etc/passwd文件中用户名同shell名的行

[root@localhost ~]# useradd bash
[root@localhost ~]# useradd testbasher
[root@localhost ~]# useradd -s "/sbin/nologin" nologin

008.png

19、显示三个用户root、 mage、 wang的UID和默认shell

[root@localhost ~]# grep -E "^wang:|^mage:|^root:" /etc/passwd | cut -d: -f3,7
0:/bin/bash
1004:/bin/bash
1005:/bin/bash

20、找出/etc/rc.d/init.d/functions文件中行首为某单词(包括下划线)后面跟一个小括号的行

[root@localhost ~]# grep "^[[:alpha:]_]\+()" /etc/rc.d/init.d/functions
checkpid() {
__pids_var_run() {
__pids_pidof() {
daemon() {
killproc() {
pidfileofproc() {
pidofproc() {
status() {
echo_success() {
...

21、使用egrep取出/etc/rc.d/init.d/functions中其基名

[root@localhost ~]# echo "/etc/rc.d/init.d/functions" | egrep -o --color "[^/]+/?$" |cut -d"/" -f1
functions
[root@localhost ~]# echo "/etc/rc.d/init.d/functions/" | egrep -o --color "[^/]+/?$" |cut -d"/" -f1
functions

23、统计以root身份登录的每个远程主机IP地址的登录次数

[root@localhost ~]# last | grep -o -E "^root\>.*((25[0-4]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(25[0-4]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])"|tr -s ' '|cut -d" " -f3|sort|uniq -c
      4 192.168.33.1

24、利用扩展正则表达式分别表示0-9、 10-99、 100-199、200-249、 250-255

[0-9]   [1-9][0-9]    1[0-9][0-9]   2[0-4][0-9]   25[0-5]

 

原创文章,作者:M20-1--孔祥文,如若转载,请注明出处:http://www.178linux.com/29985

(0)
M20-1--孔祥文M20-1--孔祥文
上一篇 2016-08-07
下一篇 2016-08-07

相关推荐

  • 磁盘管理、文件系统管理

    磁盘管理、文件系统管理 磁盘管理 磁盘设备 机械硬盘(非固态) I/O Ports —– I/O 设备地址 块设备:Block 存取单位“块”;(磁盘) 字符设备:char 存取单位“字符”;(键盘) 设备文件:关联一个设备驱动程序,进而能够跟与之对应的硬件设备进行通信; – 设备号码: 主设备号 major number…

    Linux干货 2016-07-22
  • 配额-and-RAID

    配置配额系统 控制单个用户,或组在某磁盘上所能占的最大空间大小 配额是基于磁盘分区的,不基于文件 如何基于软件控制配额:          1、设置分区挂载选项          &nbsp…

    Linux干货 2016-08-29
  • RAID磁盘阵列

    什么是RAID?    RIAD(Redundant Array of Independent Disk,独立冗余磁盘阵列)技术是加州大学伯克利分校1987年提出,最初是为了组合小的廉价磁盘来代替大的昂贵磁盘,同时希望磁盘失效时不会使对数据的访问受损失而开发出一定水平的数据保护技术。RAID就是一种由多块廉价磁盘构成的冗余阵列,在操作系统下是作为一个独立的大…

    Linux干货 2016-12-13
  • class-6 积累应用

    1、三种权限rwx对文件和目录的不同意义     ## 读写执行对于文件的意义         r:针对于文本文件         w:可修改内容,但不能删除文件本身,需要x权限配合  &n…

    Linux干货 2016-08-05
  • N25期—第二周作业

    1、  Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。   一、文件权限管理类命令  chown:改变文件所有者  chown [OPTION]… [OWNER][:[GROUP]] FILE…  常用参数 -R:递归修改(改目录就改目录中的目录及文件) &n…

    Linux干货 2016-12-11
  • 马哥教育首届IT技术博客大赛–复审阶段

    马哥教育首届IT博客大赛大众评审开始了 2016年马哥教育首届IT博客技术大赛,评委初审已结束,正式进入大众评审阶段。。。。 此次博客大赛,虽然因为准备时间比较仓促,举办经验略有不足(快去鄙视小编~&#x1f612;),但还是收到了10多位优秀的小伙伴共80篇博文(看看人家的劲头,这就是差距呀&#x1f613;)的参赛作品投递; 其中初审最高…

    Linux干货 2016-06-07