how to find the block name in which the string available ?
server.conf file
server_pool odd {
0:server1:yes:profile_server1:192.168.1.1,192.168.1.2;
1:server3:yes:profile_server3:192.168.1.5,192.168.1.6;
}
server_pool even {
0:server2:yes:profile_server2:192.168.1.3,192.168.1.4;
1:server4:yes:profile_server4:192.168.1.7,192.168.1.8;
}
#server_pool even {
# 0:server1:yes:profile_server1:192.168.1.1,192.168.1.2;
# 1:server3:yes:profile_server3:192.168.1.5,192.168.1.6;
#}
Notes:-
- "server_pool" is a static string
- "pool_name" can be any string without spaces
- "if a line has # in it ignore it
Requirement
- Need to find the "pool_name" by the provided server hostname as input i.e server{1,2,3,} and store it in a variable
for example
if need to find server1 belongs to which block/ stanza. in the given use case it belongs to odd, so store variable as POOLNAME=odd
Output
Note
I suppossed that pool are always starting by 0 and that you can not have pool with an index > 9. If that's not the case you can change, for example
[0-9]
to[0-9]{1,2}
to accept number between -1 and 100.Following awk may help you in same.
EDIT: If your pool block you could have many other entries other than servers then I have added an additional check for it, try it and let me know then.
EDIT2: Showing OP that code is providing the expected output by OP only.
Using
awk
Better Readable:
Here is Test results:
Input:
Output:
GRPNAME="server pool0 {" GRPNAME=${GRPNAME%{*}; GRPNAME=${GRPNAME#*\ }
This might work for you (GNU sed):
Focus on lines that begin
server
and extract the first two words from such lines. From subsequent lines, extract the second field (using:
as a separator) until a failure to match occurs.