Linux正则表达式

基本正则表达式:Basic REGEXP

元字符linux

描述git

.正则表达式

匹配任意单个字符vim

*编辑器

匹配其前面的字符任意次编码

.*spa

任意长度的任意字符ip

[]utf-8

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

[^]

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

[:lower:]

小写字母

[:upper:]

大写字母

[:alpha:]

全部字母

[:digit:]

数字

[:alnum:]

全部数字和字母

[:punct:]

标点符号

[:space:]

空白字符

\?

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

\{m,n\}

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

^

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

$

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

^$

表示空白行

\<或\b

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

\>或\b

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

\(\)

分组

\(ab\)*

ab做为一个总体,能够出现任意次

\(ab\).*\1

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

\(ab\).*\2

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

 

 

 

 

 

扩展正则表达式:Extended REGEXP

字符匹配

.

匹配任意单个字符

[]

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

[^]

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

次数匹配

*

匹配其前字符任意次

?

匹配其前字符0次或1次

+

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

{m,n}

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

位置铆定

^

行首

$

行尾

\<或\b

词首

\>或\b

词尾

分组

().*\1\2\3

 

或者

|

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

C|cat--> C或cat

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

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

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

 

 

 

 

vim编辑器使用

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

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

 

 

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

为何vi的替换命令里\n和\r是混用的?

%s/$/\r/g

%s/\n//g

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

 

 

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

 

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

 

 

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

%s .... /g 这样的搜索替换格式只能保证你在一行中被屡次替换,可是一旦你插入了一个行结束符(\n),这个行会停止,当前行再也不继续进行替换,所以你显然不能替换为 \n 这样的字符,这样会形成当前行不继续产生后续替换。

至于你可以把 \n 做为搜索 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 \text 在可以被地址定界圈定的行后面追加文本,支持使用\n实现多行追加

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

   c \text 替换行为单行或多行文本

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

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

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

   ! 取反条件

   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