sed是一個很強(qiáng)大的文件處理工具,主要是以行為單位進(jìn)行處理,可以將數(shù)據(jù)行進(jìn)行替換、刪除、新增、選取等特定工作
格式:sed [option] [command] [file]
常用命令:
a ∶新增
c ∶取代
d ∶刪除
i ∶插入
p∶列印
s∶取代
選項(xiàng):
-i∶直接修改讀取的檔案內(nèi)容,而不是由螢?zāi)惠敵觥?/p>
-n∶使用安靜(silent)模式。在一般 sed 的用法中,所有來自 STDIN的資料一般都會被列出到螢?zāi)簧稀5绻由?-n 參數(shù)后,則只有經(jīng)過sed 特殊處理的那一行(或者動作)才會被列出來。
1,sed '1d' ghostwu.com d代表刪除 d前面的數(shù)字代表刪除第一行,該命令不會修改文件本身
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/ 這是正則表達(dá)式, p是打印,要跟n結(jié)合起來用
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
上一個教程:整理一下Linux常用命令
下一個教程:find Linux命令