Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

vim - greedy select with slashes

1 view
Skip to first unread message

Ken Holm

unread,
Apr 19, 2006, 5:08:58 PM4/19/06
to
I have the following data in a file:

1 joeuser johnson 2702 Mar 20 08:37 /tmp/awenv.pl
1 root system 579 Mar 29 15:44
/usr/local/development/awenv.pl
1 root system 489 Nov 4 15:57
/usr/local/development/qa/awenv.pl
1 root system 30 Dec 19 11:42
/usr/local/development/dev/awenv.pl -> /development/we
1 root umcweb 551 Feb 1 12:07
/backup/development/web/cgi-local/awenv.pl
1 root system 546 Jan 26 09:35 /qa/web/cgi-local/awenv.pl
1 root system 489 Feb 3 2005
/qa/web/cgi-local/Archives/ESS.20050318/Archives/awenv.
1 root system 2721 Mar 20 08:39
/qa/web/cgi-local/Archives/deferredCompensation.2006040
1 root system 600 Feb 3 2005
/development/web/cgi-local/Archives/ESS.20050506/Archi
1 313 staff 2587 Mar 27 07:28
/development/web/cgi-local/awenv.pl

I would like to delete everything up to the first slash

:%s/^.*\/\{-}//g grabs to the last slash. So does:

:%s/^.*\/\{-}// (no g)
:%s/^.*\/\{1}//
:%s/^.*\/\?//

What am I missing here?

Many thanks,

-Ken

Bob Stearns

unread,
Apr 19, 2006, 5:44:06 PM4/19/06
to
Ken Holm wrote:

%s.^[^/]*..

Start at the beginning, grab everything that isn't a slash and remove it.

Gary Johnson

unread,
Apr 19, 2006, 5:57:13 PM4/19/06
to
Ken Holm <kholm....@gmail.com> wrote:
> I have the following data in a file:
>
> 1 joeuser johnson 2702 Mar 20 08:37 /tmp/awenv.pl
> 1 root system 579 Mar 29 15:44
> /usr/local/development/awenv.pl
> 1 root system 489 Nov 4 15:57
> /usr/local/development/qa/awenv.pl
> 1 root system 30 Dec 19 11:42
> /usr/local/development/dev/awenv.pl -> /development/we
> 1 root umcweb 551 Feb 1 12:07
> /backup/development/web/cgi-local/awenv.pl
> 1 root system 546 Jan 26 09:35 /qa/web/cgi-local/awenv.pl
> 1 root system 489 Feb 3 2005
> /qa/web/cgi-local/Archives/ESS.20050318/Archives/awenv.
> 1 root system 2721 Mar 20 08:39
> /qa/web/cgi-local/Archives/deferredCompensation.2006040
> 1 root system 600 Feb 3 2005
> /development/web/cgi-local/Archives/ESS.20050506/Archi
> 1 313 staff 2587 Mar 27 07:28
> /development/web/cgi-local/awenv.pl
>
> I would like to delete everything up to the first slash
>
> :%s/^.*\/\{-}//g grabs to the last slash. So does:

This pattern matches:
^ from the start of the line
.* any character, zero or more times (greedy)
\/\{-} a slash, zero or more times (non-greedy)

>
> :%s/^.*\/\{-}// (no g)
> :%s/^.*\/\{1}//
> :%s/^.*\/\?//
>
> What am I missing here?

One problem is that you're applying constraints on the number of
times that the slash is matched. You should instead be applying
constraints on what is allowed to match before the first slash.
Using the non-greedy operator \{-}, the first part of your pattern
should be:

^.\{-}

That will fix the problem of grabbing to the last slash instead of
stopping at the first slash.

The other problem is including the slash in the pattern without
including it in the string that's deleted. There are several ways
to do that, but I'm familiar with only a few.

1. Since it's only one character, just include it in the
replacement string.

:%s/^.\{-}\//\//

2. Use \ze to terminate the part of the pattern you want replaced.

:%s/^.\{-}\ze\///

Since you are anchoring the pattern to the start of the line with ^,
the g operator at the end of the substitution won't do anything and
can be eliminated.

HTH,
Gary

Ken Holm

unread,
Apr 20, 2006, 8:42:25 AM4/20/06
to
Excellent. I certainly appreciate the response Gary. Your info not
only works it was informative. I'm giving it a 5 star rating. :)

Ken Holm

unread,
Apr 20, 2006, 8:52:22 AM4/20/06
to
In further reviewing this we can extend this logic... If I wanted to
remove everything up to, but not including the 4th slash I could use:

:%s/^\(.\{-}\ze\/\)\{4}//

That is, wrap the part that grabs everything up to the first slash in
parens and then say I want 4 groups of that.

Excellent!

0 new messages