Linux常用基本命令:三劍客命令之-sed


sed是一個很強大的文件處理工具,主要是以行為單位進行處理,可以將數據行進行替換、刪除、新增、選取等特定工作

格式:sed [option] [command] [file]

常用命令:

        a   ∶新增
        c   ∶取代
        d   ∶刪除
         i   ∶插入
         p  ∶列印
         s  ∶取代

選項:

     -i∶直接修改讀取的檔案內容,而不是由螢幕輸出。       

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

 

1,sed '1d' ghostwu.com   d代表刪除 d前面的數字代表刪除第一行,該命令不會修改文件本身

ghostwu@dev:~/linux/sed$ cat -n ghostwu.txt 
     1    this is ghostwu
     2    how are you
     3    hod old are you
     4    and you
     5    fine thank you
     6    come with me!!!
ghostwu@dev:~/linux/sed$ sed '1d' ghostwu.txt 
how are you
hod old are you
and you
fine thank you
come with me!!!
ghostwu@dev:~/linux/sed$ cat ghostwu.txt 
this is ghostwu
how are you
hod old are you
and you
fine thank you
come with me!!!

2,刪除最后一行,$代表最后一行

ghostwu@dev:~/linux/sed$ cat ghostwu.txt 
this is ghostwu
how are you
hod old are you
and you
fine thank you
come with me!!!
ghostwu@dev:~/linux/sed$ sed '$d' ghostwu.txt 
this is ghostwu
how are you
hod old are you
and you
fine thank you

3,刪除第一行到第二行

ghostwu@dev:~/linux/sed$ cat ghostwu.txt 
this is ghostwu
how are you
hod old are you
and you
fine thank you
come with me!!!
ghostwu@dev:~/linux/sed$ sed '1,2d' ghostwu.txt 
hod old are you
and you
fine thank you
come with me!!!

4,刪除第二行到最后一行

ghostwu@dev:~/linux/sed$ sed '2,$d' ghostwu.txt 
this is ghostwu

5,查找包含'you'的行,  /you/ 這是正則表達式, p是打印,要跟n結合起來用

ghostwu@dev:~/linux/sed$ cat ghostwu.txt 
this is ghostwu
how are you
hod old are you
and you
fine thank you
come with me!!!
ghostwu@dev:~/linux/sed$ sed -n '/you/p' ghostwu.txt 
how are you
hod old are you
and you
fine thank you

6,匹配$符號結尾的行

$符號在正則表達式表示行尾,所以要用\ 轉義

ghostwu@dev:~/linux/sed$ sed -n '/\$/p' ghostwu.txt 
50$

7,在第一行后面,增加一行“你好啊"

ghostwu@dev:~/linux/sed$ cat ghostwu.txt 
this is ghostwu
how are you
hod old are you
and you
fine thank you
come with me!!!
how much do you have
50$
oh, is it?
yes
ghostwu@dev:~/linux/sed$ sed '1a 你好啊' ghostwu.txt 
this is ghostwu
你好啊
how are you
hod old are you
and you
fine thank you
come with me!!!
how much do you have
50$
oh, is it?
yes

8,在第一行和第二行的后面,增加一行

ghostwu@dev:~/linux/sed$ sed '1,2a learning how to use sed command' ghostwu.txt this is ghostwu
learning how to use sed command
how are you
learning how to use sed command
fine thank you

9,也可以增加多行

ghostwu@dev:~/linux/sed$ cat ghostwu.txt 
this is ghostwu
how are you
fine thank you
ghostwu@dev:~/linux/sed$ sed '1a 你好啊\n很高興認識你' ghostwu.txt 
this is ghostwu
你好啊
很高興認識你
how are you
fine thank you

10, c為替換操作,數字的意思,跟上面的a一樣,代表行

ghostwu@dev:~/linux/sed$ cat ghostwu.txt 
this is ghostwu
how are you
fine thank you
ghostwu@dev:~/linux/sed$ sed '1,2c hey guy' ghostwu.txt 
hey guy
fine thank you
ghostwu@dev:~/linux/sed$ sed '1c hey guy' ghostwu.txt 
hey guy
how are you
fine thank you

11, s:替換匹配到的內容, s:替換開始 /is/ 匹配包含is的行   g:全部替換

ghostwu@dev:~/linux/sed$ cat ghostwu.txt 
this is ghostwu
how are you
fine thank you
ghostwu@dev:~/linux/sed$ sed 's/is/IS/' ghostwu.txt 
thIS is ghostwu
how are you
fine thank you
ghostwu@dev:~/linux/sed$ sed 's/is/IS/g' ghostwu.txt 
thIS IS ghostwu
how are you
fine thank you
ghostwu@dev:~/linux/sed$ cat ghostwu.txt 
this is ghostwu
how are you
fine thank you

12、-i :修改,插入文件,會影響文件的內容,在最后一行,插入bye bye

ghostwu@dev:~/linux/sed$ cat ghostwu.txt 
this is ghostwu
how are you
fine thank you
ghostwu@dev:~/linux/sed$ sed -i '$a bye bye' ghostwu.txt 
ghostwu@dev:~/linux/sed$ cat ghostwu.txt 
this is ghostwu
how are you
fine thank you
bye bye

13,在1-3行,每一行的后面都插入一行數字

ghostwu@dev:~/linux/sed$ cat ghostwu.txt 
this is ghostwu
how are you
fine thank you
bye bye
ghostwu@dev:~/linux/sed$ sed -i '1,3a 123457' ghostwu.txt 
ghostwu@dev:~/linux/sed$ cat ghostwu.txt 
this is ghostwu
123457
how are you
123457
fine thank you
123457
bye bye

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM