zoukankan      html  css  js  c++  java
  • Linux正则表达式

    基本正则表达式:Basic REGEXP

    元字符

    描述

    .

    匹配任意单个字符

    *

    匹配其前面的字符任意次

    .*

    任意长度的任意字符

    []

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

    [^]

    匹配指定范围外的任意单个字符

    [:lower:]

    小写字母

    [:upper:]

    大写字母

    [:alpha:]

    所有字母

    [:digit:]

    数字

    [:alnum:]

    所有数字和字母

    [:punct:]

    标点符号

    [:space:]

    空白字符

    ?

    匹配其前面的字符1次或0次

    {m,n}

    匹配其前面的字符至少m次,至多n次

    ^

    铆定行首,此字符后面的任意内容必须出现在行首

    $

    铆定行尾,此字符前面的任意内容必须出现在行尾

    ^$

    表示空白行

    <或

    铆定词首,其后面的任意字符必须作为单词的首部出现

    >或

    铆定词尾,其前面的任意字符必须作为单词的尾部出现

    ()

    分组

    (ab)*

    ab作为一个整体,可以出现任意次

    (ab).*1

    引用第一个左括号以及与之对应的右括号所包括的所有内容

    (ab).*2

    引用第二个左括号以及与之对应的右括号所包括的所有内容

    扩展正则表达式:Extended REGEXP

    字符匹配

    .

    匹配任意单个字符

    []

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

    [^]

    匹配指定范围外的任意单个字符

    次数匹配

    *

    匹配其前字符任意次

    ?

    匹配其前字符0次或1次

    +

    匹配其前字符至少1次,类似于基本正则表达式{1,}

    {m,n}

    匹配其前面的字符至少m次,至多n次

    位置铆定

    ^

    行首

    $

    行尾

    <或

    词首

    >或

    词尾

    分组

    ().*123

    或者

    |

    or  a|b ,a或者b ,有一个就行

    C|cat--> C或cat

    (C|c)at-->Cat或cat

    单引号’’强引用,不做变量替换的

    双引号””弱引用,内部的变量会替换

    vim编辑器使用

    全文搜索空格,并在空格前面加换行符

    :%s@[[:space:]]@ &@g

     

     

    Vim 中换行符 和 分别的使用场景是怎样的?它们有什么区别?

    为什么vi的替换命令里 和 是混用的?

    %s/$/ /g

    %s/ //g

    只能被替换或删除 只能用来插入或替换

     

     

    另外linux二进制里的 为什么显示为"^@" 查了一下这个符号对应的应该是"\`"

     

    还有为什么我cat -v 和vim -b只能看到gbk编码的^@ 转为utf-8后就看不到了 有什么办法可以查看完全

     

     

    在Linux 中, 是行结束符,而 不是。

    %s .... /g 这样的搜索替换格式只能保证你在一行中被多次替换,但是一旦你插入了一个行结束符( ),这个行会中止,当前行不再继续进行替换,因此你显然不能替换为 这样的字符,这样会造成当前行不继续产生后续替换。

    至于你能够把 作为搜索 pattern 这显然是允许的。

     

     

     

     

    sed行编辑器使用

    sed: Stream Editor 流编辑器

    用法:

       sed [options] ... ‘scripts’ inputfile ...

     

    (1)常用选项

       -n 不输出模式空间中的内容至屏幕

       -e 多点编辑

       -f /PATH/TO/SCRIPT_FILE 当sed命令很长时可以写成文件,可用此选项读取

       -r 支持使用扩展正则表达式

       -i 表示在原处修改

     

    ‘scripts’包含着“地址定界”和“编辑命令”

    (2)地址定界

       1) 不给地址,则默认对全文进行处理

       2) 单地址:

          # 指定的行

       3) 地址范围

          #,#

          #,+#

          /pat1, pat2/

          #, /pat1/

          $ 最后一行

       4) 步进

          1~2 第一行开始,步进两行

          2~2 第二行开始,步进两行

     

    (3) 编辑命令

       d 删除被地址定界的行

       p 打印,显示模式空间之中的内容

       a ext 在能够被地址定界圈定的行后面追加文本,支持使用 实现多行追加

       i ext 在行前面插入文本,支持使用 实现多行插入

       c ext 替换行为单行或多行文本

       w path osomefile 保存模式空间中匹配到的内容至指定文件中

       r path osomefile 读取指定文件的文本流至模式空间中匹配到的行的行后

       = 为模式空间中的行打印行号

       ! 取反条件

       s /// 查找替换,支持使用其他分隔符:s@@@, s###

             替换标记

                g 行内全局替换

                p 显示替换成功的行

                w /path/to/somefile 将替换成功的结果保存至指定文件中

       例:仅显示被匹配到的行

          # sed -n ‘s@r..t@&er@’ /etc/passwd

     

    (4) 高级编辑命令

       h 把模式空间中的内容覆盖至保持空间中

       H 把模式空间中的内容追加到保持空间中

       g 从保持空间取出内容覆盖至模式空间中

       G 从保持空间取出内容追加至模式空间中

       x 把模式空间中的内容与保持空间中的内容进行互换

       n 读取匹配到的行的下一行至模式空间中,并覆盖前一行

       N 追加匹配到的行的下一行至模式空间中

       d 删除模式空间中的行

       D 删除多行模式空间中的所有行

    例:

       1)仅显示偶数行

       sed -n ‘n;p’ FILE

       [root@localhost test]# cat sed.txt

    This is line number 0

    This is line number 1

    This is line number 2

    This is line number 3

    This is line number 4

    This is line number 5

    This is line number 6

    This is line number 7

    This is line number 8

    This is line number 9

       [root@localhost test]# sed -n 'n;p' sed.txt

    This is line number 1

    This is line number 3

    This is line number 5

    This is line number 7

    This is line number 9

     

     

       2)逆向显示文件内容

       sed ‘1!G;h;$!d’ FILE

          第一行的内容不做G操作

          把模式空间中的内容覆盖至保持空间中

          如果模式空间中的内容是文件的最后一行就不删除

       [root@localhost test]# sed '1!G;h;$!d' sed.txt

    This is line number 9

    This is line number 8

    This is line number 7

    This is line number 6

    This is line number 5

    This is line number 4

    This is line number 3

    This is line number 2

    This is line number 1

    This is line number 0

     

     

       3) 显示文件后两行

       sed ‘$!N;$!D’ FILE

       [root@localhost test]# sed '$!N;$!D' sed.txt

    This is line number 8

    This is line number 9

     

     

       4) 取出文件最后一行

       sed ‘$!d’ FILE

       [root@localhost test]# sed '$!d' sed.txt

    This is line number 9

     

     

       5) 每行后面添一个空白行

       sed ‘G’ FILE

       [root@localhost test]# sed 'G' sed.txt

    This is line number 0

     

    This is line number 1

     

    This is line number 2

     

    This is line number 3

     

    This is line number 4

     

    This is line number 5

     

    This is line number 6

     

    This is line number 7

     

    This is line number 8

     

    This is line number 9

     

     

     

       6) 使每行后面都有一个空白行

       sed ‘/^$/d;G’ FILE

       [root@localhost test]# sed '/^$/d;G' sed.txt

    This is line number 0

     

    This is line number 1

     

    This is line number 2

     

    This is line number 3

     

    This is line number 4

     

    This is line number 5

     

    This is line number 6

     

    This is line number 7

     

    This is line number 8

     

    This is line number 9

     

     

     

       7) 显示奇数行

       sed ‘n;d’ FILE

       [root@localhost test]# sed 'n;d' sed.txt

    This is line number 0

    This is line number 2

    This is line number 4

    This is line number 6

    This is line number 8

     

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    5.24 javascrip累加求和练习
    5.23 汽车之家 界面练习
    5.22 格式与布局知识点整理
    MVC4是不是类似于html页+ashx页之间用JSON通过AJAX交换数据这种方式、?
    .net面试题
    ASP.Net1
    登陆、激活、权限
    无边框窗体
    用户控件
    弹出唯一窗口
  • 原文地址:https://www.cnblogs.com/chengtai/p/6618263.html
Copyright © 2011-2022 走看看