概要:分別的做用html
grep:文本過濾(模式:pattern)工具,grep,egrep,fgrep,擅長過濾。mysql
sed:stream editor 文本編輯工具;(流編輯器),擅長取行、替換。linux
awk:linux上的實現gawk,文件報告生成器;(獨立的編程語言),過濾內容,擅長取列。ios
find:linux上實時查找工具,經過便利指定路徑下得文件系統完成文件查找。nginx
文本過濾(模式:pattern)工具,grep,egrep,fgrepgit
文本搜索工具,根據用戶指定的模式對目標文本逐行進行匹配檢查,打印匹配到的行;web
一、模式正則表達式
由正則表達式字符及文本字所編寫的過濾條件redis
grep [OPTIONS] PATTERN[FILE]sql
--color=auto:對匹配到的我那本着色顯示;
-v:顯示不可以陪pattern匹配到的行;
-i:忽略字符大小寫;
-o:僅顯示匹配到的字符串;
-q:靜默模式,不輸出任何信息;
-A#:after,匹配到的後#行,就是顯示匹配還顯示該行以前得num行
-B#:before,前#行,還顯示該行以後得num行
-C#context,先後各#行 ,還顯示該行先後各num行
-E:使用ERE;擴展正則表達式,egrep=grep -E
-F:至關於fgrep
基於正則表達式元字符:
1)、字符匹配:
.: 匹配任意單個字符;
[]: 匹配指定範圍內的任意單個字符
[^]:匹配指定範圍外的任意單個字符
專用字符集合
[:digit:]:任意數字,至關於0-9
[:lower:]:全部的小寫字符
[:upper:]:大寫字符
[:alpha:]:大小寫
[:alnum:]:數字和字符
[:punct:]:標點符號
[:space:]:空格
[:graph:] 可打印的非空白字符
grep 's..n' /etc/passwd
grep '...t' /etc/passwd
grep'[[:alpha:]][[:alpha:]][[:alpha:]]t' /etc/passwd這樣寫太難受了,能夠用匹配次數
2)、匹配次數:用在要指定次數的字符後面,用於指定前面的字符要出現的次數;
*:匹配前面的字符任意次;僅表示次數
nano grep.txt
xxxxxxy
grep "x*y" grep.txt,這就是貪婪模式,若是隻有x是不能夠的,x能夠沒有也能夠出現多行
貪婪模式
.*:任意長度的任意字符;
\?:匹配其前面的字符0或1次;即前面的無關緊要;
\+:匹配其前面的字符至少1次;
\{m\}:匹配前面的字符m次;
\{m,n\}:匹配前面的字符至少m次,至多n次;
\{0,n\}:匹配前面的字符至多n次;
\{m,\}:匹配前面的字符至少m次;
3)、位置錨定:
^:行首錨定;用於模式的最左側;
$:行尾錨定;用於模式的最右側;
^PATTERN$: 用於模式匹配整行;
^$: 空行;
^[[:space:]]*$這個空白是能夠沒有的應爲有*號
\< 或 \b:詞首錨定;用於單詞模式的左側;單詞的最左側
\> 或 \b:詞尾錨定;用於單詞模式的右側;
\<PATTERN\>:匹配整個單詞;
分組:
\(\):將一個或多個字符捆綁在一塊兒,看成一個總體進行處理
\(xy\)*ab
nano grep.txt
xyxyxyxy
grep "\(xy\)+" 若是沒有小括號,則表明x後面至少一個y
Note: 分組括號中的模式匹配到的內容會被正則表達式引擎記錄於內部的變量中,這些變量的命名方式爲: \1, \2, \3, ...
\1: 從左側起,第一個左括號以及與之匹配右括號之間的模式所匹配到的字符;
\(ab\+\(xy\)*\):
\1: ab\+\(xy\)*這個\是和前面的有括號匹配
\2: xy
當咱們想後續內容與前面模式匹配到的語言,則須要使用引用,即後向引用
後向引用:引用前面的分組括號中的模式所匹配字符,(而非模式自己)
grep '[[:alpha:]]'\{1,3\}t\>' /etc/passwd
grep '[[:alpha:]]'\{1,3\}t\>'.*'[[:alpha:]]'\{1,3\}t /etc/passwd 觀察,有先後不一致的
grep '[[:alpha:]]'\{1,3\}t\>'.*\1' /etc/passwd 如今觀察匹配的相同的必定是同樣的
擴展正則表達式:
基於正則表達式而言擴展正則表達式在某些參數上能夠不使用轉義符(\),在使用方式上沒有區別
1)、字符匹配
.
[]
[^]
2)、次數匹配
*
?:0或1次
+:1次或屢次
{m}:匹配m次
{m,n}:至少m至多n次
3)、錨定
^
$
\<,\b
\>,\b
分組
()
後向引用:\1,\2...
或者 a|b
實例一、顯示/proc/meminfo文件中以大小s開頭得行
1 [root@web2 keepalived]# grep -i "^[Ss]" /proc/meminfo 2 SwapCached: 0 kB 3 SwapTotal: 3933180 kB 4 SwapFree: 3933180 kB 5 Shmem: 92032 kB 6 Slab: 131744 kB 7 SReclaimable: 86092 kB 8 SUnreclaim: 45652 kB 9 [root@web2 keepalived]# grep -i "^s" /proc/meminfo 10 SwapCached: 0 kB 11 SwapTotal: 3933180 kB 12 SwapFree: 3933180 kB 13 Shmem: 92032 kB 14 Slab: 131752 kB 15 SReclaimable: 86092 kB 16 SUnreclaim: 45660 kB 17 [root@web2 keepalived]# grep "^[Ss]" /proc/meminfo 18 SwapCached: 0 kB 19 SwapTotal: 3933180 kB 20 SwapFree: 3933180 kB 21 Shmem: 92032 kB 22 Slab: 131752 kB 23 SReclaimable: 86092 kB 24 SUnreclaim: 45660 kB 25 [root@web2 keepalived]# grep "^s\|^S" /proc/meminfo 26 SwapCached: 0 kB 27 SwapTotal: 3933180 kB 28 SwapFree: 3933180 kB 29 Shmem: 92032 kB 30 Slab: 131752 kB 31 SReclaimable: 86092 kB 32 SUnreclaim: 45660 kB 33 [root@web2 keepalived]#
實例二、顯示/etc/passwd文件中不以/bin/bash結尾行
1 [root@web2 keepalived]# grep -v "/bin/bash"$ /etc/passwd 2 bin:x:1:1:bin:/bin:/sbin/nologin 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin 4 adm:x:3:4:adm:/var/adm:/sbin/nologin 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin 6 sync:x:5:0:sync:/sbin:/bin/sync 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 8 halt:x:7:0:halt:/sbin:/sbin/halt 9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 10 operator:x:11:0:operator:/root:/sbin/nologin 11 games:x:12:100:games:/usr/games:/sbin/nologin 12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin 13 nobody:x:99:99:Nobody:/:/sbin/nologin 14 systemd-bus-proxy:x:999:998:systemd Bus Proxy:/:/sbin/nologin 15 systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin 16 dbus:x:81:81:System message bus:/:/sbin/nologin 17 polkitd:x:998:997:User for polkitd:/:/sbin/nologin 18 abrt:x:173:173::/etc/abrt:/sbin/nologin 19 unbound:x:997:995:Unbound DNS resolver:/etc/unbound:/sbin/nologin 20 tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin 21 libstoragemgmt:x:996:994:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin 22 rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin 23 colord:x:995:993:User for colord:/var/lib/colord:/sbin/nologin 24 usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin 25 saslauth:x:994:76:Saslauthd user:/run/saslauthd:/sbin/nologin 26 geoclue:x:993:991:User for geoclue:/var/lib/geoclue:/sbin/nologin 27 rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin 28 rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin 29 nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin 30 radvd:x:75:75:radvd user:/:/sbin/nologin 31 qemu:x:107:107:qemu user:/:/sbin/nologin 32 ntp:x:38:38::/etc/ntp:/sbin/nologin 33 chrony:x:992:989::/var/lib/chrony:/sbin/nologin 34 setroubleshoot:x:991:988::/var/lib/setroubleshoot:/sbin/nologin 35 sssd:x:990:987:User for sssd:/:/sbin/nologin 36 pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin 37 gdm:x:42:42::/var/lib/gdm:/sbin/nologin 38 gnome-initial-setup:x:989:984::/run/gnome-initial-setup/:/sbin/nologin 39 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 40 avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin 41 postfix:x:89:89::/var/spool/postfix:/sbin/nologin 42 tcpdump:x:72:72::/:/sbin/nologin 43 nginx:x:988:983:Nginx web server:/var/lib/nginx:/sbin/nologin 44 mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin 45 [root@web2 keepalived]#
實例三、顯示/etc/passwd文件中ID號最大得用戶的用戶名
1 [root@web1 keepalived]# sort -t: -k3 -n /etc/passwd |tail -1 |cut -d: -f1 2 nfsnobody 3 [root@web1 keepalived]#
實例四、若是用戶root存在,顯示其默認的shell程序
1 [root@web1 keepalived]# id root &> /dev/null && grep "^root\>" /etc/passwd | cut -d: -f7 2 /bin/bash 3 [root@web1 keepalived]#
實例五、找出/etc/passwd中的兩位或三位數
1 [root@web1 keepalived]# grep "\<[0-9]\{2,3\}\>" /etc/passwd 2 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin 3 operator:x:11:0:operator:/root:/sbin/nologin 4 games:x:12:100:games:/usr/games:/sbin/nologin 5 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin 6 nobody:x:99:99:Nobody:/:/sbin/nologin 7 systemd-bus-proxy:x:999:998:systemd Bus Proxy:/:/sbin/nologin 8 systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin 9 dbus:x:81:81:System message bus:/:/sbin/nologin 10 polkitd:x:998:997:User for polkitd:/:/sbin/nologin 11 abrt:x:173:173::/etc/abrt:/sbin/nologin 12 unbound:x:997:995:Unbound DNS resolver:/etc/unbound:/sbin/nologin 13 tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin 14 libstoragemgmt:x:996:994:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin 15 rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin 16 colord:x:995:993:User for colord:/var/lib/colord:/sbin/nologin 17 usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin 18 saslauth:x:994:76:Saslauthd user:/run/saslauthd:/sbin/nologin 19 geoclue:x:993:991:User for geoclue:/var/lib/geoclue:/sbin/nologin 20 rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin 21 rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin 22 radvd:x:75:75:radvd user:/:/sbin/nologin 23 qemu:x:107:107:qemu user:/:/sbin/nologin 24 ntp:x:38:38::/etc/ntp:/sbin/nologin 25 chrony:x:992:989::/var/lib/chrony:/sbin/nologin 26 setroubleshoot:x:991:988::/var/lib/setroubleshoot:/sbin/nologin 27 sssd:x:990:987:User for sssd:/:/sbin/nologin 28 pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin 29 gdm:x:42:42::/var/lib/gdm:/sbin/nologin 30 gnome-initial-setup:x:989:984::/run/gnome-initial-setup/:/sbin/nologin 31 sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin 32 avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin 33 postfix:x:89:89::/var/spool/postfix:/sbin/nologin 34 tcpdump:x:72:72::/:/sbin/nologin 35 zhangxingeng:x:1000:1000:centos-7-64:/home/zhangxingeng:/bin/bash 36 nginx:x:988:983:Nginx web server:/var/lib/nginx:/sbin/nologin 37 lightdm:x:987:982::/var/lib/lightdm:/sbin/nologin 38 apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin 39 redis:x:986:981:Redis Database Server:/var/lib/redis:/sbin/nologin 40 mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin 41 [root@web1 keepalived]#
實例六、顯示/etc/rc.d/rc.sysinit文件中,至少一個空白字符開頭的且後面存非空白字符的行
1 [root@web1 keepalived]# grep "^[[:space:]]\+[[:graph:]]" /etc/grub2.cfg 2 load_env 3 set default="${next_entry}" 4 set next_entry= 5 save_env next_entry 6 set boot_once=true 7 set default="${saved_entry}" 8 menuentry_id_option="--id" 9 menuentry_id_option="" 10 set saved_entry="${prev_saved_entry}" 11 save_env saved_entry 12 set prev_saved_entry= 13 save_env prev_saved_entry 14 set boot_once=true 15 if [ -z "${boot_once}" ]; then 16 saved_entry="${chosen}" 17 save_env saved_entry 18 fi 19 if [ x$feature_all_video_module = xy ]; then 20 insmod all_video 21 else 22 insmod efi_gop 23 insmod efi_uga 24 insmod ieee1275_fb 25 insmod vbe 26 insmod vga 27 insmod video_bochs 28 insmod video_cirrus 29 fi 30 set timeout_style=menu 31 set timeout=5 32 set timeout=5 33 source ${prefix}/user.cfg 34 if [ -n "${GRUB2_PASSWORD}" ]; then 35 set superusers="root" 36 export superusers 37 password_pbkdf2 root ${GRUB2_PASSWORD} 38 fi 39 load_video 40 set gfxpayload=keep 41 insmod gzio 42 insmod part_msdos 43 insmod xfs 44 set root='hd0,msdos1' 45 if [ x$feature_platform_search_hint = xy ]; then 46 search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1' a9392b0d-ae01-41ce-b4a0-efb843235bfe 47 else 48 search --no-floppy --fs-uuid --set=root a9392b0d-ae01-41ce-b4a0-efb843235bfe 49 fi 50 linux16 /vmlinuz-3.10.0-514.el7.x86_64 root=UUID=11b66499-c82b-476f-8576-fc3690a4e26d ro crashkernel=auto rhgb quiet LANG=en_US.UTF-8 51 initrd16 /initramfs-3.10.0-514.el7.x86_64.img 52 load_video 53 insmod gzio 54 insmod part_msdos 55 insmod xfs 56 set root='hd0,msdos1' 57 if [ x$feature_platform_search_hint = xy ]; then 58 search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1' a9392b0d-ae01-41ce-b4a0-efb843235bfe 59 else 60 search --no-floppy --fs-uuid --set=root a9392b0d-ae01-41ce-b4a0-efb843235bfe 61 fi 62 linux16 /vmlinuz-0-rescue-b7671beb75f14e95b5a58975dae3a897 root=UUID=11b66499-c82b-476f-8576-fc3690a4e26d ro crashkernel=auto rhgb quiet 63 initrd16 /initramfs-0-rescue-b7671beb75f14e95b5a58975dae3a897.img 64 source ${config_directory}/custom.cfg 65 source $prefix/custom.cfg; 66 [root@web1 keepalived]#
實例七、找出「netstat -tan」命令的結果以LISTEN後跟0、1或多個空白字符結尾
1 [root@web1 keepalived]# netstat -tan | grep "LISTEN[[:space:]]*$" 2 tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 3 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4 tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 5 tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 6 tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 7 tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 8 tcp6 0 0 :::111 :::* LISTEN 9 tcp6 0 0 :::80 :::* LISTEN 10 tcp6 0 0 :::22 :::* LISTEN 11 tcp6 0 0 ::1:631 :::* LISTEN 12 tcp6 0 0 ::1:25 :::* LISTEN 13 [root@web1 keepalived]#
實例八、添加用戶bash、testbash、basher以及nologin(其shell爲/sbin/nologin);然後找出/etc/passwd文件中用戶名同shell名的行;
1 [root@web1 keepalived]# cat /etc/passwd | grep "\(^[[:alnum:]]\+\>\).*\<\1$" 2 [root@web1 keepalived]# useradd bash 3 [root@web1 keepalived]# useradd testbash 4 [root@web1 keepalived]# useradd basher 5 [root@web1 keepalived]# useradd -s /sbin/nologin nologin 6 [root@web1 keepalived]# grep "^\([[:alnum:]]\+\>\).*\1$" /etc/passwd 7 sync:x:5:0:sync:/sbin:/bin/sync 8 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown 9 halt:x:7:0:halt:/sbin:/sbin/halt 10 bash:x:1001:1001::/home/bash:/bin/bash 11 nologin:x:1004:1004::/home/nologin:/sbin/nologin 12 [root@web1 keepalived]#
#謀定全部得數字和字符,至少出現一個因此用+,謀定整個單詞詞尾,而後()括號作分組,.*中間跟上任意內容,\1$結尾跟括號內一致
egrep實例
實例九、顯示當前系統root、centos或user1用戶的默認shell和UID;
1 [root@web1 keepalived]# grep -E '^(root|centos|user1)\>' /etc/passwd | cut -d: -f 1,3,7 2 root:0:/bin/bash 3 [root@web1 keepalived]#
實例十、找出/etc/rc.d/init.d/functions文件中某單詞後跟一個小括號的行
1 [root@web1 keepalived]# grep -E -o "^[_[:alpha:]]+\(\)" /etc/rc.d/init.d/functions 2 checkpid() 3 __kill_pids_term_kill_checkpids() 4 __kill_pids_term_kill() 5 __pids_var_run() 6 __pids_pidof() 7 daemon() 8 killproc() 9 pidfileofproc() 10 pidofproc() 11 status() 12 echo_success() 13 echo_failure() 14 echo_passed() 15 echo_warning() 16 update_boot_stage() 17 success() 18 failure() 19 passed() 20 warning() 21 action() 22 strstr() 23 is_ignored_file() 24 is_true() 25 is_false() 26 apply_sysctl() 27 [root@web1 keepalived]#
#_這個是連字符,+至少出現一次\(\)顯示()自己
實例十一、使用echo輸出一絕對路徑,使用egrep取出其基名
1 [root@web1 keepalived]# echo "/mnt/sdc" | grep -E -o "[^/]+/?$" | cut -d"/" -f1 2 sdc 3 [root@web1 keepalived]#
實例十二、找出ifconfig命令結果中1-255之間的數值,這裏分別表是0-九、10-9九、100-19九、200-24九、250-255
1 echo {1..1000} | egrep -o "\b[0-9]\b" | tr "\n" " " ;echo 2 echo {1..1000} | egrep -o "\b[0-9]{2}\b" | tr "\n" " " ;echo 3 echo {1..1000} | egrep -o "\b1[0-9]{2}\b" | tr "\n" " " ;echo 4 echo {1..1000} | egrep -o "\b2[0-4][0-9]\b" | tr "\n" " " ;echo 5 echo {1..1000} | egrep -o "\b25[0-5]\b" | tr "\n" " " ;echo
實例1三、找出ifconfig命令結果中的IP地址
1 [root@web1 keepalived]# ifconfig|egrep -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])\>" 2 255.255.255.0 3 127.0.0.1 4 255.0.0.0 5 192.168.122.1 6 255.255.255.0 7 192.168.122.255 8 [root@web1 keepalived]#