文本处理工具作业

1、找出ifconfig命令结果中本机的所有IPv4地址

root@cenots6.8  ~ #  ifconfig | tr -cs '[0-9]\.' '\n' |sort -u -t'.' -k3n

127.0.0.1
192.168.1.103
192.168.1.255
255.255.255.0

2、查出分区空间使用率的最大百分比值

root@cenots6.8  ~ # df | tr -s ' '|cut -d' ' -f5|sort -n|tail -1|tr -d %
19

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

root@cenots6.8  ~ # cat /etc/passwd|cut -d: -f1,3,7|sort -n -t: -k2 |tail -1
nfsnobody:65534:/sbin/nologin

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

root@cenots6.8  ~ # stat /tmp|head -4|tail -1 | tr -cs [0-9] '\n'|head -2|tail -1
1777

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

root@cenots6.8  ~ # netstat -nt|tail -3|tr -s " "|cut -d" " -f5|cut -d: -f1|sort -n|uniq -c
      1 111.108.54.43
      2 192.168.1.102

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

root@cenots6.8  ~ # grep '^[sS]' /proc/meminfo;grep -i '^s' /proc/meminfo
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:              3300 kB
Slab:             112308 kB
SReclaimable:      42212 kB
SUnreclaim:        70096 kB
SwapCached:            0 kB
SwapTotal:       2097148 kB
SwapFree:        2097148 kB
Shmem:              3300 kB
Slab:             112308 kB
SReclaimable:      42212 kB
SUnreclaim:        70096 kB

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

root@cenots6.8  ~ # 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

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

root@cenots6.8  ~ # grep 'rpc\>' /etc/passwd |cut -d: -f7
/sbin/nologin

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

root@cenots6.8  ~ #  grep -o  "\<[0-9]\{2,3\}\>" /etc/passwd

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

root@cenots6.8  ~ # grep "^[[:space:]]\+[^[:space:]]\+" /etc/grub.conf
    root (hd0,0)
    kernel /vmlinuz-2.6.32-642.el6.x86_64 ro root=UUID=8ecfb3ed-37d8-43cd-a1ec-8a4be6fa5973 nomodeset rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet
    initrd /initramfs-2.6.32-642.el6.x86_64.img

6、找出"netstat -tan"命令的结果中以'LISTEN'后跟0、 1或多个空白字符结尾的行

[root@localhost ~]# netstat -tan | grep "LISTEN[[: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

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

[root@localhost ~]# grep "^\(\<[[:alnum:]]\+\)\>.*\1$" /etc/passwd
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:3011:3016::/home/bash:/bin/bash
nologin:x:3014:3019::/home/nologin:/sbin/nologin

1、显示当前系统root、 mage或wang用户的UID和默认shell

root@cenots6.8  ~ # egrep "^(root|mage|wang)\>" /etc/passwd |cut -d: -f3,7
0:/bin/bash
503:/bin/bash
504:/bin/bash

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

root@cenots6.8  ~ # egrep "^[[:alpha:]]+\(\)|^\_+\(\)" /etc/rc.d/init.d/functions
checkpid() {
daemon() {
killproc() {
pidfileofproc() {
pidofproc() {
status() {
success() {
failure() {
passed() {
warning() {
action() {
strstr() {
confirm() {

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

root@cenots6.8  ~ # echo "/etc/rc.d/init.d/functions" | egrep -o  "\<[[:alpha:]]+$"
functions

4、使用egrep取出上面路径的目录名

root@cenots6.8  ~ # echo "/etc/rc.d/init.d/functions" | egrep -o "^/.*/"
/etc/rc.d/init.d/

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

root@cenots6.8  ~ #  last | egrep -o "^root\>.*[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"|tr -s " "|cut -d" " -f3|sort|uniq -c
      4 10.1.250.89
      3 192.168.1.102
      5 192.168.1.103
      1 192.168.1.104

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

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]

7、显示ifconfig命令结果中所有IPv4地址

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])'
192.168.1.103
192.168.1.255
255.255.255.0
127.0.0.1
255.0.0.0

1、取本机ip地址

root@cenots6.8  ~ # ifconfig|egrep -o  "inet addr:[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" |cut -d: -f2|head -1
192.168.1.103

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

root@cenots6.8  ~ # df | tr -s " "|cut -d" " -f5
Use%
4%
1%
19%

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

cat /etc/init.d/functions | tr -cs "[:alpha:]" "\n"|sort|uniq -c|sort -n

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

root@cenots6.8  ~ # echo "/etc/rc.d/init.d/functions" | egrep -o "^/.*/"
/etc/rc.d/init.d/

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

root@cenots6.8  /testdir # egrep -io "[0-9]{17,18}x?" card
421081199402080612
350722198609015732
440202198004250317
420321198709043810

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

root@cenots6.8  /testdir # egrep -o "1[34578][0-9]{9}" iphone
13901212122
13718063333
13611111919
13021218888
13051688888
13552709999

7、正则表达式表示邮箱

root@cenots6.8  /testdir # egrep "^[^@]+@.*(com|cn)$" mail 
530180782@qq.com
243678025@qq.com
398018489@qq.com
595064131@qq.com
362483245@qq.com

8、正则表达式表示QQ号

root@cenots6.8  /testdir # egrep "[1-9][0-9]{4,10}" mail -o

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

(0)
NarutoNaruto
上一篇 2016-08-07
下一篇 2016-08-07

相关推荐

  • 基于lvs调度的web应用——Discuz程序

    实验环境: 前端主机:10.1.43.101 后端主机1:172.16.0.9   作为lvs-dr的调度器,并且提供mysql和nfs文件共享 后端主机2:172.16.0.2   作为ap服务器之一 后端主机3:172.16.0.3   作为ap服务器之一 实验拓扑: 后端主机1: [root@node3…

    Linux干货 2016-10-26
  • 4th work

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其他用户均没有任何访问权限。 [root@localhost etc]# cp -r /etc/skel/ /home/tuser1 [root@localhost etc]# chmod go-rwx /home/tuser1/ 2、编辑/etc/gr…

    Linux干货 2017-10-09
  • 用shell脚本打等腰三角新

       *   ***  ***** *******   预览图形 发现图形由三个变量组成 行数n 每行的空格数a 和每行的符号数b 关系是    a=n-当前行数    b=2xn-1   并且空格都在符号前 根据关系编写脚本 #!/bin/bash read…

    Linux干货 2016-08-22
  • 缓存需知

    Edit Web缓存核心技术点需知 5.1 HTTP首部控制 5.2 基于新鲜度检测机制: 2.1 特征1:时间局部性 2.2 特征2:空间局部性 2.3 缓存的优点 2.4 哪类数据应该被缓存 2.5 哪类数据可缓存但不应该被缓存 2.6 缓存命中率决定缓存有效性 2.7 缓存数据生命周期 2.8 缓存处理步骤 2.9 缓存和普通数据读取的区别 1. 完整…

    Linux干货 2015-02-22
  • lamp的搭建

    方法一编译安装amp:   1.系统环境:CentOS 6,7       CentOS6:apr,apr-util的版本为1.3.9,不适合httpd-2.4    CentOS7:apr,apr-util的版本为1.4+2.开发环境需要安装:    Developm…

    Linux干货 2016-10-16
  • CentOS 6启动过程总结与GRUB问题修复

    一、CentOS 6 的启动流程 第一步:加电自检(POST) 主要检查硬件设备是否存在并能正常运行,如:CPU、内存、硬盘、风扇、输入输出设备等。自检功能主要是通过BIOS来实现的,BIOS程序是装载在一个硬件芯片CMOS上的,加电过程就是给CMOS通电,然后启动BIOS程序,BIOS程序会根据CMOS上面的一些配置信息区读取其他的硬件设备信息并检测其是否…

    Linux干货 2016-09-16