LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Sed and brackets (https://www.linuxquestions.org/questions/linux-newbie-8/sed-and-brackets-836754/)

unihiekka 10-07-2010 10:12 AM

Sed and brackets
 
Hi!

I have a large (TeX) file in which there are lots of nested commands such as \cmd1{...}, for instance. I would like to change these to a new command \cmd2(...). Is there a way for sed to replace \cmd1{*} with \cmd2(*) * so that it replaces the correct brackets?

Example:

\cmd1{\somecommand{(x)}\cmd1{y}} -> \cmd2(\somecommand{(x)}\cmd2(y))

GrapefruiTgirl 10-07-2010 10:15 AM

Hi there!

The best way for someone to help with this problem, is to show a real snippet (a real sample) of the data you're trying to operate on with sed. That way we know exactly what needs to be done, i.e. are those back-slashes actually in the data, what other troublesome characters might be in the data that will mess up one sed command making the sed command need to be written specifically to avoid this.

So, show us the sed command you're currently using, and a little chunk of the real data going in, and what it should look like after it's altered, and we'll see what's what.

Cheers!

unihiekka 10-08-2010 01:27 PM

OK, here's a real snippit. The backslashes are really part of the commands themselves.

Original file:

Code:

\ed{\vec{f}(x)}\ed{y^{2}}
Should become:

Code:

\D(\vec{f}(x))\D(y^{2})
Cheers.

GrapefruiTgirl 10-08-2010 01:49 PM

Here's something to try:
Code:

sed 's/\\ed{\\vec{f}(x)}\\ed{y^{2}}/\\D(\\vec{f}(x))\\D(y^{2})/' file
I suggest redirecting the output to a new temporary file for examination, or doing this on a backup copy of the original (using the -i switch for sed-inplace), before actually operating on your good file with this. There's a lot of escaping going on so it's a little confusing and I may have missed something.
It appears to work (works for me!), but it will only work on precisely the string you gave; if there are other instances inside the file that you need to change too, but they are slightly different, you'll need to alter the regex.

Good luck; let us know if this does the job or if it needs further refinement, or you want a new idea.

jcmlq 10-08-2010 02:26 PM

s/\(\\ed\){\(\\vec{f}(x)\)}\\ed{\(y^{2}\)}/\\D(\1)(\2)\D(\3)/

The important bit are the \(STRING\) sections which becomes \1, \2, \3, etc. in the replacement string.

I also strongly suggest putting the sed command in a separate script and invoking it with -f, that way you can avoid mysterious problems with the shell trying to use parts of your sed command for its own nefarious purposes.

Dark_Helmet 10-08-2010 02:34 PM

As GrapefruiTgirl said, the sed command will only work on that particular snippet you provided. If you want something more generic, you've got more work to do.

The problem is with the nested nature of the curly braces. A static regular expression will not be able to intelligently decide which closing curly brace to change. Essentially, your solution will need a rudimentary parser--something with a basic ability to keep a running tally on curly braces: +1 for each '{' and -1 for each '}'

I'm no sed expert, but I think what you need to do would be best accomplished in a traditional scripting language rather than a one- or two-line sed substitution: Perl, Python, etc. (possibly awk, but I've never messed with awk much)

grail 10-09-2010 12:56 AM

Are we not overlooking the obvious, could just be me, but isn't this just:
Code:

sed 's/ed/D/' file

unihiekka 10-09-2010 01:31 AM

Thanks for all your suggestions. I think I'll try out Python, because I do need a more general substitution.

GrapefruiTgirl 10-09-2010 01:50 PM

Quote:

Originally Posted by grail (Post 4122149)
Are we not overlooking the obvious, could just be me, but isn't this just:
Code:

sed 's/ed/D/' file

No, there are brackets that changed too. But also, to my figuring, as this is a large document the chances of s/ed/D/ changing a crap-load of wrong additional stuff, is great.

:)

grail 10-09-2010 11:08 PM

Thanks Ggirl ... missed that completely


All times are GMT -5. The time now is 11:42 AM.