Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

bash - Extract specific string between two strings and list the required content

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:-

  1. "server_pool" is a static string
  2. "pool_name" can be any string without spaces
  3. "if a line has # in it ignore it

Requirement

  1. 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
grep -oP '^serversK[^ ]+|^[^#]s+d+:K[^:]+' inputfile
pool0
server1
server2
pool1
server3
server4

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...