GREP multiple lines that are not next to each other
I have a text file that looks like this:
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
All I am interested in is landlord
and available
lines. How to grep
the available: 1
line by landlord
?
I mean cat filename | grep -i 'landlord: John Smith'
and then check if available
is 1
or 0
?
Here is one way to do it:
egrep "(landlord|available)" filename | grep -A1 "John Smith"
UPD to check availability:
egrep "(landlord|available)" filename | grep -A1 "John Smith" | grep -c "available: 1"
You can use this command for the same:
grep -A 4 -i 'John Smith' test.txt | grep -i available