歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> Linux之正則表達式

Linux之正則表達式

日期:2017/3/1 12:25:58   编辑:關於Linux
grep: linux文本處理三劍客: grep:文本過濾工具 sed:文本編輯器(行);stream editor awk:文本報告編輯器;Linux上awk的實現為gawk; grep:Global search REgularexpression and Print out the line. 作用:文本搜索工具,根據用戶的“模式(pattern)”逐行去搜索目標文本,打印匹配到的行; 模式:由正則表達式的元字符及文本字符所編寫的過濾條件 元字符:字符不表示其字面意義,而用於表示通配或控制功能。 分兩類: 基本正則表達式: BRE 擴展正則表達式引擎:ERE 正則表達式引擎: grep [OPTIONS] PATTERN[FILE...]: 選項: --color=auto:對匹配到的串做高亮顯示; -v:顯示模式匹配不到行; -i: 忽略字符大小寫; -o: 僅顯示能夠被模式匹配到的串本行; -q: 靜默模式; -E:使用擴展的正則表達式; 基本正則表達式的元字符: 字符匹配: .: 匹配任意單個字符; []:匹配指定范圍內的任意單個字符; [^]:匹配指定范圍內的任意單個字符; [:lower:], [:upper:], ... 次數匹配:用於要指定其次數的字符的後面; *: 任意次; abxy xay xxxxxxxy grep "x*y" \?:0或1次; grep "x\?y" \+:1或多次; \{m\}:精確限制為m次; \{m,n\}: 至少m次,至多n次,[m,n] \{0,n\}:至多n次; \{m,\}:至少m次; .*: 匹配任意長度的任意字符; 位置錨定: ^: 行首錨定;用於模式的最左側; $: 行尾錨定;用於模式的最右側; \<, \b: 詞首錨定;用於表示單詞的模式的左側; \>, \b:詞尾錨定;用於表示單詞的模式的右側; ^$: 空白行; 分組:\(\) 分組的小括號中的模式匹配到的內容,會在執行過程中被正則表達式引擎記錄下來,並保存內置的變量中;這些變量分別是\1, \2, ... \1: 從左側起,第一個左括號,以及與之配對的右括號中間的模式所匹配到的內容; \2: ... 後向引用:使用變量引用前面的分組括號中的模式所匹配到的字符; 擴展的正則表達式: grep家庭中有三個命令: grep:基本正則表達式 -E:擴展正則表達式 -F:不支持正則表達式 egrep:擴展正則表達式 fgrep:不支持正則表達式 1、顯示/etc/passwd文件中以bash結尾的行 [root@localhost ~]# grep --color=auto ‘bash$‘ /etc/passwd root:x:0:0:root:/root:/bin/bash wengangwang:x:1000:1000:wengangwang:/home/wengangwang:/bin/bash 2、顯示/etc/passwd文件中的兩位數或三位數 [root@localhost ~]# grep --color=auto "[[:digit:]]\{2,3\}" /etc/passwd mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin polkitd:x:999:999:User for polkitd:/:/sbin/nologin 3、顯示`netstat -tan`命令結果中以‘LISTEN’後跟0個、1個或者多個空白字符結尾的行 [root@localhost ~]# netstat -tan | egrep ‘LISTEN[[:space:]]*$‘ 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 4、添加用戶bash、testbash、basher以及nologin用戶(nologin用戶的shell為/sbin/nologin);而後找出/etc/passwd文件中用戶名與其shell名相同的行 [root@localhost ~]# useraddbash;useradd testbash;useradd busher;useradd -s /sbin/nologin nologin [root@localhost ~]# grep"\(bash\).*\1" /etc/passwd bash:x:3002:3002::/home/bash:/bin/bash testbash:x:3003:3003::/home/testbash:/bin/bash 5、顯示當前系統上root、centos或者user1用戶的默認shell和UID (請事先創建這些用戶,若不存在) [root@localhost ~]# egrep‘^root|^centos|^user1‘ /etc/passwd |cut -d: -f3,7 0:/bin/bash 3006:/bin/bash 3007:/bin/bash [root@localhost ~]# egrep--color=auto ‘(^\<root\>|^\<centos\>|^\<user1\>)‘ /etc/passwd root:x:0:0:root:/root:/bin/bash centos:x:3006:3006::/home/centos:/bin/bash user1:x:3007:3007::/home/user1:/bin/bash 6、找出/etc/rc.d/init.d/functions文件中某單詞(單詞中間可以存在下劃線)後面跟著一組小括號的行 [root@localhost ~]# grep‘.*()‘ /etc/rc.d/init.d/functions fstab_decode_str() { checkpid() { __readlink() { __fgrep() { __umount_loop() { __umount_loopback_loop() { __pids_var_run() { __pids_pidof() { daemon() { killproc() { pidfileofproc() { pidofproc() { status() { echo_success() { echo_failure() { echo_passed() { echo_warning() { update_boot_stage() { success() { failure() { passed() { warning() { action() { action_silent() { strstr() { confirm() { get_numeric_dev() { is_ignored_file() { is_true() { is_false() { apply_sysctl() { key_is_random() { find_crypto_mount_point() { init_crypto() { 7、使用echo輸出一個路徑,而後egrep找出其路徑基名;進一步的使用egrep取出其目錄名 [root@localhost ~]# echo/etc/passwd |egrep --color=auto -o "[[:alnum:]]+\/?$" passwd [root@localhost ~]# echo/etc/passwd/ | egrep --color=auto -o "^\/.*\/" /etc/passwd/ 8、找出ifconfig命令執行結果中1-255之間的數字 [root@localhost ~]# ifconfig| egrep ‘\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]25[0-5])\>‘ inet 192.168.88.106 netmask255.255.255.0 broadcast 192.168.88.255 inet6 fe80::20c:29ff:fe7b:347c prefixlen 64 scopeid 0x20<link> ether 00:0c:29:7b:34:7c txqueuelen 1000 (Ethernet) RX packets 144034 bytes202904157 (193.5 MiB) TX packets 64975 bytes 4455917(4.2 MiB) lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> RX packets 428 bytes 37366(36.4 KiB) TX packets 428 bytes 37366(36.4 KiB)
Copyright © Linux教程網 All Rights Reserved