08-27做業

Linux 文本處理三大工具:正則表達式

        grep: 文本過濾工具;shell

                   sed:文本編輯器()                   apache

awk:文本報告生成器;centos

gerpbash

使用格式:grep [OPTIONS] PATTERN [FILE...]app

選項:dom

--color=auto:對匹配到的串作高亮顯示;(CentOS7默認帶有,CentOS6須要手動填寫)ssh

                            -v:顯示模式匹配不到行;tcp

                            -i:忽略字符大小寫;編輯器

                            -o:僅顯示可以被模式匹配到的串本行;

                            -q:靜默模式;

                            -E:使用擴展的正則表達式;

元字符:

字符匹配:

                                     .:匹配任意單個字符;

                                     []:匹配指定範圍內的任意單個字符;

                                     [^]:匹配指定範圍外的任意單個字符;

                      次數匹配:用於要指定其次數的字符的後面;

                                     *:任意次;

                                              

                                     \?01次;

                                     \+1或屢次;

                                     \{m\}:精確限制爲m次;

                                     \{m,n\}:至少m次,至多n次,[m,n]

                                               \{0,n\}:至多n次;

                                               \{m,\}:至少m次;

                                     .*:匹配任意長度的任意字符;

                            位置錨定:

                                     ^:行首錨定;用於模式的最左側;

                                     $:行尾錨定;用於模式的最右側;

                                     \<,\b: 詞首錨定;用於表示單詞的模式的左側;

                                     \>,\b:詞尾錨定;用於表示單詞的模式的右側;

                                     ^$:空白行;

 

                            分組:\(\)

 

                                     分組的小括號中的模式匹配到的內容,會在執行過程當中被正則表達式引擎記錄下來,並保存內置的變量中;這些變量分別是\1, \2, ...

Egrep

 grep 用法基本相同,區別在於次數匹配時不須要再轉義!

練習題

顯示/etc/passwd文件中以bash結尾的行

[root@localhost ~]# grep bash$ /etc/passwd

root:x:0:0:root:/root:/bin/bash

lishuai:x:500:500:davachi:/home/lishuai:/bin/bash

[root@localhost ~]#

顯示/etc/passwd文件中的兩位數或三位數

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

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

gopher:x:13:30:gopher:/var/gopher:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

nobody:x:99:99:Nobody:/:/sbin/nologin

dbus:x:81:81:System messagebus:/:/sbin/nologin

usbmuxd:x:113:113:usbmuxduser:/:/sbin/nologin

vcsa:x:69:69:virtual console memoryowner:/dev:/sbin/nologin

rpc:x:32:32:RpcbindDaemon:/var/cache/rpcbind:/sbin/nologin

rtkit:x:499:497:RealtimeKit:/proc:/sbin/nologin

avahi-autoipd:x:170:170:Avahi IPv4LLStack:/var/lib/avahi-autoipd:/sbin/nologin

abrt:x:173:173::/etc/abrt:/sbin/nologin

rpcuser:x:29:29:RPC ServiceUser:/var/lib/nfs:/sbin/nologin

haldaemon:x:68:68:HALdaemon:/:/sbin/nologin

gdm:x:42:42::/var/lib/gdm:/sbin/nologin

ntp:x:38:38::/etc/ntp:/sbin/nologin

apache:x:48:48:Apache:/var/www:/sbin/nologin

saslauth:x:498:76:Saslauthduser:/var/empty/saslauth:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

pulse:x:497:496:PulseAudio SystemDaemon:/var/run/pulse:/sbin/nologin

sshd:x:74:74:Privilege-separatedSSH:/var/empty/sshd:/sbin/nologin

tcpdump:x:72:72::/:/sbin/nologin

lishuai:x:500:500:davachi:/home/lishuai:/bin/bash

顯示`netstat -tan`命令結果中以‘LISTEN’後跟0個、1個或者多個空白字符結尾的行

[root@localhost ~]# netstat -tan | grep"\<LISTEN\> [[:space:]]\{0,\}$"

tcp       0      0 0.0.0.0:111                 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     

tcp       0      0 0.0.0.0:44060               0.0.0.0:*                   LISTEN     

tcp       0      0 :::49155                    :::*                        LISTEN     

tcp       0      0 :::111                      :::*                        LISTEN     

tcp       0      0 :::22                       :::*                        LISTEN     

tcp       0      0 ::1:631                     :::*                        LISTEN     

tcp       0      0 ::1:25                      :::*                        LISTEN     

添加用戶bashtestbashbasher以及nologin用戶(nologin用戶的shell/sbin/nologin);

然後找出/etc/passwd文件中用戶名與其shell名相同的行

[root@localhost ~]# useradd bash

[root@localhost ~]# useradd textbash

[root@localhost ~]# useradd basher

[root@localhost ~]# useradd -s/sbin/nologin nologin

[root@localhost ~]# egrep    '(\b[a-z]+[a-z]\b).*\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:501:3002::/home/bash:/bin/bash

nologin:x:504:504::/home/nologin:/sbin/nologin

[root@localhost ~]#

顯示當前系統上rootcentos或者user1用戶的默認shellUID (請事先建立這些用戶,若不存在)

[root@localhost ~]# egrep'^\<root|centos|user1\>' /etc/passwd

root:x:0:0:root:/root:/bin/bash

centos:x:505:505::/home/centos:/bin/bash

user1:x:506:506::/home/user1:/bin/bash

找出/etc/rc.d/init.d/functions文件中某單詞(單詞中間能夠存在下劃線)後面跟着一組小括號的行

[root@localhost ~]# egrep '\<[[:alpha:]_]+\(\)'/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() {

[root@localhost ~]#

使用echo輸出一個路徑,然後egrep找出其路徑基名;進一步的使用egrep取出其目錄名

路徑基名

[root@localhost 12]# echo /1/12/123 | egrep -o '([^/]*)$'

123

[root@localhost 12]#

找出ifconfig命令執行結果中1-255之間的數字

[root@localhost ~]# ifconfig | egrep -n'\b[1-9]\b|\b[1-9][0-9]\b|\b[1-2][0-9][0-5]\b|\b[1][0-9][0-9]\b'

1:eth0      Linkencap:Ethernet  HWaddr00:0C:29:F9:D4:CE 

2:          inetaddr:192.168.200.128 Bcast:192.168.200.255 Mask:255.255.255.0

3:          inet6 addr:fe80::20c:29ff:fef9:d4ce/64 Scope:Link

4:          UP BROADCASTRUNNING MULTICAST  MTU:1500  Metric:1

8:          RXbytes:23884192 (22.7 MiB)  TXbytes:1152869 (1.0 MiB)

11:          inetaddr:127.0.0.1  Mask:255.0.0.0

12:          inet6 addr:::1/128 Scope:Host

13:          UP LOOPBACKRUNNING  MTU:65536  Metric:1

14:          RXpackets:224 errors:0 dropped:0 overruns:0 frame:0

15:          TXpackets:224 errors:0 dropped:0 overruns:0 carrier:0

17:          RXbytes:17354 (16.9 KiB)  TX bytes:17354(16.9 KiB)

相關文章
相關標籤/搜索