倘若文件test.txt的内容是:centos
ert fff ** [abcfd] 123 324 444 [rty] ** fgfgf
怎么能截取code
[abcfd] 123 324 444 [rty]
这一部分出来呢?字符串
操做命令:test
[root@centos01 t1019]# sed -nr '/\[abcfd\]/,/\[rty\]/p' test.txt [abcfd] 123 324 444 [rty]
[root@centos01 t1019]# sed -n '4,8p' test.txt [abcfd] 123 324 444 [rty]
文件内容以下sed
[root@centos01 t1019]# cat test.txt ert fff ** [abcfd] 123 324 444 [rty] ** fgfgf Test line.
[root@centos01 t1019]# sed 's/\b[a-z]/\u&/g' test.txt Ert Fff ** [Abcfd] 123 324 444 [Rty] ** Fgfgf Test Line.
[root@centos01 t1019]# sed 's/[a-z]/\u&/g' test.txt ERT FFF ** [ABCFD] 123 324 444 [RTY] ** FGFGF TEST LINE.
[root@centos01 t1019]# sed 's/[A-Z]/\l&/g' test.txt ert fff ** [abcfd] 123 324 444 [rty] ** fgfgf test line.
[root@centos01 t1019]# sed -r 's/(^f.*)/\1 888/' test.txt ert fff 888 ** [abcfd] 123 324 444 [rty] ** fgfgf 888 Test line.
方案一:循环
[root@centos01 t1019]# sed -i '/\[rty\]/{p;:a;N;$!ba;d}' test.txt [root@centos01 t1019]# cat test.txt ert fff ** [abcfd] 123 324 444 [rty]
定义一个标签a,匹配[rty],而后N把下一行加到模式空间里,匹配最后一行时,才退出标签循环,而后命令d,把这个模式空间里的内容所有清除。
if 匹配"[rty]"
:a
追加下一行
if 不匹配"$"
goto a
最后退出循环,d命令删除。文件
方案二:标签
[root@centos01 t1019]# sed -r '/\[rty\]/,$'d test.txt ert fff ** [abcfd] 123 324 444
[root@centos01 t1019]# cat -n test.txt 1 ert 2 fff 3 ** 4 [abcfd] 5 123 6 324 7 444 8 [rty] 9 fgfgf 10 Test line. [root@centos01 t1019]# sed -n '1,5{/f/p}' test.txt fff [abcfd] [root@centos01 t1019]# sed -n '1,10{/f/p}' test.txt fff [abcfd] fgfgf