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
上一个教程:整理一下Linux常用命令
下一个教程:find Linux命令