基本正則表達式:Basic REGEXP
| 元字符 |
描述 |
| . |
匹配任意單個字符 |
| * |
匹配其前面的字符任意次 |
| .* |
任意長度的任意字符 |
| [] |
匹配指定范圍內的任意單個字符 |
| [^] |
匹配指定范圍外的任意單個字符 |
| [: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
