0

I want to delete some lines containing specific strings from two configuration files. These files are for docks: Wbar & SimDock. I'm trying to complete my bash scripts but I cannot find an effective method for this.

This is my first file, "$HOME/.wbar":

i: /usr/share/pixmaps/wbar/dock.png
c: wbar --bpress --above-desk --pos top --grow --isize 48 --idist 3 --nanim 4 --falfa 65 --filter 0
t: /usr/share/fonts/truetype/liberation/LiberationMono-Bold/10

i: /usr/share/icons/nuoveXT2/128x128/apps/firefox.png
c: /usr/bin/www-browser
t: Web Browser

i: /usr/share/icons/nuoveXT2/128x128/apps/gthumb.png
c: gpicview
t: Image Viewer

i: /usr/share/icons/nuoveXT2/128x128/devices/media-cdrom-audio.png
c: audacious
t: Audio Player : Audacious

i: /usr/share/icons/nuoveXT2/128x128/apps/text-editor.png
c: medit
t: Text Editor

i: /usr/share/icons/nuoveXT2/128x128/apps/kfm.png
c: xfe
t: File Manager Xfe

i: /usr/share/icons/nuoveXT2/128x128/apps/terminal.png
c: xfce4-terminal
t: Terminal

i: /usr/share/pixmaps/wbar/wbar.png
c: wbar-config
t: Configure Wbar

i: /home/inukaze/dosbox/bchess/bchess.png
c: /home/inukaze/dosbox/bchess/bchess.sh
t: DosBox BattleChess

How can I delete the lines of "Battle Chess" + Last Blank Space? Should I use sed, or another method? I tried the following but it didn't work.

$ sed -i 'i: /home/inukaze/dosbox/bchess/bchess.png/d'
sed: -e expression #1, character 6: unknown order : «u»

I am also trying to modify the following file, "launchers.xml":

<?xml version="1.0" encoding="utf-8"?>
<Program>
  <SimDock>
    <path>/usr/bin/firefox</path>
    <icon>/usr/share/firefox/icons/mozicon128.png</icon>
    <description>Firefox web browser</description>
    <name>Firefox</name>
  </SimDock>
  <SimDock>
    <path>/home/inukaze/dosbox/bchess/bchess.sh</path>
    <icon>/home/inukaze/dosbox/bchess/bchess.png</icon>
    <description>Launch The BattleChess MS-D.o.s Game</description>
    <name>BattleChess</name>
  </SimDock>
</Program>

The problem with this is that every entry is contained within a <SimDock></SimDock> section, which must also be removed. I would also like to remove the section corresponding to "BattleChess" from this file.

How can I remove the sections from these two files, using sed or another method?

inukaze
  • 441
  • 1
  • 6
  • 17
  • It would make it clearer exactly what you are trying to do if you showed the desired output in the question. What do you want the file to look like afterwards? – Tom Fenech Aug 09 '14 at 18:39
  • There is a ton of similar questions. While the linked one may not be a precise duplicate, it is not hard to find similar questions with similar answers. Therefore, I don't think this one adds anything new to the StackOverflow knowledge base. – tripleee Aug 09 '14 at 20:00

2 Answers2

1

First question first: sed is getting confused because there are lots of (unescaped) slashes in your path. The easiest is to use a different control character other than forward slash (i.e '@' or '+' - anything can be used).

...but this should do it too:

sed '/\(bchess\|BattleChess\)/d' -i yourfile

(in your case '/([cC]hess)/d' would also work)

bryn
  • 3,155
  • 1
  • 16
  • 15
1

Here's a way you could remove the "bchess" section from your first file, using a Perl one-liner:

$ perl -00 -ne 'chop; $_ = "\n$_" if $. > 1; print unless /bchess/' wbar

Read in each block one at a time. Remove the last newline with chop. Add a newline before each section after the first one. Print the section if it doesn't contain "bchess".

The above approach prevents the trailing newline at the bottom of the file. If you're not bothered about that, you could go for something much simpler, like:

perl -00 -ne 'print unless /bchess/' wbar

Which just prints every block that doesn't match "bchess".

As for the XML file, I'd recommend using an XML parser such as the XML::LibXML Perl module:

use strict;
use warnings;
use XML::LibXML;

my $parser = XML::LibXML->new();
my $xmldoc = $parser->parse_file(shift);
for my $dead ($xmldoc->findnodes(q{/Program/SimDock[name = "BattleChess"]})) {
    $dead->unbindNode;
}
print $xmldoc->toString;

Usage: perl script.pl filename.xml > newfile.xml

This will remove the section of your XML file where the <name> tag contains "BattleChess".

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141