歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> Shell學習筆記 - 正則表達式

Shell學習筆記 - 正則表達式

日期:2017/3/1 12:20:21   编辑:SHELL編程
一、正則表達式是什麼? 正則表達式是用於描述字符排列和匹配模式的一種語法規則。它主要用於字符串的模式分割、匹配、查找及替換操作。 二、正則表達式與通配符 1. 正則表達式 用來在文件中匹配符合條件的字符串,正則表達式是“包含匹配”。grep、awk、sed等命令可以支持正則表達式。 2. 正則表達式元字符 正則表達式是通過元字符來進行字符串匹配的,具體請參考:/kf/201208/147682.html 3. 通配符 用來匹配符合條件的文件名,通配符是“完全匹配”。ls、find、cp這些命令不支持正則表達式,所以只能使用shell自己的通配符來進行匹配了。 4. 通配符包括 * 匹配任意字符 ? 匹配任意一個字符 [] 匹配中括號中的任意一個字符 三、cut命令 cut 命令從文件的每一行剪切字節、字符和字段並將這些字節、字符和字段寫至標准輸出。 1. 常用參數 -b :以字節為單位進行分割。這些字節位置將忽略多字節字符邊界,除非也指定了 -n 標志。 -c :以字符為單位進行分割。 -d :自定義分隔符,默認為制表符。 -f :與-d一起使用,指定顯示哪個區域。 -n :取消分割多字節字符。僅和 -b 標志一起使用。 2. 示例1:打印出用制表符分割的文件的某一行
[root@localhost shell]# cat student.txt 
ID      Name    Gender  Mark
1       ming    F       85
2       zhang   F       70
3       wang    M       75
4       li      M       90
[root@localhost shell]# cut -f 4 student.txt 
Mark
85
70
75
90

3. 示例2:打印csv文件的某一行
[root@localhost shell]# cat student.csv 
ID,Name,Gender,Mark
1,ming,F,85
2,zhang,F,70
3,wang,M,75
4,li,M,90
[root@localhost shell]# cut -d "," -f 4 student.csv 
Mark
85
70
75
90

4. 示例3:打印一個字符串的第幾個字符 [root@localhost shell]# echo "abcdef" | cut -c 3 c 5. 示例4:截取中文字符的某一個文字 [root@localhost shell]# echo "Shell編程" | cut -nb 1 S [root@localhost shell]# echo "Shell編程" | cut -nb 2 h [root@localhost shell]# echo "Shell編程" | cut -nb 3 e [root@localhost shell]# echo "Shell編程" | cut -nb 4 l [root@localhost shell]# echo "Shell編程" | cut -nb 5 l [root@localhost shell]# echo "Shell編程" | cut -nb 8 編 [root@localhost shell]# echo "Shell編程" | cut -nb 11 程 四、printf命令 1. 命令格式 printf '輸出類型輸出格式' 輸出內容 2. 輸出類型 %ns:輸出字符串。n代表輸出幾個字符,n省略則代表全部字符 %ni:輸出整數。n是指輸出幾個數字,n省略代表所有數字 %m.nf:輸出浮點數。m和n是數字,指代輸出的整數位數和小數位數。如%8.2f則代表共輸出8位數,其中2位是小樹,6位是整數。 3. 輸出格式 \a:輸出警告聲音 \b:輸出退格鍵(Backspace) \f:清除屏幕 \n:換行 \r:回車(Enter) \t:水平輸出退格鍵 \v:垂直輸出退格鍵 4. 示例 [root@localhost ~]# printf '%i %s %i %s %i\n' 1 "+" 2 "=" 3 1 + 2 = 3 [root@localhost ~]# printf '%i-%i-%i %i:%i:%i\n' 2015 12 3 21 56 30 2015-12-3 21:56:30 五、awk命令 1. 命令格式 awk '條件1{動作1}條件2{動作2}...' 文件名 條件:一般使用關系表達式作為條件,如x > 10 動作:格式化輸出、流程控制語句 2. 示例1:提取制表符分割的文件的某一行 [root@localhost shell]# cat student.txt ID Name Gender Mark 1 ming F 85 2 zhang F 70 3 wang M 75 4 li M 90 [root@localhost shell]# awk '{print $1 "\t" $4}' student.txt ID Mark 1 85 2 70 3 75 4 90 3. 示例2:獲取磁盤利用率
[root@localhost shell]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              18G  2.4G   14G  15% /
/dev/sda1             289M   16M  258M   6% /boot
tmpfs                 411M     0  411M   0% /dev/shm
[root@localhost shell]# df -h | grep "sda1" | awk '{print $5}'
6%
六、sed命令 sed是一種幾乎包括在所有UNIX平台(包括Linux)的輕量級流編輯器。sed主要是用來將數據進行選取、替換、刪除、新增的命令。 1. 命令格式 sed [選項] '[動作]' 文件名 2. 選項 -n:一般sed命令會把所有數據都輸出到屏幕,如果加入此選擇,則只會把經過sed命令處理的行輸出到屏幕。 -e:允許對輸入數據應用多條sed命令編輯。 -i:用sed的修改結果直接修改讀取數據的文件,而不是由屏幕輸出。 3. 動作 a:追加,在當前行後添加一行或多行 c:行替換,用c後面的字符串替換原數據行 i:插入,在當前行前插入一行或多行。 d:刪除,刪除指定的行 p:打印,輸出指定的行 s:字符串替換,用一個字符串替換另一個字符串。格式為“行范圍/s/舊字符串/新字符串/g”(和vim中的替換格式類似) 4. 示例
[root@localhost shell]# cat student.txt 
ID      Name    Gender  Mark
1       ming    F       85
2       zhang   F       70
3       wang    M       75
4       li      M       90

#測試-n參數
[root@localhost shell]# sed -n '2p' student.txt 
1       ming    F       85

#測試單行刪除
[root@localhost shell]# sed '2d' student.txt 
ID      Name    Gender  Mark
2       zhang   F       70
3       wang    M       75
4       li      M       90

#測試多行刪除
[root@localhost shell]# sed '2,4d' student.txt 
ID      Name    Gender  Mark
4       li      M       90

#測試追加
[root@localhost shell]# sed '2a test append' student.txt
ID      Name    Gender  Mark
1       ming    F       85
test append
2       zhang   F       70
3       wang    M       75
4       li      M       90

#測試插入
[root@localhost shell]# sed '2i test insert' student.txt
ID      Name    Gender  Mark
test insert
1       ming    F       85
2       zhang   F       70
3       wang    M       75
4       li      M       90

#測試行替換
[root@localhost shell]# sed '2c test replace' student.txt
ID      Name    Gender  Mark
test replace
2       zhang   F       70
3       wang    M       75
4       li      M       90

#測試內容替換
[root@localhost shell]# sed '2s/ming/replace/g' student.txt
ID      Name    Gender  Mark
1       replace F       85
2       zhang   F       70
3       wang    M       75
4       li      M       90


Copyright © Linux教程網 All Rights Reserved