第5天(半天)【shell编程初步、grep及正则表达式】

2022-10-24,,,,

第5天(半天)【shell编程初步、grep正则表达式

shell编程初步(01)_recv

shell脚本:文本文件

#!:/bin/bash

#!:/usr/bin/python

#!:/usr/bin/perl

  案列1:

Vim first.sh

#!:/bin/bash

Cat /etc/fstab

Wc –l /etc/fstab

给脚本权限              Chmod a+x first.sh

运行脚本                   ./first.sh

------------------------------------------------------------------

逻辑运算:

True

False

与:

1 && 1 =1

1 && 0=0

0 && 1 =0

0 && 0 =0

  或:

   1 || 1= 1

     1 || 0= 1

     0 || 1= 1

    0 || 0 =0

  非:

    !1 = 0

    !0 = 1

grep及正则表达式(02)_recv

Linux上文本处理三剑客:

    Grep:文本过滤工具

    Sed:文本编辑工具

    Awk:

Grep:Global search Regular expression and print out the line

作用:文本搜索工具,根据用户指定的模式对目标文本逐行进行陪陪检查;

Grep:

-v:反转查找,既输出与查找条件不相符的行

Grep root /etc/passwd

-E:扩展正则表达式;

-G:基本正则表达式;

-F:固定字符串

-i:忽略字符大小写;

-o:仅显示匹配到的字串;

Grep –o root /etc/passwd

-q:静默模式,不输入任何信息

Grep –q root /etc/passwd

-c:统计一共有多少行;

-n:在左侧显示行号;

-A number:后#行

-B number:前#行

-C number:前后#行

Grep –c ‘nologin’ passwork

Allisa grep=‘grep –color=auto’

Grep root /etc/passwd

基本正则表达式元字符:

字符匹配:

.:匹配任何单个字符

Grep  ‘r.ot’ /etc/passwd

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

Grep [root] /etc/passwd

       [^]:匹配指定范围外的任意单个字符
           Grep [^root] /etc/passwd
       [0-9]: 统计指定范围内的数字
           Grep [0-9] /etc/passwd00 
             [:digit:]、[:lower:]、[:upper:]、[:alpha:]、[:alnum:]、[:punct:]、[:space:]
          Grep [0-9] /etc/passwd

匹配次数:用在要指定次数的字符后面,用于指定前面的字符要出现的次数;

*:匹配前面的字符任意次;

例如:grep “x*y”

.*:任意长度的任意字符;

位置锚定:

^:行首锚定,用于模式的最左侧

$:行尾锚定,

^$:空行;

第5天(半天)【shell编程初步、grep及正则表达式】的相关教程结束。

《第5天(半天)【shell编程初步、grep及正则表达式】.doc》

下载本文的Word格式文档,以方便收藏与打印。