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