sed是一种流编辑器,按顺序逐行处理文件。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。Sed主要用来自动编辑一个或多个文件。
命令格式
sed [参数] [动作] 一个或多个文件
参数说明
- -e 直接在命令列模式上进行 sed 的动作编辑;
- -n 使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN 的数据一般都会被列出到终端上。但如果加上 -n 参数后,则只有经过sed 特殊处理的那一行(或者动作)才会被列出来。
- -f 直接将 sed 的动作写在一个文件内, -f filename 则可以运行 filename 内的 sed 动作;
- -r sed 的动作支持的是延伸型正规表示法的语法。(默认是基础正规表示法语法)
- -i 直接修改读取的文件内容,而不是输出到终端。
动作说明
- 动作的格式如下:
[n1[,n2]]function
n1, n2 :不见得会存在,一般代表『选择进行动作的行数』,举例来说,如果我的动作是需要在 10 到 20 行之间进行的,则『 10,20[动作行为] 』
function如下:
- a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
- c :替换, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
- s :取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g 就是啦!
- d :删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
- i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
- p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
示例
以下示例使用的testfile原始内容如下:
$ cat testfile #查看testfile 中的内容
1:HELLO LINUX!
2:Linux is a free unix-type opterating system.
3:This is a linux testfile!
4:Linux test
新增行 a/i
a 在特定行增加行,i 在特定行前增加行。
- 第4行后添加一行内容
$ sed -e 4a\newline testfile #使用sed 在第四行后添加新字符串
1:HELLO LINUX!
2:Linux is a free unix-type opterating system.
3:This is a linux testfile!
4:Linux test
newLine
- 第二行后(亦即是加在第三行)加上『drink tea?』字样!
# sed '2a drink tea' testfile
1:HELLO LINUX!
2:Linux is a free unix-type opterating system.
drink tea
3:This is a linux testfile!
4:Linux test
- 在第二行后面加入两行字,例如 Drink tea or ….. 与 drink beer?
# sed '2a Drink tea or .....\
drink beer ?' testfile
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
- 在最后一行添加一行
$ sed -e '$a hello' #使用sed 在第四行后添加新字符串
1:HELLO LINUX!
2:Linux is a free unix-type opterating system.
3:This is a linux testfile!
4:Linux test
hello
替换行 c
- 将2-3行的内容替换为 No_2_3_number:
# sed '2,3c No_2_3_number' testfile
1:HELLO LINUX!
No_2_3_number
4:Linux test
取代字符 s
除了使用 c function进行整行的取代模式外, sed 还可以用行为单位进行部分数据的搜寻并取代:
sed 's/要被取代的字串/新的字串/g'
- 例如将文件中的LINUX替换为WORLD:
# sed 's/LINUX/WORLD/g' testfile
1:HELLO WORLD!
2:Linux is a free unix-type opterating system.
3:This is a linux testfile!
4:Linux test
- 搜索单词linux,将这行的testfile替换为test_sed
# sed -n '/linux/{s/testfile/test_sed/;p}' testfile
3:This is a linux test_sed!
删除行 d
- 删除 testfile 的2-4行内容。
# sed '2,4d' testfile
1:HELLO LINUX!
其中d
就是删除!因为 2-4 行给他删除了,只显示第1行的内容。
- 删除第3行内容:
# sed '3d' testfile
1:HELLO LINUX!
2:Linux is a free unix-type opterating system.
4:Linux test
- 删除第 2 到最后一行:
# sed '2,$d' testfile
1:HELLO LINUX!
- 搜索 I 字母,并删除相应的行
# sed '/I/d' testfile
2:Linux is a free unix-type opterating system.
3:This is a linux testfile!
4:Linux test
搜索显示行 p
- sed 有以行为单位的显示功能,通过 -n 参数,配合 p 功能,显示某个特定行:
# sed -n '2,3p' testfile
2:Linux is a free unix-type opterating system.
3:This is a linux testfile!
- 搜索 I 字母并显示
# sed -n '/I/p' testfile
1:HELLO LINUX!
直接修改文件
sed 可以直接修改文件的内容,不必使用管道命令或数据流重导向!
利用 sed 直接在最后一行加入 ‘# This is a test’
# sed -i '$a # This is a test' testfile
[root@localhost ~]# cat testfile
1:HELLO LINUX!
2:Linux is a free unix-type opterating system.
3:This is a linux testfile!
4:Linux test
# This is a test
其中,$
代表的是最后一行,而 a 的动作是新增,因此该文件最后新增『# This is a test』!
sed 的『 -i 』选项可以直接修改文件内容,这功能非常有帮助!举例来说,如果你有一个 100 万行的文件,你要在第 100 行加某些文字,此时使用 vim 可能会疯掉!因为文件太大了!那怎办?就利用 sed 啊!透过 sed 直接修改/取代的功能,你甚至不需要使用 vim 去修订!
参考
Was this helpful?
0 / 0