Find all lines containing only the same character and replace with the same amount of another character












0















My goal is to convert a NEWS format (the output of appstream-util appdata-to-news) to a markdown format (to use on GitHub/GitLab).



A good approximation for me would be to just go from this:



Version 0.5.1
~~~~~~~~~~~~~
Released: 2019-01-03

* This is a test with a ~ tilde ~~~~
* Second line


to this



Version 0.5.1
=============
Released: 2019-01-03

* This is a test with a ~ tilde ~~~~
* Second line


But my current solution with tr '~' '=' gives me this:



Version 0.5.1
=============
Released: 2019-01-03

* This is a test with a = tilde ====
* Second line


The regex to find all the lines containing only ~ should be this: ^~*$
But how can, with bash/sed/awk, replace with the same amount of =?










share|improve this question









New contributor




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





















  • Is it only tilde-only lines you want to replace, or are there other characters?

    – DopeGhoti
    Jan 14 at 22:03











  • As far as I know is tilde only, beside the new line at the end.

    – Roberto Leinardi
    Jan 14 at 22:04
















0















My goal is to convert a NEWS format (the output of appstream-util appdata-to-news) to a markdown format (to use on GitHub/GitLab).



A good approximation for me would be to just go from this:



Version 0.5.1
~~~~~~~~~~~~~
Released: 2019-01-03

* This is a test with a ~ tilde ~~~~
* Second line


to this



Version 0.5.1
=============
Released: 2019-01-03

* This is a test with a ~ tilde ~~~~
* Second line


But my current solution with tr '~' '=' gives me this:



Version 0.5.1
=============
Released: 2019-01-03

* This is a test with a = tilde ====
* Second line


The regex to find all the lines containing only ~ should be this: ^~*$
But how can, with bash/sed/awk, replace with the same amount of =?










share|improve this question









New contributor




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





















  • Is it only tilde-only lines you want to replace, or are there other characters?

    – DopeGhoti
    Jan 14 at 22:03











  • As far as I know is tilde only, beside the new line at the end.

    – Roberto Leinardi
    Jan 14 at 22:04














0












0








0








My goal is to convert a NEWS format (the output of appstream-util appdata-to-news) to a markdown format (to use on GitHub/GitLab).



A good approximation for me would be to just go from this:



Version 0.5.1
~~~~~~~~~~~~~
Released: 2019-01-03

* This is a test with a ~ tilde ~~~~
* Second line


to this



Version 0.5.1
=============
Released: 2019-01-03

* This is a test with a ~ tilde ~~~~
* Second line


But my current solution with tr '~' '=' gives me this:



Version 0.5.1
=============
Released: 2019-01-03

* This is a test with a = tilde ====
* Second line


The regex to find all the lines containing only ~ should be this: ^~*$
But how can, with bash/sed/awk, replace with the same amount of =?










share|improve this question









New contributor




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












My goal is to convert a NEWS format (the output of appstream-util appdata-to-news) to a markdown format (to use on GitHub/GitLab).



A good approximation for me would be to just go from this:



Version 0.5.1
~~~~~~~~~~~~~
Released: 2019-01-03

* This is a test with a ~ tilde ~~~~
* Second line


to this



Version 0.5.1
=============
Released: 2019-01-03

* This is a test with a ~ tilde ~~~~
* Second line


But my current solution with tr '~' '=' gives me this:



Version 0.5.1
=============
Released: 2019-01-03

* This is a test with a = tilde ====
* Second line


The regex to find all the lines containing only ~ should be this: ^~*$
But how can, with bash/sed/awk, replace with the same amount of =?







text-processing awk sed






share|improve this question









New contributor




Roberto Leinardi 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




Roberto Leinardi 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 Jan 14 at 22:25









don_crissti

50.3k15134162




50.3k15134162






New contributor




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









asked Jan 14 at 21:59









Roberto LeinardiRoberto Leinardi

1033




1033




New contributor




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





New contributor





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






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













  • Is it only tilde-only lines you want to replace, or are there other characters?

    – DopeGhoti
    Jan 14 at 22:03











  • As far as I know is tilde only, beside the new line at the end.

    – Roberto Leinardi
    Jan 14 at 22:04



















  • Is it only tilde-only lines you want to replace, or are there other characters?

    – DopeGhoti
    Jan 14 at 22:03











  • As far as I know is tilde only, beside the new line at the end.

    – Roberto Leinardi
    Jan 14 at 22:04

















Is it only tilde-only lines you want to replace, or are there other characters?

– DopeGhoti
Jan 14 at 22:03





Is it only tilde-only lines you want to replace, or are there other characters?

– DopeGhoti
Jan 14 at 22:03













As far as I know is tilde only, beside the new line at the end.

– Roberto Leinardi
Jan 14 at 22:04





As far as I know is tilde only, beside the new line at the end.

– Roberto Leinardi
Jan 14 at 22:04










2 Answers
2






active

oldest

votes


















4














You can pattern match for lines that contain only tilde characters, and then perform a character-by-character replacement



sed '/^~*$/s/~/=/g'





share|improve this answer
























  • It works! Thanks!

    – Roberto Leinardi
    Jan 14 at 22:05






  • 3





    or sed '/[^~]/!y/~/=/' if you want to use sed's builtin tr

    – don_crissti
    Jan 14 at 22:28











  • @don_crissti trying to get my head around "find lines without a tilde; and then don't replace it" :-) Elegant. But not immediately readable...

    – roaima
    Jan 14 at 22:59








  • 1





    Or, sed -E '/^(.)1*$/ s/./=/g' which does not hardcode the tilde character.

    – glenn jackman
    Jan 14 at 23:06











  • It's more like "on lines with other chars (those lines may or may not contain tilde chars) don't translate...". As to the readability, yes, it's sed: it's not for everyone :)

    – don_crissti
    Jan 14 at 23:09





















0














I Tried with below sed command and it worked fine



Command: sed '/Version/{n;s/~/=/g}' filename


e



n_linux_example ~]# sed '/Version/{n;s/~/=/g}' filename
Version 0.5.1
=============
Released: 2019-01-03

* This is a test with a ~ tilde ~~~~
* Second line
[root@praveen_linux_example ~]#





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
    });


    }
    });






    Roberto Leinardi 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%2f494501%2ffind-all-lines-containing-only-the-same-character-and-replace-with-the-same-amou%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









    4














    You can pattern match for lines that contain only tilde characters, and then perform a character-by-character replacement



    sed '/^~*$/s/~/=/g'





    share|improve this answer
























    • It works! Thanks!

      – Roberto Leinardi
      Jan 14 at 22:05






    • 3





      or sed '/[^~]/!y/~/=/' if you want to use sed's builtin tr

      – don_crissti
      Jan 14 at 22:28











    • @don_crissti trying to get my head around "find lines without a tilde; and then don't replace it" :-) Elegant. But not immediately readable...

      – roaima
      Jan 14 at 22:59








    • 1





      Or, sed -E '/^(.)1*$/ s/./=/g' which does not hardcode the tilde character.

      – glenn jackman
      Jan 14 at 23:06











    • It's more like "on lines with other chars (those lines may or may not contain tilde chars) don't translate...". As to the readability, yes, it's sed: it's not for everyone :)

      – don_crissti
      Jan 14 at 23:09


















    4














    You can pattern match for lines that contain only tilde characters, and then perform a character-by-character replacement



    sed '/^~*$/s/~/=/g'





    share|improve this answer
























    • It works! Thanks!

      – Roberto Leinardi
      Jan 14 at 22:05






    • 3





      or sed '/[^~]/!y/~/=/' if you want to use sed's builtin tr

      – don_crissti
      Jan 14 at 22:28











    • @don_crissti trying to get my head around "find lines without a tilde; and then don't replace it" :-) Elegant. But not immediately readable...

      – roaima
      Jan 14 at 22:59








    • 1





      Or, sed -E '/^(.)1*$/ s/./=/g' which does not hardcode the tilde character.

      – glenn jackman
      Jan 14 at 23:06











    • It's more like "on lines with other chars (those lines may or may not contain tilde chars) don't translate...". As to the readability, yes, it's sed: it's not for everyone :)

      – don_crissti
      Jan 14 at 23:09
















    4












    4








    4







    You can pattern match for lines that contain only tilde characters, and then perform a character-by-character replacement



    sed '/^~*$/s/~/=/g'





    share|improve this answer













    You can pattern match for lines that contain only tilde characters, and then perform a character-by-character replacement



    sed '/^~*$/s/~/=/g'






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 14 at 22:03









    roaimaroaima

    43.5k553116




    43.5k553116













    • It works! Thanks!

      – Roberto Leinardi
      Jan 14 at 22:05






    • 3





      or sed '/[^~]/!y/~/=/' if you want to use sed's builtin tr

      – don_crissti
      Jan 14 at 22:28











    • @don_crissti trying to get my head around "find lines without a tilde; and then don't replace it" :-) Elegant. But not immediately readable...

      – roaima
      Jan 14 at 22:59








    • 1





      Or, sed -E '/^(.)1*$/ s/./=/g' which does not hardcode the tilde character.

      – glenn jackman
      Jan 14 at 23:06











    • It's more like "on lines with other chars (those lines may or may not contain tilde chars) don't translate...". As to the readability, yes, it's sed: it's not for everyone :)

      – don_crissti
      Jan 14 at 23:09





















    • It works! Thanks!

      – Roberto Leinardi
      Jan 14 at 22:05






    • 3





      or sed '/[^~]/!y/~/=/' if you want to use sed's builtin tr

      – don_crissti
      Jan 14 at 22:28











    • @don_crissti trying to get my head around "find lines without a tilde; and then don't replace it" :-) Elegant. But not immediately readable...

      – roaima
      Jan 14 at 22:59








    • 1





      Or, sed -E '/^(.)1*$/ s/./=/g' which does not hardcode the tilde character.

      – glenn jackman
      Jan 14 at 23:06











    • It's more like "on lines with other chars (those lines may or may not contain tilde chars) don't translate...". As to the readability, yes, it's sed: it's not for everyone :)

      – don_crissti
      Jan 14 at 23:09



















    It works! Thanks!

    – Roberto Leinardi
    Jan 14 at 22:05





    It works! Thanks!

    – Roberto Leinardi
    Jan 14 at 22:05




    3




    3





    or sed '/[^~]/!y/~/=/' if you want to use sed's builtin tr

    – don_crissti
    Jan 14 at 22:28





    or sed '/[^~]/!y/~/=/' if you want to use sed's builtin tr

    – don_crissti
    Jan 14 at 22:28













    @don_crissti trying to get my head around "find lines without a tilde; and then don't replace it" :-) Elegant. But not immediately readable...

    – roaima
    Jan 14 at 22:59







    @don_crissti trying to get my head around "find lines without a tilde; and then don't replace it" :-) Elegant. But not immediately readable...

    – roaima
    Jan 14 at 22:59






    1




    1





    Or, sed -E '/^(.)1*$/ s/./=/g' which does not hardcode the tilde character.

    – glenn jackman
    Jan 14 at 23:06





    Or, sed -E '/^(.)1*$/ s/./=/g' which does not hardcode the tilde character.

    – glenn jackman
    Jan 14 at 23:06













    It's more like "on lines with other chars (those lines may or may not contain tilde chars) don't translate...". As to the readability, yes, it's sed: it's not for everyone :)

    – don_crissti
    Jan 14 at 23:09







    It's more like "on lines with other chars (those lines may or may not contain tilde chars) don't translate...". As to the readability, yes, it's sed: it's not for everyone :)

    – don_crissti
    Jan 14 at 23:09















    0














    I Tried with below sed command and it worked fine



    Command: sed '/Version/{n;s/~/=/g}' filename


    e



    n_linux_example ~]# sed '/Version/{n;s/~/=/g}' filename
    Version 0.5.1
    =============
    Released: 2019-01-03

    * This is a test with a ~ tilde ~~~~
    * Second line
    [root@praveen_linux_example ~]#





    share|improve this answer




























      0














      I Tried with below sed command and it worked fine



      Command: sed '/Version/{n;s/~/=/g}' filename


      e



      n_linux_example ~]# sed '/Version/{n;s/~/=/g}' filename
      Version 0.5.1
      =============
      Released: 2019-01-03

      * This is a test with a ~ tilde ~~~~
      * Second line
      [root@praveen_linux_example ~]#





      share|improve this answer


























        0












        0








        0







        I Tried with below sed command and it worked fine



        Command: sed '/Version/{n;s/~/=/g}' filename


        e



        n_linux_example ~]# sed '/Version/{n;s/~/=/g}' filename
        Version 0.5.1
        =============
        Released: 2019-01-03

        * This is a test with a ~ tilde ~~~~
        * Second line
        [root@praveen_linux_example ~]#





        share|improve this answer













        I Tried with below sed command and it worked fine



        Command: sed '/Version/{n;s/~/=/g}' filename


        e



        n_linux_example ~]# sed '/Version/{n;s/~/=/g}' filename
        Version 0.5.1
        =============
        Released: 2019-01-03

        * This is a test with a ~ tilde ~~~~
        * Second line
        [root@praveen_linux_example ~]#






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 16 at 18:10









        Praveen Kumar BSPraveen Kumar BS

        1,346138




        1,346138






















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










            draft saved

            draft discarded


















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













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












            Roberto Leinardi 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%2f494501%2ffind-all-lines-containing-only-the-same-character-and-replace-with-the-same-amou%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

            How to make a Squid Proxy server?

            Is this a new Fibonacci Identity?

            19世紀