2018-08-20 13:02:15 327浏览
今天扣丁学堂红帽Linux培训老师给大家介绍一下关于sed流编辑器命令,首先sed是streameditor的简称,也就是流编辑器。它一次处理一行内容,处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(patternspace),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。sed [option] 'command' input_file -n 使用安静(silent)模式(想不通为什么不是-s)。在一般sed的用法中,所有来自stdin的内容一般都会被列出到屏幕上。但如果加上-n参数后,则只有经过sed特殊处理的那一行(或者动作)才会被列出来;其中option是可选的,常用的option有如下几种: -e 直接在指令列模式上进行 sed 的动作编辑; -f 直接将 sed 的动作写在一个文件内, -f filename 则可以执行filename内的sed命令; -r 让sed命令支持扩展的正则表达式(默认是基础正则表达式); -i 直接修改读取的文件内容,而不是由屏幕输出。 常用的命令有以下几种: a \: append即追加字符串, a \的后面跟上字符串s(多行字符串可以用\n分隔),则会在当前选择的行的后面都加上字符串s; c \: 取代/替换字符串,c \后面跟上字符串s(多行字符串可以用\n分隔),则会将当前选中的行替换成字符串s; d: delete即删除,该命令会将当前选中的行删除; i \: insert即插入字符串,i \后面跟上字符串s(多行字符串可以用\n分隔),则会在当前选中的行的前面都插入字符串s; p: print即打印,该命令会打印当前选择的行到屏幕上; s: 替换,通常s命令的用法是这样的:1,2s/old/new/g,将old字符串替换成new字符串 动作说明: [n1[,n2]]function n1, n2 :不见得会存在,一般代表『选择进行动作的行数』,举例来说,如果我的动作是需要在 10 到 20 行之间进行的,则『 10,20[动作行为] 』
[root@master rh]# cat test.txt this is first line this is second line this is third line this is fourth line this fifth line happy everyday end
[root@master rh]# sed '1a \add one' test.txt this is first line add one this is second line this is third line this is fourth line this fifth line happy everyday end
[root@master rh]# sed '1,$a \add one' test.txt this is first line add one this is second line add one this is third line add one this is fourth line add one this fifth line add one happy everyday add one end add one
[root@master rh]# sed '/first/a \add one' test.txt this is first line add one this is second line this is third line this is fourth line this fifth line happy everyday end
[root@master rh]# sed '/^ha.*day$/a \add one' test.txt this is first line this is second line this is third line this is fourth line this fifth line happy everyday add one end
[root@master rh]# sed '$c \add one' test.txt this is first line this is second line this is third line this is fourth line this fifth line happy everyday add one
[root@master rh]# sed '4,$c \add one' test.txt this is first line this is second line this is third line add one
[root@master rh]# sed '/^ha.*day$/c \replace line' test.txt this is first line this is second line this is third line this is fourth line this fifth line replace line end
[root@master rh]# sed '/^ha.*day$/d' test.txt this is first line this is second line this is third line this is fourth line this fifth line end
[root@master rh]# sed '4,$d' test.txt this is first line this is second line this is third line
[root@master rh]# sed -n '4,$p' test.txt this is fourth line this fifth line happy everyday end
[root@master rh]# sed -n '/^ha.*day$/p' test.txt happy everyday
[root@master rh]# sed 's/line/text/g' test.txt this is first text this is second text this is third text this is fourth text this fifth text happy everyday end
[root@master rh]# sed '/^ha.*day$/s/happy/very happy/g' test.txt this is first line this is second line this is third line this is fourth line this fifth line very happy everyday end
[root@master rh]# sed 's/\(.*\)line$/\1/g' test.txt this is first this is second this is third this is fourth this fifth happy everyday end
[root@master rh]# sed 's/\(.*\)is\(.*\)line/\1\2/g' test.txt this first this second this third this fourth th fifth happy everyday end
正则表达式中is两边的部分可以用\1和\2表示,该例子的作用其实就是删除中间部分的is,希望对同学们有所帮助,想要了解更多关于Linux开发方面内容的小伙伴可以登录扣丁学堂官网咨询。扣丁学堂有大量的Linux视频教程让学员免费观看学习,想要快速学习Linux开发就到由专业老师授课的扣丁学堂学习吧。扣丁学堂Linux技术交流群:422345477。
【关注微信公众号获取更多学习资料】