我有一个如下所示的文本文件:
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
我感兴趣的只是landlord
和available
线。如何通过线grep
?我的意思是然后检查是否是或?available: 1
landlord
cat filename | grep -i 'landlord: John Smith'
available
1
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可用