1

File content.

[kafka_properties]
listeners=PLAINTEXT://:KAFKA_CLIENT_PORT
default.replication.factor=2
ssl.client.auth=required
ssl.cipher.suites=["TLS_DHE_RSA_WITH_AES_256_GCM_SHA384",
            "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256",
            "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256",
            "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256",]
ssl.enabled.protocols=TLSV1.2
ssl.secure.random.implem=SHA1PRNG
security.inter.broker.protocol=PLAINTEXT
security.protocol=SSL
ssl.endpoint.identification.algorithm=https


[kafka_ports]
KAFKA_CLIENT_PORT=9082

[zookeeper_properties]
clientPort=ZK_CLIENT_PORT
syncLimit=2
initLimit=5
tickTime=2000
autopurge.snapRetainCount=3
autopurge.purgeInterval=1
admin.serverPort=ZK_SERVER_ADMIN_PORT

I am trying to read the values from every section, e.g. [kafka_properties], or [kafka_ports] using this command:

cat file.txt | sed -n '0,/kafka_properties/d;/\[/,$d;/^$/d;p'

And write the values into a different file. It works okay if I don't add the parameter:

ssl.cipher.suites=["TLS_DHE_RSA_WITH_AES_256_GCM_SHA384",
            "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256",
            "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256",
            "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256",] 

but after adding the ssl.cipher.suites= parameter to the file.txt sed is not working as expected. Where am I going wrong ?

1
  • Please edit your question and explain how sed is "not working as expected". What output were you expecting and what output do you actually get? Commented Apr 9, 2019 at 10:52

2 Answers 2

2

Make [ only match at the beginning of the line with ^:

sed -n '0,/kafka_properties/d;/^\[/,$d;/^$/d;p' file.txt
0
0

For the general case, writing each section to a different file without needing to know the section names, you could do:

awk '/^\[/{n=$1;gsub(/[][]/,"",n)}{print >> n".txt"}' file

This will create these files from your example:

kafka_ports.txt  kakfa_properties.txt  zookeeper_properties.txt

Explanation

  • /^\[/{n=$1;gsub(/[][]/,"",n)} : if this line starts with a [, save the 1st field as the variable n and remove all [ or ] from it.
  • print >> n".txt" : append the curent line to the file n.txt where n is the name of the section.

Note that this assumes you never have whitespace in a section name. If you do, try this instead:

awk '/^\[/{n=$0;gsub(/[][]/,"",n); gsub(/ /,"_",n)}{print >> n".txt"}' file

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.