总结基本正则表达式及扩展正则表达式

基本正则表达式的元字符:

字符匹配

.: 匹配任意单个字符;

[]:匹配指定范围内的任意单个字符;

[^]:匹配指定范围外的任意单个字符;

[:lower:], [:upper:], ...

次数匹配:用于要指定其次数的字符的后面;

*: 任意次;

\?: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:不支持正则表达式


扩展正则表达式的元字符

字符匹配:

.: 任意单个字符

[]:

[^]:

次数匹配:

*

?: 0次或1次;

+: 1次以上;

{m}: 精确匹配m次;

{m,n}: 至少m次,至多n次;

锚定:

^: 锚定行首

$: 锚定行尾

\<:词首锚定

\>:词尾锚定


分组:()

后向引用:\1, \2, ...

或者:

a|b 

C|cat: 不表示Cat或cat,而表示C或cat;

要写成(C|c)at


练习:1、显示/etc/passwd文件中以bash结尾的行。

       cat /etc/passwd |grep 'bash$'

      2、显示/etc/passwd文件中的两位数或三位数。

       cat /etc/passwd |grep '[[:digit:]]\{2,3}\'

      3、显示`netstat -tan`命令结果中以‘LISTEN’后跟0个、1个或者多个空白字符结尾的行。

       cat /etc/passwd |grep 'LISTEN[[:space:]]\{0,\}$'

      4、添加用户bash、testbash、basher以及nologin用户(nologin用户的shell为/sbin/nologin);而后找出/etc/passwd文件中用户名与其shell名相同的行。

       useradd bash

       useradd testbash

       useradd basher

       useradd nologin -s /bin/nologin

[root@bogon ~]# grep '\(bash\).*\1' /etc/passwd
bash:x:500:506::/home/bash:/bin/bash
testbash:x:501:507::/home/testbash:/bin/bash
basher:x:502:508::/home/basher:/bin/bash
 
[root@bogon ~]# grep '^\(nologin\).*\1' /etc/passwd
nologin:x:503:509::/home/nologin:/sbin/nologin

  (注:这一部分没搞明白,是抄别人的)

       5、显示当前系统上root、centos或者user1用户的默认shell和UID (请事先创建这些用户,若不存在)

         useradd centos

[root@bogon ~] # grep -E '^root|^centos|^user1' /etc/passwd | cut -d: -f1,3,7
root:0: /bin/bash
centos:504: /bin/bash
user1:505: /bin/bash

 (注:这一部分也没搞明白,是抄别人的)

       6、找出/etc/rc.d/init.d/functions文件中某单词(单词中间可以存在下划线)后面跟着一组小括号的行。

       grep -n  '^[[:alpha:]].*()' /etc/rc.d/init.d/functions 

执行结果如下:

25:systemctl_redirect () {

97:checkpid() {

143:daemon() {

233:killproc() {

325:pidfileofproc() {

340:pidofproc() {

366:status() {

426:echo_success() {

437:echo_failure() {

448:echo_passed() {

459:echo_warning() {

471:update_boot_stage() {

479:success() {

485:failure() {

493:passed() {

500:warning() {

507:action() {

520:strstr() {

526:is_ignored_file() {

536:is_true() {

546:is_false() {

556:apply_sysctl() {

     7、使用echo输出一个路径,而后egrep找出其路径基名;进一步的使用egrep取出其目录名。

  echo "/etc/sysconfig/network"|egrep -o '\<network\>'

network
echo   "/etc/sysconfig/network" | egrep   -o  '/etc/sysconfig/'
/etc/sysconfig/

(注:此部分没搞明白,抄的)

      8.找出ifconfig命令执行结果中1-255之间的数字。

      ifconfig |grep -E '\<([1-9]|[1-9][0-9]|[1][0-9][0-9]|2[0-4][0-9]|25[0-5])\>'

执行结果:

inet 192.168.80.134  netmask 255.255.255.0  broadcast 192.168.80.255

        inet6 fe80::20c:29ff:fe36:92b4  prefixlen 64  scopeid 0x20<link>

        ether 00:0c:29:36:92:b4  txqueuelen 1000  (Ethernet)

        TX packets 2814  bytes 479932 (468.6 KiB)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536

        inet 127.0.0.1  netmask 255.0.0.0

        inet6 ::1  prefixlen 128  scopeid 0x10<host>

        RX packets 48  bytes 4176 (4.0 KiB)

        TX packets 48  bytes 4176 (4.0 KiB)



spacer.gif