检索文本例子结合sed,head,tail,cut

来源:互联网 发布:it人才网 编辑:程序博客网 时间:2024/05/02 05:06

有如下文本

[root@hadoop ~]# cat s.txt
head test
head test again
weihongrao start
this is going
weihongrao is stupy
weihongrao is the ruler
endweihongrao but still going
endweihongrao this is true end
something
nothing
nothing or somthing in test
just test line

假如我需要检索第一次开头出现weihongrao 到最后一次开头出现endweihongrao之间所有的行

[root@hadoop ~]#s=$(grep -n "^weihongrao" s.txt |head -1 |cut -d':' -f1)

[root@hadoop ~]# e=$(grep -n "^endweihongrao" s.txt |tail -1 |cut -d':' -f1)

[root@hadoop ~]# sed -n "${s},${e}p" s.txt
weihongrao start
this is going
weihongrao is stupy
weihongrao is the ruler
endweihongrao but still going
endweihongrao this is true end


0 0
原创粉丝点击