How to edit file sections that cross multiple lines?

Hello,

I'm wondering where I could go to learn how to edit file sections that cross multiple lines. I'm wanting to write scripts that will add Gnome menu entries for all users on a system for scripts I write, etc. I can search an replace simple examples with sed, but this seems more complex.

I am on a 'doz system in a hotel right now so I can't post the actual file, but the Gnome menu entry has a structure similar to this:

<Menu>
    <Name>Accessibility</Name>
    <Directory>Accessibility.directory</Directory>
    <Include>
      <And>
        <Category>Accessibility</Category>
        <Not><Category>Settings</Category></Not>
      </And>
    </Include>
    <Exclude>
      <Filename>dasher.desktop</Filename>
    </Exclude>
  </Menu>

Say I need to change the markups for the dasher.desktop file from <Exclude> to <Include> and </Exclude> to </Include>?

What If I wanted to add a new <Include></Include> section for a new .desktop file?

Compounding this is that there is variable whitespace indentation depending on how deep into the structure one needs to go and it would be nice to keep the formatting for readability and a nicer, more polished look.

Basically, what is the technique for finding something, and then modifying something on lines above and/or below it OR finding a section, and then adding something on a line below that section without modifying that line in the process.

I don't mind learning myself, but I need pointed in the right direction. I'm not sure where to start. This seems like a bit of a big job for sed. If someone would want to code an example, I'm up for that too as I can learn from that (hey, learning is fun, right?)

With thanks,
Narnie

can you please post a sample o/p ?

Please forgive me being abbreviation challenged here, but what is mean by a sample o/p? What does the o/p stand for? :slight_smile:

Thanks,
Narnie

output : )

Ah, OK. Now I feel dumb, hehe.

OK, from the code posted above, say I wanted to change it to this:

<Menu>
    <Name>Accessibility</Name>
    <Directory>Accessibility.directory</Directory>
    <Include>
      <And>
        <Category>Accessibility</Category>
          <Include>
            <Filename>dasher.desktop</Filename>
          </Include>
        <Not><Category>Settings</Category></Not>
      </And>
    </Include>
  </Menu>

I will also need to be doing the reverse. Taking this config in this reply, and converting it into the code first posted.

---------- Post updated at 06:44 PM ---------- Previous update was at 12:33 AM ----------

Output posted

Try This:

[jasonralph@shine[~]# sed 's/Include/Exclude/g' filein > fileout

And for the reverse:

[jasonralph@shine[~]# sed 's/Exclude/Include/g' filein > fileout

You will need to redirect the output to a temporary file unless your sed version supports the -i option for edit in place.

If in place editing is available:

[jasonralph@shine[~]# sed -i 's/Include/Exclude/g' filein

Hope this is what you need.
jaysunn

Sadly, that won't work, because the location needs moved within the file, not just a simple changing of the <exclude> tags to <include> tags.

I have a feeling this gets into the sed buffer and reading into the buffer and changing it there then writing it out.

you won't be able to do this reliably with sed.
it's non trivial I'd say to get it right.
it's an xml file you're better off using a proper XML processor.

how about this?
GNOME_Tips#Menu_Editing

I use alacarte for single user, but unfortunately, it only adds things for the user, not for the system (running as root doesn't work either, of course).

I have seen websites like above (the Gnome one is the best giving examples to hand-edit files and create system-wide meus and can be found at Editing System Menus), but I'm more interested in automation. I manage many computers and I would like to write a script to add these to the system menus, but there may be some user customizing, too, so I can't just make one global menu system and deploy it (also some are using Linux Mint and Ubuntu so different menus are necessary).

I'm not sure how alacarte and the one that comes with gnome works.

This is what I hate about many things going to an XML format. Makes command line manipulation difficult. I'm sure there is some module to import for python (which I am in the process of learning), perl, c, etc, but right now, that is a little beyond my scope. But I'm getting pretty good with shell programming.

Thank you for confirming what I was fearing about not being able to use sed for this.

Anyone know of a good python module for me to look into to edit XML files?

With thanks,
Narnie