Register
It is currently Sun Dec 21, 2014 2:44 am

Basic sed


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Wed Oct 10, 2007 12:41 pm   
User avatar

Joined: Tue Oct 02, 2007 9:22 pm
Posts: 11
Location: Argentina
I'm working on a script layout to have "theming scripts", that is, to be able to change lots of things appearance with just a command =P
I came to the conclusion that I will need to learn sed, and I'm on it. But some handy help would come great now =D
The only thing that rests unchangeable to me is my Openbox theme. There is a config file called rc.xml on ~/.config/openbox with a line that simply encloses the theme name between <theme><name></name></theme>

Now, I learned that with sed I can use -i option to take the input from the same file where I write the output. I asked for help on Freenode #Bash, but got a quickly "no" and a recommendation to use portable tools, like ed instead of sed. Is that advisable?

In any case, I found some lines on a script that seems to do something similar:

Code:
CFGDIR=$HOME/.config/openbox
cat $CFGDIR/rc.xml > $CFGDIR/rc.temp
sed -e "/<theme>/{n;s/\(<name>\).*\(<\/name>\)/\1$1\2/;}" $CFGDIR/rc.temp > rc.xml


Tell you what I don't get:

- The use of the -e option in this case. I'd understood that it's used to have multiple replacements on the same sed instance.
- The {n;s part.
- Why it uses parentheses.

Thanks for your time!


Top
 Profile  
 PostPosted: Thu Oct 11, 2007 8:53 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Hey Juan, I know what you mean. I'll research this if I get a chance today. There is so much that you can do with sed, and so many options :)


Top
 Profile YIM  
 PostPosted: Sat Jun 11, 2011 8:29 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Yes, sed(1) doesn't really edit files because it uses temporary ones, whereas ed(1) is an editor, not a stream editor.

The use of the -e option is not needed in this case, as the braces that just group commands.
The n command is here to « jump » to the line after the pattern <theme> was matched.

Code:
# search for the pattern «  <theme> »
/<theme>/

# read the next line
n

# only retrieve the pattern « <name> » and « </name> », then put a variable value in between

    # use the  (s)ubstitute  command
    s/

    # retrieve the pattern « <name> » from the line
    \(<name>\)

    # don't catch what's between the tags
    .*

    # retrieve the pattern « </name> » from the line
    \(<\/name>\)

    # end of the regex part
    /

    # replace the whole line by the first pattern matched ( « <name> » ), followed by the first argument
      passed to the script, then the second pattern that we matched ( « </name> » )
    \1$1\2

    # end of  s  command
    /


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP