Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Regular expression parse pathname

by unixguy (Acolyte)
on Jan 29, 2010 at 00:41 UTC ( [id://820274]=perlquestion: print w/replies, xml ) Need Help??

unixguy has asked for the wisdom of the Perl Monks concerning the following question:

I want to print unix path name like tree.

e.g path name: /home/code/work I want to print like this.

/home /home/code /home/code/work

I tried this one line, didn't print what I want
echo "/home/code/work"|perl -lane 'while(m#([^\/]+)#g) {print "$1";}'

home code work

Can some one help me on this? Thanks

Replies are listed 'Best First'.
Re: Regular expression parse pathname
by toolic (Bishop) on Jan 29, 2010 at 01:13 UTC
    The key is to accumulate your string as you loop through the directories. Not a one-liner, but this split solution gets the job done...
    use strict; use warnings; my $s = '/home/code/work'; my @ds = split m{/}, $s; shift @ds; my $accum; for (@ds) { $accum .= "/$_"; print "$accum "; } print "\n";

    Update: or, adapting your regex attempt:

    my $s = '/home/code/work'; my $acc; while ($s =~ m#([^/]+)#g) { $acc .= "/$1"; print "$acc " }
Re: Regular expression parse pathname
by jwkrahn (Abbot) on Jan 29, 2010 at 02:49 UTC
    $ perl -le' use File::Basename; my $name = "/home/code/work"; print join " ", reverse map { my $d = dirname my $x = $name; $name = $ +d; $x } 1 .. $name =~ y!/!!; ' /home /home/code /home/code/work
Re: Regular expression parse pathname
by rubasov (Friar) on Jan 29, 2010 at 02:38 UTC
    Try this:
    echo "/home/code/work" |\ perl -nle 'chomp; print $p .= $& while m{ \G (?: [^/]* /+ | [^/]+ \z ) + }xg'
    The m/\G.../g match can be called several times, and each time it will start at the position where last time it left off.

    However be aware, that this one-liner produces a little different tree than your example, but probably you want to represent the real file system tree and this result is a better representation of it.

Re: Regular expression parse pathname
by aquarium (Curate) on Jan 29, 2010 at 02:47 UTC
    Just a little comment about the original regex. There's no need to escape special characters inside a character class match = character matches inside square brackets. Even no need to escape a forward slash inside square brackets, as regex engines know they're inside a character class and won't confuse with a regex delimiter (normally forward slash.) So if the regex was originally intended for unix only paths, no need for the backslash..unless you also want to catch dos style path delimiter.
    the hardest line to type correctly is: stty erase ^H
Re: Regular expression parse pathname
by JavaFan (Canon) on Jan 29, 2010 at 10:47 UTC
    use 5.010; my $_ = "/home/code/work"; my @paths; again: unshift @paths, $_; s!/[^/]*$!! and length $_ and goto again; say "@paths"; __END__ /home /home/code /home/code/work
Re: Regular expression parse pathname
by shawnhcorey (Friar) on Jan 29, 2010 at 14:42 UTC

    You should `use File::Spec;` when dealing with paths. File::Spec is a standard module and should have been installed with Perl. For a complete list of all the standard modules and pragmatics, see `perldoc perlmodlib`.

Re: Regular expression parse pathname
by hbm (Hermit) on Jan 29, 2010 at 19:46 UTC

    This is as tight as I can currently squeeze it:

    echo "/home/code/work"|\ perl -pe's|(/[^/]+)|$`$1 |g'

    Update: My previous, longer solutions:

    perl -pe's|(/[^/]+)|$`$1$"|g' ### $" - DUH! perl -l40p057e'$_=$;.$_;$;="$_/"' perl -ne'print$1,$"while m|($&/[^/]+)|' perl -e'$_=<>;print$1,$"while m|($&/[^/]+)|' perl -e'@_=<>=~m|(/[^/]+)|g;print@_[0..$_],$"for(0..$#_)'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://820274]
Approved by ww
Front-paged by tye
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-19 20:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found