Replacing certain commands with parenthesis/brackets while keeping the content (CLI)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








up vote
1
down vote

favorite












I have a plain text file with LaTeX commands like hspace5cm. To convert this file to .odt some of my custom commands are not converted correctly. So I want to automatically find & replace specific commands but keep the content of the brackets (meaning that I want to keep most of the commands and just delete some). I know that I can just open gedit and do the replacing by hand, but this is for a script for repeated, automatic replacement.



I already searched for this, but so far only found answers to delete brackets while keeping there content (cf. here, here)). I also looked at some introductions to sed (e.g. here or here) without any success.



Example:



This is my textbftext where there are about prc5 commands, i.e. mErrRange3020m.



So, since these are a percentage and an error range, I want to delete the command & curly brackets and get something like this (keeping the textbf... command):



This is my textbftext where there are 5 % commands, i.e. 30 ± 20 m.



What I tried so far:



Various ways to usesed, like:



sed -i -e 's/\prc(.*)/1%/g' hello.txt



This already gives me:



This is my textbftext where there are about 5} commands, i.e. mErrRange3020 










up vote
1
down vote

favorite












I have a plain text file with LaTeX commands like hspace5cm. To convert this file to .odt some of my custom commands are not converted correctly. So I want to automatically find & replace specific commands but keep the content of the brackets (meaning that I want to keep most of the commands and just delete some). I know that I can just open gedit and do the replacing by hand, but this is for a script for repeated, automatic replacement.



I already searched for this, but so far only found answers to delete brackets while keeping there content (cf. here, here)). I also looked at some introductions to sed (e.g. here or here) without any success.



Example:



This is my textbftext where there are about prc5 commands, i.e. mErrRange3020m.



So, since these are a percentage and an error range, I want to delete the command & curly brackets and get something like this (keeping the textbf... command):



This is my textbftext where there are 5 % commands, i.e. 30 ± 20 m.



What I tried so far:



Various ways to usesed, like:



sed -i -e 's/\prc(.*)/1%/g' hello.txt



This already gives me:



This is my textbftext where there are about 5 commands, i.e. mErrRange3020 








up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have a plain text file with LaTeX commands like hspace5cm. To convert this file to .odt some of my custom commands are not converted correctly. So I want to automatically find & replace specific commands but keep the content of the brackets (meaning that I want to keep most of the commands and just delete some). I know that I can just open gedit and do the replacing by hand, but this is for a script for repeated, automatic replacement.



I already searched for this, but so far only found answers to delete brackets while keeping there content (cf. here, here)). I also looked at some introductions to sed (e.g. here or here) without any success.



Example:



This is my textbftext where there are about prc5 commands, i.e. mErrRange3020m.



So, since these are a percentage and an error range, I want to delete the command & curly brackets and get something like this (keeping the textbf... command):



This is my textbftext where there are 5 % commands, i.e. 30 ± 20 m.



What I tried so far:



Various ways to usesed, like:



sed -i -e 's/\prc(.*)/1%/g' hello.txt



This already gives me:



This is my textbftext where there are about 5 commands, i.e. mErrRange3020m%.



(Replacing the last curly bracket in the line, but leaving the other one in its place.)



So, now I have no clue how to continue with that. Maybe I should use another tool instead of sed?! I am happy about any suggestion running on Ubuntu & in the terminal without installing too much.










share commands, i.e. mErrRange3020 











    share|improve this answer






















    • Thank you, this works great! I was searching for the [^}]* part. It is imho hard to find good documentation on sed.
      – bamphe
      Feb 5 at 13:55










    • Oh, just saw that you forgot to escape the prc & mErrRange in the script part (but couldn't edit it since these are not > 6 char.) And thanks for the extra explanation on why to use <hello.txt here :)
      – bamphe
      Feb 5 at 14:03











    • @bamphe You're totally right, I corrected it – thank you! As for regular expressions, I really like regular-expressions.info, sed uses POSIX BRE and what you searched for are POSIX Bracket Expressions. To try it out I often use regexr.com.
      – dessert
      Feb 5 at 14:15










    • Nice! These are really helpful sites, thanks again :)
      – bamphe
      Feb 5 at 14:26










    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "89"
    ;
    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',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1003251%2freplacing-certain-commands-with-parenthesis-brackets-while-keeping-the-content%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted












    Here's a way:



    sed 's/\prc([^]*)}/1 %/g'


    This will capture everything inside the brackets following prc and save it in group 1, you can use this to replace the command with the content of the brackets. [^}]* simply takes everything except the closing bracket here. g stands for “globally” and means it will substitute all occurences of the pattern in the line instead of just the first one – you'll want that for every expression in your case.



    As for the other one, just use multiple groups:



    sed 's/\mErrRange([^]*)}([^]*)}([^]*)}/1 ± 2 3/g'


    You can combine as many of those expressions as you like, but it gets easily unclear. I recommend writing a sed script with one expression per line like so:



    #!/bin/sed -f
    s/\prc([^]*)}/1 %/g
    s/\mErrRange([^]*)}([^]*)}([^]*)}/1 ± 2 3/g


    Save it as e.g. script.sed, make it executable with chmod +x /path/to/script.sed and run it with /path/to/script.sed.



    Example run



    $ /path/to/script.sed <hello.txt
    This is my textbftext where there are about 5 % commands, i.e. 30 ± 20 m.


    I let the shell open input files as often as possible, hence <hello.txt instead of just hello.txt (which also works!). If you're interested in what ups this exactly has I recommend this answer on unix.SE.






    share|improve this answer






















    • Thank you, this works great! I was searching for the [^}]* part. It is imho hard to find good documentation on sed.
      – bamphe
      Feb 5 at 13:55










    • Oh, just saw that you forgot to escape the prc & mErrRange in the script part (but couldn't edit it since these are not > 6 char.) And thanks for the extra explanation on why to use <hello.txt here :)
      – bamphe
      Feb 5 at 14:03











    • @bamphe You're totally right, I corrected it – thank you! As for regular expressions, I really like regular-expressions.info, sed uses POSIX BRE and what you searched for are POSIX Bracket Expressions. To try it out I often use regexr.com.
      – dessert
      Feb 5 at 14:15










    • Nice! These are really helpful sites, thanks again :)
      – bamphe
      Feb 5 at 14:26














    up vote
    1
    down vote



    accepted












    Here's a way:



    sed 's/\prc([^]*)}/1 %/g'


    This will capture everything inside the brackets following prc and save it in group 1, you can use this to replace the command with the content of the brackets. [^}]* simply takes everything except the closing bracket here. g stands for “globally” and means it will substitute all occurences of the pattern in the line instead of just the first one – you'll want that for every expression in your case.



    As for the other one, just use multiple groups:



    sed 's/\mErrRange([^]*)}([^]*)}([^]*)}/1 ± 2 3/g'


    You can combine as many of those expressions as you like, but it gets easily unclear. I recommend writing a sed script with one expression per line like so:



    #!/bin/sed -f
    s/\prc([^]*)}/1 %/g
    s/\mErrRange([^]*)}([^]*)}([^]*)}/1 ± 2 3/g


    Save it as e.g. script.sed, make it executable with chmod +x /path/to/script.sed and run it with /path/to/script.sed.



    Example run



    $ /path/to/script.sed <hello.txt
    This is my textbftext where there are about 5 % commands, i.e. 30 ± 20 m.


    I let the shell open input files as often as possible, hence <hello.txt instead of just hello.txt (which also works!). If you're interested in what ups this exactly has I recommend this answer on unix.SE.






    share|improve this answer






















    • Thank you, this works great! I was searching for the [^}]* part. It is imho hard to find good documentation on sed.
      – bamphe
      Feb 5 at 13:55










    • Oh, just saw that you forgot to escape the prc & mErrRange in the script part (but couldn't edit it since these are not > 6 char.) And thanks for the extra explanation on why to use <hello.txt here :)
      – bamphe
      Feb 5 at 14:03











    • @bamphe You're totally right, I corrected it – thank you! As for regular expressions, I really like regular-expressions.info, sed uses POSIX BRE and what you searched for are POSIX Bracket Expressions. To try it out I often use regexr.com.
      – dessert
      Feb 5 at 14:15










    • Nice! These are really helpful sites, thanks again :)
      – bamphe
      Feb 5 at 14:26












    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted








    Here's a way:



    sed 's/\prc([^]*)}/1 %/g'


    This will capture everything inside the brackets following prc and save it in group 1, you can use this to replace the command with the content of the brackets. [^}]* simply takes everything except the closing bracket here. g stands for “globally” and means it will substitute all occurences of the pattern in the line instead of just the first one – you'll want that for every expression in your case.



    As for the other one, just use multiple groups:



    sed 's/\mErrRange([^]*)}([^]*)}([^]*)}/1 ± 2 3/g'


    You can combine as many of those expressions as you like, but it gets easily unclear. I recommend writing a sed script with one expression per line like so:



    #!/bin/sed -f
    s/\prc([^]*)}/1 %/g
    s/\mErrRange([^]*)}([^]*)}([^]*)}/1 ± 2 3/g


    Save it as e.g. script.sed, make it executable with chmod +x /path/to/script.sed and run it with /path/to/script.sed.



    Example run



    $ /path/to/script.sed <hello.txt
    This is my textbftext where there are about 5 % commands, i.e. 30 ± 20 m.


    I let the shell open input files as often as possible, hence <hello.txt instead of just hello.txt (which also works!). If you're interested in what ups this exactly has I recommend this answer on unix.SE.






    share|improve this answer
















    Here's a way:



    sed 's/\prc([^]*)}/1 %/g'


    This will capture everything inside the brackets following prc and save it in group 1, you can use this to replace the command with the content of the brackets. [^}]* simply takes everything except the closing bracket here. g stands for “globally” and means it will substitute all occurences of the pattern in the line instead of just the first one – you'll want that for every expression in your case.



    As for the other one, just use multiple groups:



    sed 's/\mErrRange([^]*)}([^]*)}([^]*)}/1 ± 2 3/g'


    You can combine as many of those expressions as you like, but it gets easily unclear. I recommend writing a sed script with one expression per line like so:



    #!/bin/sed -f
    s/\prc([^]*)}/1 %/g
    s/\mErrRange([^]*)}([^]*)}([^]*)}/1 ± 2 3/g


    Save it as e.g. script.sed, make it executable with chmod +x /path/to/script.sed and run it with /path/to/script.sed.



    Example run



    $ /path/to/script.sed <hello.txt
    This is my textbftext where there are about 5 % commands, i.e. 30 ± 20 m.


    I let the shell open input files as often as possible, hence <hello.txt instead of just hello.txt (which also works!). If you're interested in what ups this exactly has I recommend this answer on unix.SE.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Feb 5 at 14:08

























    answered Feb 5 at 13:00









    dessert

    20k55795




    20k55795











    • Thank you, this works great! I was searching for the [^}]* part. It is imho hard to find good documentation on sed.
      – bamphe
      Feb 5 at 13:55










    • Oh, just saw that you forgot to escape the prc & mErrRange in the script part (but couldn't edit it since these are not > 6 char.) And thanks for the extra explanation on why to use <hello.txt here :)
      – bamphe
      Feb 5 at 14:03











    • @bamphe You're totally right, I corrected it – thank you! As for regular expressions, I really like regular-expressions.info, sed uses POSIX BRE and what you searched for are POSIX Bracket Expressions. To try it out I often use regexr.com.
      – dessert
      Feb 5 at 14:15










    • Nice! These are really helpful sites, thanks again :)
      – bamphe
      Feb 5 at 14:26
















    • Thank you, this works great! I was searching for the [^}]* part. It is imho hard to find good documentation on sed.
      – bamphe
      Feb 5 at 13:55










    • Oh, just saw that you forgot to escape the prc & mErrRange in the script part (but couldn't edit it since these are not > 6 char.) And thanks for the extra explanation on why to use <hello.txt here :)
      – bamphe
      Feb 5 at 14:03











    • @bamphe You're totally right, I corrected it – thank you! As for regular expressions, I really like regular-expressions.info, sed uses POSIX BRE and what you searched for are POSIX Bracket Expressions. To try it out I often use regexr.com.
      – dessert
      Feb 5 at 14:15










    • Nice! These are really helpful sites, thanks again :)
      – bamphe
      Feb 5 at 14:26















    Thank you, this works great! I was searching for the [^}]* part. It is imho hard to find good documentation on sed.
    – bamphe
    Feb 5 at 13:55




    Thank you, this works great! I was searching for the [^}]* part. It is imho hard to find good documentation on sed.
    – bamphe
    Feb 5 at 13:55












    Oh, just saw that you forgot to escape the prc & mErrRange in the script part (but couldn't edit it since these are not > 6 char.) And thanks for the extra explanation on why to use <hello.txt here :)
    – bamphe
    Feb 5 at 14:03





    Oh, just saw that you forgot to escape the prc & mErrRange in the script part (but couldn't edit it since these are not > 6 char.) And thanks for the extra explanation on why to use <hello.txt here :)
    – bamphe
    Feb 5 at 14:03













    @bamphe You're totally right, I corrected it – thank you! As for regular expressions, I really like regular-expressions.info, sed uses POSIX BRE and what you searched for are POSIX Bracket Expressions. To try it out I often use regexr.com.
    – dessert
    Feb 5 at 14:15




    @bamphe You're totally right, I corrected it – thank you! As for regular expressions, I really like regular-expressions.info, sed uses POSIX BRE and what you searched for are POSIX Bracket Expressions. To try it out I often use regexr.com.
    – dessert
    Feb 5 at 14:15












    Nice! These are really helpful sites, thanks again :)
    – bamphe
    Feb 5 at 14:26




    Nice! These are really helpful sites, thanks again :)
    – bamphe
    Feb 5 at 14:26

















     

    draft saved


    draft discarded















































     


    draft saved


    draft discarded














    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1003251%2freplacing-certain-commands-with-parenthesis-brackets-while-keeping-the-content%23new-answer', 'question_page');

    );

    Post as a guest













































































    Popular posts from this blog

    pylint3 and pip3 broken

    Missing snmpget and snmpwalk

    How to enroll fingerprints to Ubuntu 17.10 with VFS491