VIM Advanced text string search HELP

39 views
Skip to first unread message

Matt Truslove

unread,
May 25, 2018, 4:21:52 AM5/25/18
to vim_use
Good morning,
I have a very important XML file which contains a lot of important metadata. Recently my old computer has become useless so i managed to backup all my data and export all my files.

as part of the backup i exported the metadata via an XML feature. But not realising that the new location would be different for all the files once moved.

the XML file has a tag for Location and i need this to change whilst retaining the file name.

i was told VIM is perfect for this. so fired fired up my old linux box to give it a go but no avail.

please see the below example

<key>Location</key><string>file:///Volumes/OLDDRIVE/OTHERFOLDER/aaaaa/bbbbb/ccccc/ddddd/eeeee/20Berksonax.uue</string>

i want to find and replace the text between the "file:///" and the last but one "/" (as the last slash will affect the XML </string>.) but retain the file name, extension and end tag </string>

to end up with


<key>Location</key><string>file:///NewVolume/NEW_SSD/NEWFOLDER2018/0Berksonax.uue</string>

Keith Edmunds

unread,
May 25, 2018, 4:25:07 AM5/25/18
to vim...@googlegroups.com
If it were me, I'd put symlinks on the system to match the old path so
that the file doesn't need editing.

I'd also review my backup AND TEST RESTORE process.

--
"The best argument against democracy is a five-minute conversation with
the average voter" - Winston Churchill

Shlomi Fish

unread,
May 25, 2018, 4:36:47 AM5/25/18
to vim...@googlegroups.com
Hi Matt,

On Fri, 25 May 2018 01:16:54 -0700 (PDT)
Matt Truslove <m81t...@gmail.com> wrote:

> Good morning,
> I have a very important XML file which contains a lot of important metadata.
> Recently my old computer has become useless so i managed to backup all my
> data and export all my files.
>
> as part of the backup i exported the metadata via an XML feature. But not
> realising that the new location would be different for all the files once
> moved.
>

see http://perl-begin.org/uses/text-parsing/ and
http://perl-begin.org/uses/xml/ and equivalents for other languages. See
https://blog.codinghorror.com/parsing-html-the-cthulhu-way/ .


> the XML file has a tag for Location and i need this to change whilst
> retaining the file name.
>
> i was told VIM is perfect for this. so fired fired up my old linux box to
> give it a go but no avail.
>
> please see the below example
>
> <key>Location</key><string>file:///Volumes/OLDDRIVE/OTHERFOLDER/aaaaa/bbbbb/ccccc/ddddd/eeeee/20Berksonax.uue</string>
>
> i want to find and replace the text between the "file:///" and the last but
> one "/" (as the last slash will affect the XML </string>.) but retain the
> file name, extension and end tag </string>
>
> to end up with
>
>
> <key>Location</key><string>file:///NewVolume/NEW_SSD/NEWFOLDER2018/0Berksonax.uue</string>
>



--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
http://youtu.be/xZLwtc9x4yA - Anime in Real Life!! (Parody)

What's the point of claiming your code is illegal?
I might anger a few, but I just wanna sue
http://www.shlomifish.org/humour/bits/Can-I-SCO-Now/

Please reply to list if it's a mailing list post - http://shlom.in/reply .

Matt Truslove

unread,
May 25, 2018, 4:51:23 AM5/25/18
to vim_use
Thank you for the resources, but all i have at the moment is a linux terminal and my work machine, so resources and software is limited

John Little

unread,
May 25, 2018, 6:14:01 AM5/25/18
to vim_use
It may help if you can start vim in non-compatible mode, such as with

vi -N

I suggest also turning on search highlighting:

:set hlsearch

If the path you want to replace does not have any dots, I would first search for the text that needs to be replaced, perhaps

/\/\/\/[^.]\+\/

With search highlighting, you can see what's been matched. When you're happy with it,

:%s##///new/path/to/file/

(Note the # above is an arbitrary choice, usually one uses / but that's messy here.)

Tom M

unread,
May 25, 2018, 7:29:11 AM5/25/18
to vim_use

Hello,

how about this substitution?

%s#\(<key>Location</key><string>file://\)/\(Volumes\)/\(OLDDRIVE\)/\(OTHERFOLDER\)/.*/\(.*\)\(</string>\)#\1/NewVolume/NEW_SSD/NEWFOLDER2018/\5\6#

First try on one line (without the '%') to see if the result is precisely what you need.

Tom

Matt Truslove

unread,
May 25, 2018, 7:36:20 AM5/25/18
to vim_use
Great, i will run some tests now. thanks for your help, i will report back

Matt Truslove

unread,
May 25, 2018, 8:29:48 AM5/25/18
to vim_use
Thank you for all your help, TOM M had the solution
Reply all
Reply to author
Forward
0 new messages