1. 程式人生 > >Linux學習之shell 程式設計基礎(一)

Linux學習之shell 程式設計基礎(一)

一、linux中經常和正則表示式聯合使用的工具

grep

sed

awk(自己去研究吧).

二,以grep為例,有以下正則操作

特殊符號彙總
特殊符號 代表意義
[:alnum:] 代表英文大小寫字元及數字,亦0-9,A-Z,a-z
[:alpha:] 代表任何英文大小寫字元,亦A-Z,a-z
[:blank:] 代表空格鍵與[Tab]按鍵兩者
[:cntrl:] 代表鍵盤上面的控制按鍵,亦即包括CR、LF、Tab、Del..等等
[:digit:] 代表數字而已,亦即0-9
[:graph:] 除了空格符(空格鍵與[Tab]按鍵)外的其他所有按鍵
[:lower:] 代表小寫字元,亦即a-z
[:print:] 代表任何可以被打印出來的字元
[:punct:] 代表標點符號(punctuation,symbol),亦即A:" ' ?!;:#$...
[:upper:] 代表大寫字元,亦即A-Z
[:space:] 任何會產生空白的字元,包括空格鍵,[Tab],CR等等
[:xdigit:] 代表16進位的數字型別,因此包括:0-9,A-F,a-f的數字與字元

 

基礎正則表示法字元彙總
RE字元 意義與範例
^word

意義:待搜尋的字串(word)在行首!

範例:搜尋行首為#開始的那一行,並列出行號

grep -n '^#' regular_express.txt

word$

意義:帶搜尋的字串(word)在行尾!

範例:將行尾為!的那一行打印出來,並列出行號

grep -n '!$' regular_express.txt

.

意義:代表一定有一個任意字元的字元!

範例:搜尋的字串可以是(eve) (eae) (eee) (e e),但不能僅有(ee) ! 亦即e與e中間一定僅有一個字元,而空格符也是字元!

grep -n 'e.e' reg

\

意義:跳脫字元,將特殊符號的特殊意義去除!

範例:搜尋含有單引號'的那一行!

grep -n \' regular_express.txt

*

意義:重複零個到無窮多個的前一個RE字元

範例:找出含有(es)(ess)(esss)等等的字串,注意,因為*可以是0個,所以es也是符合待搜尋字串。另外,因為*為重複前一個RE字元的符號,因此,在*之前必須要緊接著一個RE字元喔!例如任意字元則為 .*  

[list]

意義:字元集合的RE字元,裡面列出想要的字元!

範例:搜尋含有(gl)或(gd)的那一行,需要特別留意的是,在[]當中僅代表一個待搜尋的字元,例如a[afl]y代表搜尋的字串可以是aay,afy,aly即[afl]代表a或f或l的意思!

grep -n 'g[ld]' regular_express.txt

[n1-n2]

意義:字元集合的RE字元,裡面列出想要獲取的字元範圍!

範例:搜尋含有任意數字的那一行!需特別留意,在字元集合[]中的減號 - 是有特殊意義的,他代表兩個字元之間的所有連續字元!但這個連續與否與ASCII編碼有關,因此,你的編碼需要設定正確(在bash當中,需要確定LANG與LANGUAGE的變數是否正確!) 例如所有大寫字元則為[A-Z]

grep -n '[0-9]' regular_express.txt 

或 grep -n '[[:digit:]]' regular_express.txt 

[^list]

意義:字元集合的RE字元,裡面列出不要的字串或範圍!

範例:搜尋的字串可以是(oog) (ood) 但不能是(oot),那個^在[]內時,代表的意義是反向選擇的意思。例如,我不要大寫字元,則為[^A-Z]。但是,需要特別注意的是,如果以grep -n [^A-Z] regular_express.txt 來搜尋,卻發現該檔案內的所有行都被列出,為什麼?因為這個[^A-Z]是非大寫字元的意思,因為每一行均有非大寫字元,例如第一行的"Open Source" 就有p,e,n,o...等等的小寫字

grep -n 'oo[^t]' regular_express.txt

\{n,m\}

意義:連續n到m個的前一個RE字元

意義:若為\{n\}則是連續n個的前一個RE字元。

意義:若是\{n,\}則是連續n個以上的前一個RE字元!

範例:在g與g之間有2個到3個的o存在的字串,亦即(goog)(gooog)

grep -n 'go\{2,3\}g' regular_express.txt

延伸型正則表示式字元
RE字元 意義與範例
+

意義:重複一個或一個以上的前一個RE字元

範例:搜尋(god)(good)(goood)...等等的字串。那個o+代表一個以上o,所以,底下的執行成果會將1,9,13行列出來。

egrep -n 'go+d' regular_express.txt

意義:零個或一個的前一個RE字元

範例:搜尋(gd)(god)這兩個字串。那個o?代表空的或1個o,所以,上面的執行成果會將第13,14行列出來。有沒有發現到,這兩個案例('go+d'與’go?d')的結果集合與'go*d'相同?想想看,為啥呢

egrep -n 'go?d' regular_express.txt

|

意義:用或(or)的方法找出數個字串

範例:搜尋gd或good這兩個字串,注意,是或!所以,第1,9,14這三行都可以被打印出來,那如果還想找出dog呢?

egrep -n 'gd|good' regular_express.txt

egrep -n 'gd|good|dog' regular_express.txt

()

意義:找出群組字串

範例:搜尋(glad)或(good)這兩個字串,因為g與d是重複的,所以,我就可以將la與oo列於()當中,並以|來分隔開來,就可以啦!

egrep -n 'g(la|oo)d' regular_express.txt

()+

意義:多個重複群組的判別

範例:將AxyzxyzxyzxyzC 用echo叫出,然後再使用如下的方法搜尋一下!

echo 'AxyzxyzxyzC'|egrep 'A(xyz)+C'

上面的例子意思是說,我要找開頭是A結尾是C,中間有一個以上的'xyz'字串的意思。

三、sed工具

1) 以行為單位的新增/刪除功能

    刪除2-5行

[[email protected] ~]$ nl regular_express.txt |sed '2,5d'
     1	"Open Source" is a good mechanism to develop programs.
     6	GNU is free air not free beer.
     7	Her hair is very beauty.
     8	I can't finish the test.
     9	Oh! The soup taste good.
    10	motorcycle is cheap than car.
    11	This window is clear.
    12	the symbol '*' is represented as start.
    13	Oh!	My god!
    14	The gd software is a library for drafting programs.
    15	You are the best is mean you are the no. 1.
    16	The world <Happy> is the same with "glad".
    17	I like dog.
    18	google is the best tools for search keyword.
    19	goooooogle yes!
    20	go! go! Let's go.
    21	# I am VBird

  刪除第6行

[[email protected] ~]$ nl regular_express.txt |sed '6d'
     1	"Open Source" is a good mechanism to develop programs.
     2	apple is my favorite food.
     3	Football game is not use feet only.
     4	this dress doesn't fit me.
     5	However, this dress is about $ 3183 dollars.
     7	Her hair is very beauty.
     8	I can't finish the test.
     9	Oh! The soup taste good.
    10	motorcycle is cheap than car.
    11	This window is clear.
    12	the symbol '*' is represented as start.
    13	Oh!	My god!
    14	The gd software is a library for drafting programs.
    15	You are the best is mean you are the no. 1.
    16	The world <Happy> is the same with "glad".
    17	I like dog.
    18	google is the best tools for search keyword.
    19	goooooogle yes!
    20	go! go! Let's go.
    21	# I am VBird

   刪除第6行到最後一行

[[email protected] ~]$ nl regular_express.txt |sed '6,$d'
     1	"Open Source" is a good mechanism to develop programs.
     2	apple is my favorite food.
     3	Football game is not use feet only.
     4	this dress doesn't fit me.
     5	However, this dress is about $ 3183 dollars.

  在第二行後(亦即是加在第三行)加上dalianmao字樣

[[email protected] ~]$ nl regular_express.txt |sed '2a dalianmao'
     1	"Open Source" is a good mechanism to develop programs.
     2	apple is my favorite food.
dalianmao
     3	Football game is not use feet only.
     4	this dress doesn't fit me.
     5	However, this dress is about $ 3183 dollars.
     6	GNU is free air not free beer.
     7	Her hair is very beauty.
     8	I can't finish the test.
     9	Oh! The soup taste good.
    10	motorcycle is cheap than car.
    11	This window is clear.
    12	the symbol '*' is represented as start.
    13	Oh!	My god!
    14	The gd software is a library for drafting programs.
    15	You are the best is mean you are the no. 1.
    16	The world <Happy> is the same with "glad".
    17	I like dog.
    18	google is the best tools for search keyword.
    19	goooooogle yes!
    20	go! go! Let's go.
    21	# I am VBird

   增加三行如下,每行末尾用\分隔

[[email protected] ~]$ nl regular_express.txt |sed '2a dalianmao\
> 23:root\
> 45:fidss\'
     1	"Open Source" is a good mechanism to develop programs.
     2	apple is my favorite food.
dalianmao\
23:root\
45:fidss
     3	Football game is not use feet only.
     4	this dress doesn't fit me.
     5	However, this dress is about $ 3183 dollars.
     6	GNU is free air not free beer.
     7	Her hair is very beauty.
     8	I can't finish the test.
     9	Oh! The soup taste good.
    10	motorcycle is cheap than car.
    11	This window is clear.
    12	the symbol '*' is represented as start.
    13	Oh!	My god!
    14	The gd software is a library for drafting programs.
    15	You are the best is mean you are the no. 1.
    16	The world <Happy> is the same with "glad".
    17	I like dog.
    18	google is the best tools for search keyword.
    19	goooooogle yes!
    20	go! go! Let's go.
    21	# I am VBird

   注:如果增加在前面,把a換成i即可。

2) 以行為單位的取代與顯示功能

sed [-nefr][動作]
選項與引數:
-n :使用安靜(silent)模式。在一般sed的用法中,所有來自STDIN的資料一般都會被列出到螢幕上。但如果加上-n引數後,則只有經過sed特殊處理的那一行(或者動作)才會被列出來。

-e :直接在指令列模式上進行sed的動作編輯;
-i :直接修改讀取的檔案內容,而不是由螢幕輸出。

動作說明:[n1[,n2]]function
n1,n2:不見得會存在,一般代表選擇進行動作的行數,舉例來說,如果我的動作是需要在10到20行之間進行的,則10,20[動作行為]

function有如下這些:

a:新增,a的後面可以接字串,而這些字串會在新的一行出現(目前的下一行)
c:取代,c的後面可以接字串,這些字串可以取代n1,n2之間的行
d:刪除,因為是刪除,所以d後面通常不接任何東西;
i:插入,i的後面可以接字串,而這些字串會在新的一行出現(目前的上一行);
s:取代,可以直接進行取代的工作,通常這個s的動作可以搭配正則表示式,例如'1,20s/old/new/g'就是了,如果new為變數的話,外面用雙引號。

 

 

 將第2-5 行的內容取代成為 'No 2-5 number'

[[email protected] ~]$ nl regular_express.txt |sed '2,5c No 2-5 number'
     1	"Open Source" is a good mechanism to develop programs.
No 2-5 number
     6	GNU is free air not free beer.
     7	Her hair is very beauty.
     8	I can't finish the test.
     9	Oh! The soup taste good.
    10	motorcycle is cheap than car.
    11	This window is clear.
    12	the symbol '*' is represented as start.
    13	Oh!	My god!
    14	The gd software is a library for drafting programs.
    15	You are the best is mean you are the no. 1.
    16	The world <Happy> is the same with "glad".
    17	I like dog.
    18	google is the best tools for search keyword.
    19	goooooogle yes!
    20	go! go! Let's go.
    21	# I am VBird

  列出5-7行內容

[[email protected] ~]$ nl regular_express.txt |sed -n '5,7p'
     5	However, this dress is about $ 3183 dollars.
     6	GNU is free air not free beer.
     7	Her hair is very beauty.

3) 部分資料的搜尋並取代的功能

sed 's/要被取代的字串/新的字串/g'

  獲取ip地址

[[email protected] ~]$ /sbin/ifconfig eth0|grep 'inet addr'|sed 's/^.*addr://g'|sed 's/Bcast.*$//g'
192.168.235.129  

  只要MAN存在的那幾行資料,但是含有#在內的批註我不要,而且空白行也不要

[[email protected] ~]$ cat /etc/man.config |grep 'MAN'|sed 's/#.*$//g'|sed '/^$/d'
MANPATH	/usr/man
MANPATH	/usr/share/man
MANPATH	/usr/local/man
MANPATH	/usr/local/share/man
MANPATH	/usr/X11R6/man
MANPATH_MAP	/bin			/usr/share/man
MANPATH_MAP	/sbin			/usr/share/man
MANPATH_MAP	/usr/bin		/usr/share/man
MANPATH_MAP	/usr/sbin		/usr/share/man
MANPATH_MAP	/usr/local/bin		/usr/local/share/man
MANPATH_MAP	/usr/local/sbin		/usr/local/share/man
MANPATH_MAP	/usr/X11R6/bin		/usr/X11R6/man
MANPATH_MAP	/usr/bin/X11		/usr/X11R6/man
MANPATH_MAP	/usr/bin/mh		/usr/share/man
MANSECT		1:1p:8:2:3:3p:4:5:6:7:9:0p:n:l:p:o:1x:2x:3x:4x:5x:6x:7x:8x

4)直接修改檔案內容(危險操作!!!!

  將regular_express.txt 內每一行結尾若為.,則換成!

[[email protected] ~]$ sed -i 's/\.$/\!/g' regular_express.txt 
[[email protected] ~]$ cat regular_express.txt 
"Open Source" is a good mechanism to develop programs!
apple is my favorite food!
Football game is not use feet only!
this dress doesn't fit me!
However, this dress is about $ 3183 dollars.
GNU is free air not free beer.
Her hair is very beauty.
I can't finish the test.
Oh! The soup taste good.
motorcycle is cheap than car!
This window is clear!
the symbol '*' is represented as start!
Oh!	My god!
The gd software is a library for drafting programs.
You are the best is mean you are the no. 1!
The world <Happy> is the same with "glad"!
I like dog!
google is the best tools for search keyword!
goooooogle yes!
go! go! Let's go!
# I am VBird

 利用sed直接在regular_express.txt 最後一行加入 #Hi,dalianmao

[[email protected] ~]$ sed -i '$a # Hi,dalianmao' regular_express.txt 
[[email protected] ~]$ cat regular_express.txt 
"Open Source" is a good mechanism to develop programs!
apple is my favorite food!
Football game is not use feet only!
this dress doesn't fit me!
However, this dress is about $ 3183 dollars.
GNU is free air not free beer.
Her hair is very beauty.
I can't finish the test.
Oh! The soup taste good.
motorcycle is cheap than car!
This window is clear!
the symbol '*' is represented as start!
Oh!	My god!
The gd software is a library for drafting programs.
You are the best is mean you are the no. 1!
The world <Happy> is the same with "glad"!
I like dog!
google is the best tools for search keyword!
goooooogle yes!
go! go! Let's go!
# I am VBird

# Hi,dalianmao

注:通過 wget http://linux.vbird.org/linux_basic/0330regularex/regular_express.txt 自行下載文件練習