Going places with a Dolphin (adding to xml in sequence)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Going places with a Dolphin (adding to xml in sequence)
# 1  
Old 01-30-2011
Going places with a Dolphin (adding to xml in sequence)

Hi all, I hope someone can help me write a script that:

Reads the xml file which holds Dolphins "Places" in KDE.
If a specific tag is not there, it should create a set for it, in order.

so this little file lives in
~/.kde/share/apps/kfileplaces/bookmark.xml
Here is my example.
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xbel>
<xbel xmlns:bookmark="http://www.freedesktop.org/standards/desktop-bookmarks" 
xmlns:mime="http://www.freedesktop.org/standards/shared-mime-info" 
xmlns:kdepriv="http://www.kde.org/kdepriv" dbusName="kfilePlaces" >
 <bookmark href="file:///home/mcc" >
  <title>Home</title>
  <info>
   <metadata owner="http://freedesktop.org" >
    <bookmark:icon name="user-home" />
   </metadata>
   <metadata owner="http://www.kde.org" >
    <ID>1295957584/0</ID>
    <isSystemItem>true</isSystemItem>
   </metadata>
  </info>
 </bookmark>
 <bookmark href="remote:/" >
  <title>Network</title>
  <info>
   <metadata owner="http://freedesktop.org" >
    <bookmark:icon name="network-workgroup" />
   </metadata>
   <metadata owner="http://www.kde.org" >
    <ID>1295957584/1</ID>
    <isSystemItem>true</isSystemItem>
   </metadata>
  </info>
 </bookmark>
 <bookmark href="file:///" >
  <title>Root</title>
  <info>
   <metadata owner="http://freedesktop.org" >
    <bookmark:icon name="folder-red" />
   </metadata>
   <metadata owner="http://www.kde.org" >
    <ID>1295957584/2</ID>
    <isSystemItem>true</isSystemItem>
   </metadata>
  </info>
 </bookmark>
 <bookmark href="trash:/" >
  <title>Trash</title>
  <info>
   <metadata owner="http://freedesktop.org" >
    <bookmark:icon name="user-trash" />
   </metadata>
   <metadata owner="http://www.kde.org" >
    <ID>1295957584/3</ID>
    <isSystemItem>true</isSystemItem>
   </metadata>
  </info>
 </bookmark>
</xbel>

So it should look for a specific title, and if it is not there, create a set in sequence

for example you are looking for the title SuperDirectory, and if it is not in that file, create it.

Code:
 <bookmark href="file:///mnt/usb/superdir" >
  <title>SuperDirectory</title>
  <info>
   <metadata owner="http://freedesktop.org" >
    <bookmark:icon name="folder-red" />
   </metadata>
   <metadata owner="http://www.kde.org" >
    <ID>1295957584/4</ID>
    <isSystemItem>true</isSystemItem>
   </metadata>
  </info>
 </bookmark>

should be injected above </xbel>. So 1295957584 needs to be held as a variable I guess and then /4 figured out and tagged on.

I am thinking it should be a perl script... only... I haven't ever worked with perl, and I haven't had to work my brain in a decade either. I would really appreciate the help.

---------- Post updated at 11:17 AM ---------- Previous update was at 12:15 AM ----------

So I have been reading.

It seems like the smartest thing to do is parse the file, create an array out of the ID tags. and add 1 stored as a variable.

Then I guess I can just delete that end tag, and stuff a new bookmark set using the variable and replace the end tag... because it looks like xml doesn't much like appending sets.

Is this the right direction?

Last edited by CtrlKey; 01-30-2011 at 12:28 PM..
# 2  
Old 01-30-2011
Yes, that would appear to be a good starting point
# 3  
Old 01-31-2011
Thank you. New to Linux... programmed a few languages in the past. Never written a shell script.

So it looks like sed is going to be my friend. Ignoring the array right now, because I need baby steps, and I expect I will work that out even though it seems more complicated than what I am trying to do now..


Ok... I can just echo that over. Working on creating that odd array. I guess I will make a variable out of the number before the slash, and then a +1 array to variable for after.

I could use some direction on how to remove the slash, and create two variables... even if I just grab the last tag to split it, and add one to the end.
Code:
#!/bin/bash
cat bookmarks.xml > oldbookmarks.xml
sed -i '/<[\/]xbel>/d' bookmarks.xml
while IFS='<>' read _ starttag value endtag; do
    case "$starttag" in 
        ID) Ids+=("$value") ;;
   esac
let newid="$Ids"+1
done < bookmarks.xml
echo '<bookmark href="file:///home/public/data" >
  <title>DataDir</title>
  <info>
   <metadata owner="http://freedesktop.org" >
    <bookmark:icon name="folder-red" />
   </metadata>
   <metadata owner="http://www.kde.org" >
    <ID>'"$Ids"'</ID>
    <isSystemItem>true</isSystemItem>
   </metadata>
  </info> 
 </bookmark> 
</xbel>' >>bookmarks.xml

Not really there but closer

Thanks again.

---------- Post updated at 06:57 PM ---------- Previous update was at 03:50 PM ----------

I'm thinking I should be writing this in perl.

Of course I know even less about that.

---------- Post updated 01-31-11 at 01:00 AM ---------- Previous update was 01-30-11 at 06:57 PM ----------

This is for FOSS if it matters. I would just like direction, not solutions.

Last edited by CtrlKey; 01-30-2011 at 06:18 PM..
# 4  
Old 02-13-2011
Ok. So I have made a lot of progress. But I am cheating to make this work on a fresh install...

What I need it to do is read all of those ID tags and return the last number of the last tag and +1.

Code:
#!/bin/bash
LOGINNAME=$(logname)
cat /home/$LOGINNAME/.kde/share/apps/kfileplaces/bookmarks.xml > /home/$LOGINNAME/.kde/share/apps/kfileplaces/oldbookmarks.xml
sed -i '/<[\/]xbel>/d' /home/$LOGINNAME/.kde/share/apps/kfileplaces/bookmarks.xml 
while IFS='<>' read _ starttag value endtag; do
    case "$starttag" in 
        ID) Ids+=("$value") ;
e=$(echo $Ids | sed 's/\(.\)/\1 /g') ;
a=($e)
    esac
done < /home/$LOGINNAME/.kde/share/apps/kfileplaces/bookmarks.xml
echo '<bookmark href="file:///home/public/data" >
  <title>DataDir</title>
  <info>
   <metadata owner="http://freedesktop.org" >
    <bookmark:icon name="folder-red" />
   </metadata>
   <metadata owner="http://www.kde.org" >
    <ID>'"${a[0]}${a[1]}${a[2]}${a[3]}${a[4]}${a[5]}${a[6]}${a[7]}${a[8]}${a[9]}${a[10]}${a[11]+4}"'</ID>
    <isSystemItem>true</isSystemItem>
   </metadata>
  </info> 
 </bookmark> 
</xbel>' >>/home/$LOGINNAME/.kde/share/apps/kfileplaces/bookmarks.xml

Any help is appreciated. I am pretty sure it is in sed, but sed is a very weird tool to me.

Last edited by Neo; 02-13-2011 at 03:15 AM.. Reason: removed bump
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Help in adding sequence number to a file

Hi All , I have a file which contains data(comma separated) in below format : 500,Sourav ,kolkata ,8745775020,700091 505,ram,delhi ,9875645874,600032 510 ,madhu ,mumbai ,5698756430 ,500042 515 ,ramesh ,blore ,8769045601 ,400092 I want to add unique sequence number at the start of each... (7 Replies)
Discussion started by: STCET22
7 Replies

2. Shell Programming and Scripting

Escape Sequence Overide in XML file

Hi I am try to use sed to remove decleration information from an XML file however their are special characters in the string and sed is not able to parse it . I am using the following commond. sed -e "s/xmlns=http://www.abc.com/integration/services/testtemplate1//g" Orginal.xml... (3 Replies)
Discussion started by: jimmyb
3 Replies

3. UNIX for Dummies Questions & Answers

Adding a running sequence number while appending 2 files

Hi, I have to append 2 files and while appending i need to add a running sequence number (counter ) for each line at the first column. For e.g. If file x contains details as below. Tom Dick Harry Charlie and file y contains Boston Newyork LA Toledo Then the new file should... (1 Reply)
Discussion started by: kalyansid
1 Replies

4. UNIX for Dummies Questions & Answers

Adding Sequence Number to file

Hi All, I need to create a script which checks for a particular file for eg.kumar1.txt. If kumar1.txt is already exist the script should increment the file name as kumar2.txt and so on. Please Advise. Thanks & Regards, Kumar66 (2 Replies)
Discussion started by: kumar66
2 Replies

5. Shell Programming and Scripting

Adding sequence to the file

How do I add the sequence number to the file? I have a file seperated by commas. appusage,243,jsdgh,798 appusage,876,0989,900 . . appusage,82374,ajfgdh,9284 The output would be as below 1,appusage,243,jsdgh,798 2,appusage,876,0989,900 . . 100,appusage,876,0989,900 (5 Replies)
Discussion started by: smee
5 Replies

6. Shell Programming and Scripting

How to split a file with adding sequence number and extension.

I have a file name -HRCFTSIN05PLA1602100430444444 my requirement is to split the file in 10000 count each file and to add sequence number.rch at the end of each file. output should be in this format HRCFTSIN05PLA160210043044444401.rch HRCFTSIN05PLA160210043044444402.rch... (4 Replies)
Discussion started by: abhigrkist
4 Replies

7. Shell Programming and Scripting

Adding a sequence number within a file

Can someone please help. I need to add a sequence number to the start of each line of a file. It needs to be 6 characters long. ie 000001 next line starts 000002 etc. (4 Replies)
Discussion started by: Dolph
4 Replies

8. Shell Programming and Scripting

Adding a sequence string to a file

I have a pipe delimited file I need to add a sequence number to in the third field. The record fields will be variable length, so I have to parse for the second pipe. Another requirement is that the sequence number must be unique to all records in the file and subsequent files created, so the... (5 Replies)
Discussion started by: MrPeabody
5 Replies
Login or Register to Ask a Question