Vim Tips Wiki
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
FANDOM
Fan Central
BETA
Games
Anime
Movies
TV
Video
Wikis
Explore Wikis
Community Central
Start a Wiki
Don't have an account?
Register
Sign In
Sign In
Register
Vim Tips Wiki
1,649
pages
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
Editing
Remove up to the nth slash to clean file lists
Back to page
Edit
Edit source
View history
Talk (0)
Edit Page
Remove up to the nth slash to clean file lists
We recommend that you
log in
before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your
Watchlist
.
Creating an account
is quick and free.
The edit appears to have already been undone.
Anti-spam check. Do
not
fill this in!
{{review}} {{TipImported |id=1206 |previous=1204 |next=1213 |created=2006 |complexity=intermediate |author=Ken Holm |version=5.7 |rating=2/5 |category1= |category2= }} Lets say you find a list of files: <pre> :r !find / -name blahblah -ls </pre> and get the following: <pre> 3139934 4 -rw-r--r-- 1 dude dude 600 Feb 3 13:25 /var/www/lawwebdev.blahblah.com/cgi-bin/Archives/ESS.2005050 3010144 4 -rw-r--r-- 1 mang mang 1848 Mar 31 09:35 /var/www/lawwebdev.blahblah.com/cgi-bin/blahblah.pl 1964295 4 -rw-r--r-- 1 apache apache 597 Mar 8 11:48 /var/www/lawweb.blahblah.com/cgi-bin/blahblah.pl 2339286 4 -rw-r--r-- 1 mang mang 1851 Mar 31 09:29 /var/www/lawweb.blahblah.com/cgi-bin/blahblah.pl 3010157 4 -rw-r--r-- 1 mang mang 1940 Mar 31 10:44 /var/www/lawwebtest.blahblah.com/cgi-bin/blahblah.pl 294691 4 -rw-r--r-- 1 mang mang 1848 Apr 10 16:02 /tmp/PMT/blahblah.pl 1880561 4 -rw-r--r-- 1 mang mang 2432 Feb 22 13:49 /usr/local/scripts/prod/blahblah.pl 1880565 4 -rw-r--r-- 1 mang mang 1700 Mar 30 14:44 /usr/local/scripts/dev/blahblah.pl 899894 4 -rw-r--r-- 1 mang mang 1848 Mar 31 09:48 /usr/local/scripts/dev/Gold/base/blahblah.pl 1880626 4 -rw-r--r-- 1 mang mang 1699 Mar 30 14:46 /usr/local/scripts/test/blahblah.pl </pre> Now, you'd like to just have a list of files without the rest of the info. Issue the global replace as follows: <pre> :%s/^.\{-}\ze\/// </pre> That leaves you with a nice list: <pre> /var/www/lawwebdev.blahblah.com/cgi-bin/Archives/ESS.20050506/Archives/blahblah.pl /var/www/lawwebdev.blahblah.com/cgi-bin/blahblah.pl /var/www/lawweb.blahblah.com/cgi-bin/blahblah.pl /var/www/lawweb.blahblah.com/cgi-bin/blahblah.pl /var/www/lawwebtest.blahblah.com/cgi-bin/blahblah.pl /tmp/PMT/blahblah.pl /usr/local/scripts/prod/blahblah.pl /usr/local/scripts/dev/blahblah.pl /usr/local/scripts/dev/Gold/base/blahblah.pl /usr/local/scripts/test/blahblah.pl </pre> If you want two levels deep you can wrap the atom being grabbed in escaped parens and quantify it with braces: <pre> :%s/^\(.\{-}\ze\/\)\{3}// </pre> Note: If you want two levels deep, you need to grab three atom's worth of stuff, all the junk to the left of the first slash, then the first two levels of dirs. ==Comments== Why not use: <pre> :r !find /path/to/search -name pattern_to_find </pre> and skip the '-ls' parameter completely? That will do away with the extraneous listing fields in the first place. Or am I missing something really subtle? ---- The really subtle part was how to delete all the stuff before the first slash. One could ask "Why not use <code>:r !locate filename</code>?" That would have given us the data we needed with no manipulation necessary. However that is beyond the scope of this tip. ---- Why not the shorter: <pre> :%s:[^/]*:: </pre> ----
Summary:
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Navigation
(
view source
)
Template:Review
(
view source
)
Template:TipImported
(
view source
)
Follow on IG
TikTok
Join Fan Lab