linux n00b

vim Search and Replace

Posted in Howto by downforce on April 15, 2010

Had some grief tonight with a search and replace string.

I am backing up some videos to an external HDD and some are files and some are folders, so I’ve created a rsync script which uses a include.txt file.

rsync script is as follows:
rsync -avr --progress --files-from=include.txt / /media/Ext\ Storage

Then in my include.txt I have the following:
data/videos/dir1/
data/videos/dir2/
data/videos/file1.avi
data/videos/file2.mkv

Which I obtained from doing a find dump into a file find /data/videos/ > include.txt

Except for the includes.txt, it the directories need to have the trailing slash.

I first off tried to create a regex search and replace in vim which found all rows that didn’t have a file extension. I managed to create a expression which DID find them but couldn’t work out how to then wrap that in a NOT.

So I thought screw it, I’ll add a trailing slash / to every row then strip them out and after 45min of screwing around I finally got it.

:1,$ s/\(\.[a-zA-Z]*\)\/$/\1/c

I used /c just to make sure it wasn’t going to go crazy, and it didn’t!

It’s probably not the most elegant or efficient but I have a sense of achievement all the same 🙂

Tagged with: , , , ,