Replacing certain commands with parenthesis/brackets while keeping the content (CLI)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO9GURib1T8z7lCwjOGLQaGtrueEthgQ8LO42ZX8cOfTqDK4jvDDpKkLFwf2J49kYCMNW7d4ABih_XCb_2UXdq5fPJDkoyg7-8g_YfRUot-XnaXkNYycsNp7lA5_TW9td0FFpLQ2APzKcZ/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYQ0N5W1qAOxLP7t7iOM6O6AzbZnkXUy16s7P_CWfOb5UbTQY_aDsc727chyphenhyphen5W4IppVNernMMQeaUFTB_rFzAd95_CDt-tnwN-nBx6JyUp2duGjPaL5-VgNO41AVsA_vu30EJcipdDG409/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
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.
command-line bash scripts sed
add a comment /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.
Thank you, this works great! I was searching for the[^}]*
part. It is imho hard to find good documentation onsed
.
â bamphe
Feb 5 at 13:55
Oh, just saw that you forgot to escape theprc
&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
add a comment |Â
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.
Thank you, this works great! I was searching for the[^}]*
part. It is imho hard to find good documentation onsed
.
â bamphe
Feb 5 at 13:55
Oh, just saw that you forgot to escape theprc
&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
add a comment |Â
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.
Thank you, this works great! I was searching for the[^}]*
part. It is imho hard to find good documentation onsed
.
â bamphe
Feb 5 at 13:55
Oh, just saw that you forgot to escape theprc
&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
add a comment |Â
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.
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.
edited Feb 5 at 14:08
answered Feb 5 at 13:00
![](https://i.stack.imgur.com/9L8vd.png?s=32&g=1)
![](https://i.stack.imgur.com/9L8vd.png?s=32&g=1)
dessert
20k55795
20k55795
Thank you, this works great! I was searching for the[^}]*
part. It is imho hard to find good documentation onsed
.
â bamphe
Feb 5 at 13:55
Oh, just saw that you forgot to escape theprc
&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
add a comment |Â
Thank you, this works great! I was searching for the[^}]*
part. It is imho hard to find good documentation onsed
.
â bamphe
Feb 5 at 13:55
Oh, just saw that you forgot to escape theprc
&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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password