다음과 같은 텍스트 파일이 있습니다. 이는 구성 파일입니다.
advertised.host.name: DEPRECATED: only used when advertised.listeners or listeners are not set. Use advertised.listeners instead. Hostname to publish to ZooKeeper for clients to use. In IaaS environments, this may need to be different from the interface to which the broker binds. If this is not set, it will use the value for host.name if configured. Otherwise it will use the value returned from java.net.InetAddress.getCanonicalHostName().
Type: string
Default: node1
Valid Values:
Importance: high
Update Mode: read-only
advertised.listeners: Listeners to publish to ZooKeeper for clients to use, if different than the listeners config property. In IaaS environments, this may need to be different from the interface to which the broker binds. If this is not set, the value for listeners will be used. Unlike listeners it is not valid to advertise the 0.0.0.0 meta-address.
Type: string
Default: null
Valid Values:
Importance: high
Update Mode: per-broker
advertised.port: DEPRECATED: only used when advertised.listeners or listeners are not set. Use advertised.listeners instead. The port to publish to ZooKeeper for clients to use. In IaaS environments, this may need to be different from the port to which the broker binds. If this is not set, it will publish the same port that the broker binds to.
Type: int
Default: 5500
Valid Values:
Importance: high
Update Mode: read-only
auto.create.topics.enable: Enable auto creation of topic on the server
Type: boolean
Default: true
Valid Values:
Importance: high
Update Mode: read-only
.
.
.
우리가 원하는 것은 위의 파일을 다음과 같이 ini 파일로 변환하는 것입니다.
advertised.host.name=node1
advertised.listeners=null
advertised.port=5500
auto.create.topics.enable=true
.
.
.
참고 - 텍스트 파일의 각 매개변수는 공백 없이 파일의 시작 부분에 있으며 값은기본,
bash, awk 또는 perl/python 등을 사용하여 text
파일을 파일로 변환하는 방법에 대한 제안ini
답변1
와 함께 awk
:
$ awk -F': ' '/^[^\t ]+:/{key=$1; next}; $1 ~ /^[\t ]+Default/{print key "=" $2}' file
advertised.host.name=node1
advertised.listeners=null
advertised.port=5500
auto.create.topics.enable=true
답변2
아래 방법으로 시도했는데 잘 작동했습니다.
awk -F ":" '/advertised|auto.create/{f=$1;print f}/Default/{print $2}' filename| sed "N;s/\n/=/g"
산출
advertised.host.name=node1
advertised.listeners=null
advertised.port=5500
auto.create.topics.enable=true
파이썬
#!/usr/bin/python
import re
import itertools
from itertools import islice
final=[]
k=open('filename','r')
for i in k:
if i.startswith('advertised' or 'auto') or i.startswith('auto'):
final.append(i.split(":")[0].strip())
p=list(islice(k,3))
for z in p:
if re.search('Default',z):
final.append(z.split(":")[-1].strip())
for g in range(0, len(final),2):
print "=".join(final[g:g+2])
산출
advertised.host.name=node1
advertised.listeners=null
advertised.port=5500
auto.create.topics.enable=true