Replacing variable Text between fixed strings

Hello all,

This is my first post and I hope you can help me out.
I searched for quite some hours now and haven't found a simple solution to my problem.

It is as following:

I got this file:

dl.dropbox.com/u/14586156/stuff/Bookmarks.plist

and want to replace the Text between "file://localhost/ and /Documents/ with whatever. (Not literaly whatever, just any other text).
( in BASH/SHELL)

Since this is not a normal sorted text file and instead a plist it seems a quite :wall: task to do.

I guess sed and gawk should be able to do this, yet neither of the solution provided anywhere helped me to accomplish this

I really hope anyone could help me with this since it is driving me crazy.

Thanks in advance for helping out a desperate shell scripting newb:

pasc
BTW: if there is a solution: Can these replacements also be limited to the ocurrences (e.g. only replace the first, second or maybe third occurence ?

Also: Is there any plist parsing utility for shell or awk that can write the parsed result to a new file in easily readable format ?

sed -r 's/(file:\/\/localhost\/).*(\/Documents\/)/\1wathever\2/g' Bookmarks.plist > out.txt

Try:

sed 's#file://localhost/.*/Documents/#whatever#' Bookmarks.plist

Perfect :slight_smile: Thanks for the quick answers.

It works!

if I wanted to do the following:

Do the above, but instead of the "whatever"
read a file in /var/mobile/ called "RELINKING.txt" and paste the first line of that file instead of "whatever"

?

Thanks again, you guys are awesome :D:b:

x=`head -1 /var/mobile/RELINKING.txt`; sed "s#file://localhost/.*/Documents/#$x#" Bookmarks.plist

Also worked :slight_smile:

Now for my last two questions on this topic:

  1. Is there a way to apply this only to the first, second whatever occurence of finding the strings localhost and Documents in the file ? (not all inisde the file ?)

  2. (unlikely), is there a plist parser for shell or awk ?

x=`head -1 /var/mobile/RELINKING.txt`; perl -pse '$n++<3 && s#file://localhost/.*/Documents/#$x#' -- -x=$x Bookmarks.plist

Change red "3" to the number of occurences you want to replace (assuming that only one "file://localhost..." entry is present per line).

can I also choose only to replace exactly the second with this ?

Cause it seems like it only stops after replacing 3 ?

Try:

x=`head -1 /var/mobile/RELINKING.txt`; perl -pse 's#file://localhost/.*/Documents/#$&# && ++$n==2 && s#file://localhost/.*/Documents/#$x#'  -- -x=$x Bookmarks.plist

x=`head -1 /var/mobile/RELINKING.txt`; sed "s#file://localhost/.*/Documents/#$x#" Bookmarks.plist

^ It seems that this one you posted works fine except for the fact that it kills the localhost and Documents portion aswell.

That Second Script, I can't try it since it requires perl, is there any substiute that works with awk or sed ?

Thanks again, really helpful.