有没有办法告诉sed或grep任何搜索模式可以先发生,我们需要从那里选择直到下一个搜索模式?

时间:2017-07-27 08:44:52

标签: sed grep

cat my_file  
>a1  
AAAT  
>a2  
TTAA  
>a3  
TGCA  
>a4  
TGAC  
>a5  
ATAA

然后

sed '/a4/,/a2/p' my_file

应打印出模式之间的界线

>a2  
TTAA  
>a3  
TGCA  
>a4  

两种模式都包含在内,但目前它从搜索词a4打印到文件末尾

1 个答案:

答案 0 :(得分:0)

p打印当前模式空间,是一个接受地址范围的命令。

来自手册:

$ man sed | grep -A5 Addresses
Addresses
   Sed commands can be given with no addresses, in which case the command will be executed for all input lines; with one address, in  which  case  the  command
   will  only be executed for input lines which match that address; or with two addresses, in which case the command will be executed for all input lines which
   match the inclusive range of lines starting from the first address and continuing to the second address.  Three things to note  about  address  ranges:  the
   syntax is addr1,addr2 (i.e., the addresses are separated by a comma); the line which addr1 matched will always be accepted, even if addr2 selects an earlier
   line; and if addr2 is a regexp, it will not be tested against the line that addr1 matched.

试试这个:

sed -n '/a2/,/a4/p' my_file