using sed command to read between sections in a given file The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsWhy the inconsistency with using cat vs. echo piped to this sed command?Replace words in file using sed commandRename file using sedRemove and replace between two specific strings using sed commandunix: replace one entire column in one file with a single value from another fileSimulate sed read command with awkBusybox ASH inside initrd has a problem when piping tee to sedsed - calling a variable from a file with multilineremove line if word start in lineshell script /unix commands for text file content editing

My body leaves; my core can stay

Can a flute soloist sit?

Separating matrix elements by lines

What information about me do stores get via my credit card?

Presidential Pardon

How do I design a circuit to convert a 100 mV and 50 Hz sine wave to a square wave?

Simulating Exploding Dice

Why are there uneven bright areas in this photo of black hole?

University's motivation for having tenure-track positions

What was the last x86 CPU that did not have the x87 floating-point unit built in?

Do working physicists consider Newtonian mechanics to be "falsified"?

Why can't wing-mounted spoilers be used to steepen approaches?

Is there a way to generate uniformly distributed points on a sphere from a fixed amount of random real numbers per point?

Is 'stolen' appropriate word?

Why are PDP-7-style microprogrammed instructions out of vogue?

should truth entail possible truth

"... to apply for a visa" or "... and applied for a visa"?

Can the DM override racial traits?

Homework question about an engine pulling a train

Is this wall load bearing? Blueprints and photos attached

Can we generate random numbers using irrational numbers like π and e?

why is the limit of this expression equal to 1?

US Healthcare consultation for visitors

Can each chord in a progression create its own key?



using sed command to read between sections in a given file



The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election ResultsWhy the inconsistency with using cat vs. echo piped to this sed command?Replace words in file using sed commandRename file using sedRemove and replace between two specific strings using sed commandunix: replace one entire column in one file with a single value from another fileSimulate sed read command with awkBusybox ASH inside initrd has a problem when piping tee to sedsed - calling a variable from a file with multilineremove line if word start in lineshell script /unix commands for text file content editing



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








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 ?










share|improve this question









New contributor




sks is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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?

    – terdon
    Apr 9 at 10:52

















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 ?










share|improve this question









New contributor




sks is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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?

    – terdon
    Apr 9 at 10:52













1












1








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 ?










share|improve this question









New contributor




sks is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












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 ?







text-processing sed






share|improve this question









New contributor




sks is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




sks is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 12 hours ago









Rui F Ribeiro

42k1483142




42k1483142






New contributor




sks is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Apr 9 at 10:45









skssks

82




82




New contributor




sks is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





sks is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






sks is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • 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?

    – terdon
    Apr 9 at 10:52

















  • 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?

    – terdon
    Apr 9 at 10:52
















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?

– terdon
Apr 9 at 10:52





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?

– terdon
Apr 9 at 10:52










2 Answers
2






active

oldest

votes


















2














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



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





share|improve this answer























  • Thanks a ton.. It worked

    – sks
    Apr 9 at 10:58


















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





share|improve this answer























    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    sks is a new contributor. Be nice, and check out our Code of Conduct.









    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f511429%2fusing-sed-command-to-read-between-sections-in-a-given-file%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














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



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





    share|improve this answer























    • Thanks a ton.. It worked

      – sks
      Apr 9 at 10:58















    2














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



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





    share|improve this answer























    • Thanks a ton.. It worked

      – sks
      Apr 9 at 10:58













    2












    2








    2







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



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





    share|improve this answer













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



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






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Apr 9 at 10:55









    RoVoRoVo

    3,761317




    3,761317












    • Thanks a ton.. It worked

      – sks
      Apr 9 at 10:58

















    • Thanks a ton.. It worked

      – sks
      Apr 9 at 10:58
















    Thanks a ton.. It worked

    – sks
    Apr 9 at 10:58





    Thanks a ton.. It worked

    – sks
    Apr 9 at 10:58













    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





    share|improve this answer



























      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





      share|improve this answer

























        0












        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





        share|improve this answer













        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 9 at 11:17









        terdonterdon

        134k33270450




        134k33270450




















            sks is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded


















            sks is a new contributor. Be nice, and check out our Code of Conduct.












            sks is a new contributor. Be nice, and check out our Code of Conduct.











            sks is a new contributor. Be nice, and check out our Code of Conduct.














            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f511429%2fusing-sed-command-to-read-between-sections-in-a-given-file%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

            Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

            Marilyn Monroe Ny fiainany manokana | Jereo koa | Meny fitetezanafanitarana azy.