文本過濾、文本查找工具應用示例
-
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.www58058.com/83022
文本管理工具是非常重要的一項技能,從作業情況來看,掌握的還不錯,繼續努力。