LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 10-07-2010, 11:12 AM   #1
unihiekka
Member
 
Registered: Aug 2005
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273

Rep: Reputation: 32
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))
 
Old 10-07-2010, 11:15 AM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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.
 
Old 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: Reputation: 32
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.
 
Old 10-08-2010, 02:49 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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.
 
Old 10-08-2010, 03:26 PM   #5
jcmlq
Member
 
Registered: Aug 2009
Posts: 32

Rep: Reputation: 19
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
 
Old 10-08-2010, 03:34 PM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
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)
 
Old 10-09-2010, 01:56 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Are we not overlooking the obvious, could just be me, but isn't this just:
Code:
sed 's/ed/D/' file
 
Old 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: Reputation: 32
Thanks for all your suggestions. I think I'll try out Python, because I do need a more general substitution.
 
Old 10-09-2010, 02:50 PM   #9
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

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

 
Old 10-10-2010, 12:08 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Thanks Ggirl ... missed that completely
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[Terminal] Brackets around username AMA Linux - Newbie 5 08-06-2010 01:05 PM
brackets in bash shell lipun4u Programming 3 06-19-2010 09:24 AM
Capture data in brackets nonnumquam Programming 11 07-11-2009 11:31 PM
highlight matching brackets in vim muddywaters Programming 5 06-26-2006 09:43 AM
Kmail--Unbalanced Brackets mooreted Linux - Software 4 01-14-2005 08:41 PM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration