1. 程式人生 > 其它 >快速排序(C語言實現)

快速排序(C語言實現)

轉自 https://www.runoob.com/linux/linux-comm-sed.html

Linux sed 命令是利用指令碼來處理文字檔案。

sed 可依照指令碼的指令來處理、編輯文字檔案。

Sed 主要用來自動編輯一個或多個檔案、簡化對檔案的反覆操作、編寫轉換程式等。

動作說明

  • a :新增, a 的後面可以接字串,而這些字串會在新的一行出現(目前的下一行)~
  • c :取代, c 的後面可以接字串,這些字串可以取代 n1,n2 之間的行!
  • d :刪除,因為是刪除啊,所以 d 後面通常不接任何東東;
  • i :插入, i 的後面可以接字串,而這些字串會在新的一行出現(目前的上一行);
  • p :列印,亦即將某個選擇的資料印出。通常 p 會與引數 sed -n 一起執行~
  • s :取代,可以直接進行取代的工作哩!通常這個 s 的動作可以搭配正規表示法!例如 1,20s/old/new/g 就是啦!例項

我們先建立一個 testfile 檔案,內容如下:

$ cat testfile #檢視testfile 中的內容  
HELLO LINUX!  
Linux is a free unix-type opterating system.  
This is a linux testfile!  
Linux test 
Google
Taobao
Runoob
Tesetfile
Wiki

testfile 檔案的第四行後新增一行,並將結果輸出到標準輸出,在命令列提示符下輸入如下命令:

sed -e 4a\newLine testfile 
使用 sed 命令後,輸出結果如
$ sed -e 4a\newLine testfile 
HELLO LINUX!  
Linux is a free unix-type opterating system.  
This is a linux testfile!  
Linux test 
newLine
Google
Taobao
Runoob
Tesetfile
Wiki

以行為單位的新增/刪除

testfile 的內容列出並且列印行號,同時,請將第 2~5 行刪除!

$ nl testfile | sed '
2,5d' 1 HELLO LINUX! 6 Taobao 7 Runoob 8 Tesetfile 9 Wiki

sed 的動作為 2,5d,那個 d 是刪除的意思,因為刪除了 2-5 行,所以顯示的資料就沒有 2-5 行了, 另外,原本應該是要下達 sed -e 才對,但沒有 -e 也是可以的,同時也要注意的是, sed 後面接的動作,請務必以 '...' 兩個單引號括住喔!

只要刪除第 2 行:

$ nl testfile | sed '2d' 
     1  HELLO LINUX!  
     3  This is a linux testfile!  
     4  Linux test 
     5  Google
     6  Taobao
     7  Runoob
     8  Tesetfile
     9  Wiki

要刪除第 3 到最後一行:

$ nl testfile | sed '3,$d' 
     1  HELLO LINUX!  
     2  Linux is a free unix-type opterating system.  

在第二行後(即加在第三行) 加上drink tea? 字樣:

$ nl testfile | sed '2a drink tea'
     1  HELLO LINUX!  
     2  Linux is a free unix-type opterating system.  
drink tea
     3  This is a linux testfile!  
     4  Linux test 
     5  Google
     6  Taobao
     7  Runoob
     8  Tesetfile
     9  Wiki

如果是要在第二行前,命令如下:

$ nl testfile | sed '2i drink tea' 
     1  HELLO LINUX!  
drink tea
     2  Linux is a free unix-type opterating system.  
     3  This is a linux testfile!  
     4  Linux test 
     5  Google
     6  Taobao
     7  Runoob
     8  Tesetfile
     9  Wiki

如果是要增加兩行以上,在第二行後面加入兩行字,例如 Drink tea or .....drink beer?

$ nl testfile | sed '2a Drink tea or ......\
drink beer ?'

1  HELLO LINUX!  
     2  Linux is a free unix-type opterating system.  
Drink tea or ......
drink beer ?
     3  This is a linux testfile!  
     4  Linux test 
     5  Google
     6  Taobao
     7  Runoob
     8  Tesetfile
     9  Wiki

每一行之間都必須要以反斜槓 \ 來進行新行標記。上面的例子中,我們可以發現在第一行的最後面就有 \ 存在。

以行為單位的替換與顯示

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

$ nl testfile | sed '2,5c No 2-5 number'
     1  HELLO LINUX!  
No 2-5 number
     6  Taobao
     7  Runoob
     8  Tesetfile
     9  Wiki

透過這個方法我們就能夠將資料整行取代了。

僅列出 testfile 檔案內的第 5-7 行:

$ nl testfile | sed -n '5,7p'
     5  Google
     6  Taobao
     7  Runoob

可以透過這個 sed 的以行為單位的顯示功能, 就能夠將某一個檔案內的某些行號選擇出來顯示。

資料的搜尋並顯示

搜尋 testfile 有 oo 關鍵字的行:

$ nl testfile | sed -n '/oo/p'
     5  Google
     7  Runoob

如果 root 找到,除了輸出所有行,還會輸出匹配行。

資料的搜尋並刪除

刪除 testfile 所有包含 oo 的行,其他行輸出

$ nl testfile | sed  '/oo/d'
     1  HELLO LINUX!  
     2  Linux is a free unix-type opterating system.  
     3  This is a linux testfile!  
     4  Linux test 
     6  Taobao
     8  Tesetfile
     9  Wiki

資料的搜尋並執行命令

搜尋 testfile,找到 oo 對應的行,執行後面花括號中的一組命令,每個命令之間用分號分隔,這裡把 oo 替換為 kk,再輸出這行:

$ nl testfile | sed -n '/oo/{s/oo/kk/;p;q}'  
     5  Gkkgle

最後的 q 是退出。

資料的查詢與替換

除了整行的處理模式之外, sed 還可以用行為單位進行部分資料的查詢與替換<。

sed 的查詢與替換的與 vi 命令類似,語法格式如下:

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

將 testfile 檔案中每行第一次出現的 oo 用字串 kk 替換,然後將該檔案內容輸出到標準輸出:

sed -e 's/oo/kk/' testfile

g 識別符號表示全域性查詢替換,使 sed 對檔案中所有符合的字串都被替換,修改後內容會到標準輸出,不會修改原檔案:

sed -e 's/oo/kk/g' testfile

選項 i 使 sed 修改檔案:

sed -i 's/oo/kk/g' testfile

批量操作當前目錄下以 test 開頭的檔案:

sed -i 's/oo/kk/g' ./test*

多點編輯

一條 sed 命令,刪除 testfile 第三行到末尾的資料,並把 HELLO 替換為 RUNOOB :

$ nl testfile | sed -e '3,$d' -e 's/HELLO/RUNOOB/'
     1  RUNOOB LINUX!  
     2  Linux is a free unix-type opterating system.  

e 表示多點編輯,第一個編輯命令刪除 testfile 第三行到末尾的資料,第二條命令搜尋 HELLO 替換為 RUNOOB。

直接修改檔案內容(危險動作)

sed 可以直接修改檔案的內容,不必使用管道命令或資料流重導向! 不過,由於這個動作會直接修改到原始的檔案,所以請你千萬不要隨便拿系統配置來測試! 我們還是使用檔案 regular_express.txt 檔案來測試看看吧!

regular_express.txt 檔案內容如下:

$ cat regular_express.txt 
runoob.
google.
taobao.
facebook.
zhihu-
weibo-

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

$ sed -i 's/\.$/\!/g' regular_express.txt
$ cat regular_express.txt 
runoob!
google!
taobao!
facebook!
zhihu-
weibo-

q:q

利用 sed 直接在 regular_express.txt 最後一行加入 # This is a test:

$ sed -i '$a # This is a test' regular_express.txt
$ cat regular_express.txt 
runoob!
google!
taobao!
facebook!
zhihu-
weibo-
# This is a test

參考 :https://www.cnblogs.com/ctaixw/p/5860221.html