Latest LQ Deal: Linux Power User Bundle
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org Sed and brackets
 Linux - Newbie This Linux forum is for members that are new to Linux. Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

 10-07-2010, 11:12 AM #1 unihiekka Member   Registered: Aug 2005 Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu Posts: 273 Rep: 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))
 10-07-2010, 11:15 AM #2 GrapefruiTgirl LQ Guru   Registered: Dec 2006 Location: underground Distribution: Slackware64 Posts: 7,594 Rep: 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! Last edited by GrapefruiTgirl; 10-07-2010 at 11:16 AM.
 10-08-2010, 02:27 PM #3 unihiekka Member   Registered: Aug 2005 Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu Posts: 273 Original Poster Rep: 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.
 10-08-2010, 02:49 PM #4 GrapefruiTgirl LQ Guru   Registered: Dec 2006 Location: underground Distribution: Slackware64 Posts: 7,594 Rep: 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.
 10-08-2010, 03:26 PM #5 jcmlq Member   Registered: Aug 2009 Posts: 32 Rep: 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. Last edited by jcmlq; 10-08-2010 at 06:13 PM. Reason: colored the saved matches
 10-08-2010, 03:34 PM #6 Dark_Helmet Senior Member   Registered: Jan 2003 Posts: 2,786 Rep: 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)
 10-09-2010, 01:56 AM #7 grail LQ Guru   Registered: Sep 2009 Location: Perth Distribution: Manjaro Posts: 9,250 Rep: Are we not overlooking the obvious, could just be me, but isn't this just: Code: sed 's/ed/D/' file
 10-09-2010, 02:31 AM #8 unihiekka Member   Registered: Aug 2005 Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu Posts: 273 Original Poster Rep: Thanks for all your suggestions. I think I'll try out Python, because I do need a more general substitution.
10-09-2010, 02:50 PM   #9
GrapefruiTgirl
LQ Guru

Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep:
Quote:
 Originally Posted by grail 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.

 10-10-2010, 12:08 AM #10 grail LQ Guru   Registered: Sep 2009 Location: Perth Distribution: Manjaro Posts: 9,250 Rep: Thanks Ggirl ... missed that completely

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post AMA Linux - Newbie 5 08-06-2010 01:05 PM lipun4u Programming 3 06-19-2010 09:24 AM nonnumquam Programming 11 07-11-2009 11:31 PM muddywaters Programming 5 06-26-2006 09:43 AM mooreted Linux - Software 4 01-14-2005 08:41 PM

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

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -