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

Craig Taylor ctalkobt at gmail.com
Mon Jul 14 14:26:15 EDT 2008


First rule of regular expressions : Expect your first attempt not to work.
2nd rule of regular expression : Modify the 1st rule to reflect which rule
number you're on.

I'd try something similair to the following first, then refine it each
piece-wise:

:1,%s/^.EXP[ a-zA-Z0-9]*$//g

For a whole line match you definately should use the ^ beginning of line and
$end of line. I also used a . to match any character as I'm not certain (and
too lazy to look up) if the # or \# is what's causing you problems.

Thereafter it will look for the characterset defined in the [ ]'s any number
of times (*) and finally the end of line($).

Hope this helps, try adding stuff back in / modifying bit by bit...

On Mon, Jul 14, 2008 at 2:15 PM, Brian Blater <brb.lists at gmail.com> 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?
> --
> TriLUG mailing list        : http://www.trilug.org/mailman/listinfo/trilug
> TriLUG FAQ  : http://www.trilug.org/wiki/Frequently_Asked_Questions
>



More information about the TriLUG mailing list