Reputation: 35
VAR="10.101.10.5"
I have a text file abc.txt in the below format:
Name : www.ayz.com { Destination : 10.101.10.2 tcpmask members { 10.101.10.5 :http { address 10.101.10.5 } 10.101.10.3 :http { address 10.101.10.3 } }
Name : www.abc.com { Destination : 10.101.10.5 tcpmask members { 10.101.10.5 :http { address 10.101.10.5 } 10.101.10.3 :http { address 10.101.10.3 } }
The requirement is when I grep for a specific IP for example 10.101.10.5
. It should the search only the line which has the IP 10.101.10.5
after the word Destination and before the word members.
So basically I need a command which just lists the second line only when I grep for 10.101.10.5
, it should not return first line because it has IP after members.
I tried using sed
but it's not helping:
sed -n '/destination : $VAR /,/<members>/p' /abc.txt
Upvotes: 1
Views: 239
Reputation: 1428
Use double quotas and variable name. You can use also some regexp, for example grep "Destination\s*:\s*$VAR" to be more flexible for extra spaces after Destination
$ VAR="10.101.10.5"
$ grep "Destination : $VAR" file.txt
Name : www.abc.com { Destination : 10.101.10.5 tcpmask members { 10.101.10.5 :http { address 10.101.10.5 } 10.101.10.3 :http { address 10.101.10.3 } }
Upvotes: 1
Reputation: 88563
I suggest:
grep 'Destination .* 10\.101\.10\.5 .* members' abc.txt
Update:
grep "Destination .* ${VAR//./\\.} .* members" abc.txt
See: Difference between single and double quotes in bash
Upvotes: 1