GREP不相邻的多个行

分享于 

1分钟阅读

互联网

  繁體

问题:

我有一个如下所示的文本文件:


landlord: John Smith


 has:


 house: 0


 flat: 5


 available: 1


 cheap: 0


 quality: 1


landlord: Will Hall


 has:


 house: 3


 flat: 4


 available: 1


 cheap: 1


 quality: 0


landlord: Marry Moe


 has:


 house: 0


 flat: 2


 available: 1


 cheap: 1


 quality: 0



我只对landlordavailable行感兴趣,如何grepavailable: 1landlord?意思是cat filename | grep -i 'landlord: John Smith' 然后检查available1还是0


答案1:

下面是一种方法:


egrep "(landlord|available)" filename | grep -A1 "John Smith"



UPD检查可用性:


egrep "(landlord|available)" filename | grep -A1 "John Smith" | grep -c "available: 1"




答案2:

可以同时使用此命令:

grep -A 4 -i'John Smith'test.txt | grep -i available 


GREP  
相关文章