文本过滤、文本查找工具应用示例
-
1.显示当前系统上root、fedora或user1用户的默认shell
[root@localhost ~]# cat /etc/passwd|grep "^root\>\|^fedora\>\|^user1\>"|cut -d: -f7 /bin/bash /bin/bash /bin/bash
-
2.找出/etc/rc.d/init.d/functions文件中某个单词后面跟一组小括号的行,形如:hello()
[root@localhost ~]# cat /etc/rc.d/init.d/functions|grep -o "[[:alpha:]]\+\>()" checkpid() checkpids() kill() run() pidof() daemon() killproc() pidfileofproc() pidofproc() status() success() failure() passed() warning() stage() success() failure() passed() warning() action() strstr() file() true() false() sysctl()
-
3.使用echo命令输出一个绝对路径,使用grep取出其基名;扩展,取出其路径名
[root@localhost ~]# echo "/etc/rc.d/init.d/functions"|grep -o "[^\/]\+\/\?$" functions [root@localhost ~]# echo "/etc/rc.d/init.d/functions"|grep -o "^\/[^[:space:]]\+\/" /etc/rc.d/init.d/
-
4.找出ifconfig命令结果中1-255之间的数字
[root@localhost ~]# ifconfig|grep -E -o "([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" 33 41 63 150 192 168 91 128 255 255 255 192 168 91 255 6 80
-
5.挑战题:写一个模式,能匹配合理的IP地址
[root@localhost ~]# ifconfig|grep -E -o "([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])" 192.168.91.128 192.168.91.255 192.168.122.1 192.168.122.255
-
6.挑战题:写一个模式,能匹配所有的邮件地址
[root@localhost ~]# echo "1234567nn@163.com"|grep -o "[[:alnum:]]\+@[[:alnum:]]\+\.[[:alpha:]]\+" 1234567nn@163.com
-
7.查找/var目录下属主为root,且属组为mail的所有文件或目录
[root@localhost ~]# find /var -user root -a -group mail -ls 67292968 0 drwxrwxr-x 2 root mail 129 Jul 28 18:28 /var/spool/mail
-
8.查找当前系统上没有属主或属组的文件;进一步查找当前系统上没有属主或属组,且最近3天内曾经被访问过的文件或目录
[root@localhost ~]# find / -nouser -a -nogroup -ls [root@localhost ~]# find / -nouser -a -nogroup -a -atime -3 -ls
-
9.查找/etc目录下所有用户都有写权限的文件
[root@localhost ~]# find /etc -perm -222 -ls 33554500 0 lrwxrwxrwx 1 root root 17 Jun 30 01:53 /etc/mtab -> /proc/self/mounts 35553953 0 lrwxrwxrwx 1 root root 56 Jun 30 02:10 /etc/fonts/conf.d/65-0-lohit-marathi.conf -> /usr/share/fontconfig/conf.avail/65-0-lohit-marathi.conf 33722185 0 lrwxrwxrwx 1 root root 56 Jun 30 01:57 /etc/fonts/conf.d/59-liberation-mono.conf -> /usr/share/fontconfig/conf.avail/59-liberation-mono.conf 35553954 0 lrwxrwxrwx 1 root root 59 Jun 30 02:10 /etc/fonts/conf.d/65-0-lohit-devanagari.conf -> /usr/share/fontconfig/conf.avail/65-0-lohit-devanagari.conf 33722192 0 lrwxrwxrwx 1 root root 56 Jun 30 01:57 /etc/fonts/conf.d/59-liberation-sans.conf -> /usr/share/fontconfig/conf.avail/59-liberation-sans.conf 35553957 0 lrwxrwxrwx 1 root root 54 Jun 30 02:11 /etc/fonts/conf.d/66-ucs-miscfixed.conf -> /usr/share/fontconfig/conf.avail/66-ucs-miscfixed.conf 33735545 0 lrwxrwxrwx 1 root root 59 Jun 30 01:57 /etc/fonts/conf.d/10-scale-bitmap-fonts.conf -> /usr/share/fontconfig/conf.avail/10-scale-bitmap-fonts.conf 35553958 0 lrwxrwxrwx 1 root root 50 Jun 30 02:11 /etc/fonts/conf.d/60-open-sans.conf -> /usr/share/fontconfig/conf.avail/60-open-sans.conf
-
10.查找/etc目录下大于1M,且类型为普通文件的所有文件
[root@localhost ~]# find /etc -size +1M -a -type f -ls 102108754 1364 -rw-r--r-- 1 root root 1394978 Jul 9 05:37 /etc/selinux/targeted/contexts/files/file_contexts.bin 68163210 3620 -rw-r--r-- 1 root root 3703887 Jul 9 05:37 /etc/selinux/targeted/policy/policy.30 68163206 3620 -rw-r--r-- 1 root root 3703887 Jul 9 05:37 /etc/selinux/targeted/active/policy.kern 68112876 7120 -r--r--r-- 1 root root 7289802 Jul 9 05:33 /etc/udev/hwdb.bin 34410868 1336 -rw-r--r-- 1 root root 1367395 Nov 5 2016 /etc/brltty/zh-tw.ctb
原创文章,作者:N27_xiaoni,如若转载,请注明出处:http://www.178linux.com/83022
评论列表(1条)
文本管理工具是非常重要的一项技能,从作业情况来看,掌握的还不错,继续努力。