[TriLUG] Help with search and replace in vi using regexp

Chander Ganesan chander at otg-nc.com
Mon Jul 21 16:06:26 EDT 2008


Ugh.

I didn't test this, but I suppose a short script like this:

#!/bin/bash
IFS='
'
for FILE in $(cat $1); do
  echo ${FILE##*/}
done

Would work.  But that assumes you don't have escaped slashes in the names.



Brian Blater wrote:
> I'm trying to do a search and replace in a text file using vi and a
> regexp and just not getting my arms around it. I've never really tried
> using regexp before (except for something someone had already
> configured and passed on to me.) So, I hit the net and did some
> searches on how to use vi and regexp and I'm just not getting it to
> work.
>
> So, here is what I've done. I've got an .m3u file that I'm using for
> testing my regexp skills and this is what I want to do:
>
> I want to search for each line that begins with #EXT and is followed
> by any number of visible characters or spaces and delete that line. In
> vi I put the following:
>
> :%s/\#EXT([\x20-\x7E]*)$//g or several other iterations using [0-9a-zA-Z]* etc.
>
> Well they all return "pattern not found".
>
> Having failed to figure that out I'm not sure how to even go about the
> next step which is this:
>
> The file has several lines (of course) in the following format (but
> artist and filename would be different):
>
> ../../../../../audiostuff/audio/Current (70's and newer)/Dexy's
> Midnight Runners/Dexy's Midnight Runners - Come On Eileen.mp3
>
> I want to delete everything except the filename - so I would have only
> Dexy's Midnight Runners - Come On Eileen.mp3 on the line.
>
> Since the beginning part is mostly the same I could just do a search
> and replace for that without using a regexp, but how would I go about
> searching and replacing the whole line with just the filename?
>
> Thanks for any help you all can give me as I try to learn something
> new. In the windows world I used to do this with some elaborate
> wordperfect macros, but gone are those days.
>
> Thanks,
> Brian
>
> ps - any good regexp tutorials out there I should grab a copy of?
>   


-- 
Chander Ganesan
Open Technology Group, Inc.
One Copley Parkway, Suite 210
Morrisville, NC  27560
919-463-0999/877-258-8987
http://www.otg-nc.com




More information about the TriLUG mailing list