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 


相关文章