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)

相关内容