Created
October 26, 2014 13:15
-
-
Save paulgregg/f7500dc77c23675bfe7b to your computer and use it in GitHub Desktop.
Extract unique subdirectories from a file, ignoring parent dirs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# [email protected] | |
use Data::Dumper; | |
my $d={}; | |
while(<STDIN>) { | |
chomp; | |
next if(/^$/); | |
@bits = split(/\//, $_); | |
$last = pop(@bits); | |
$dr = $d; | |
for my $bit (@bits) { | |
next if ($bit eq ""); | |
if (exists $dr->{$bit}) { | |
if (ref $dr->{$bit} ne HASH) { | |
#print "deleting $bit\n"; | |
delete $dr->{$bit}; | |
#print "reinit $bit\n"; | |
$dr->{$bit} = {}; | |
} else { | |
#print "ignoring $bit\n"; | |
} | |
} else { | |
#print "init $bit\n"; | |
$dr->{$bit} = {}; | |
} | |
#print "moving ptr\n"; | |
$dr = $dr->{$bit}; | |
} | |
if (!exists $dr->{$last}) { | |
$dr->{$last} = 1; | |
} | |
} | |
print Dumper($d); | |
sub walkhashref { | |
my $arg = shift; | |
my $prefix = shift; | |
my $line; | |
foreach $k (keys %{$arg}) { | |
$line = $prefix . "/$k"; | |
if (ref $arg->{$k} eq HASH) { | |
walkhashref($arg->{$k}, $line); | |
} else { | |
print $line . "\n"; | |
} | |
} | |
} | |
walkhashref($d, ""); |
Output:
$ ./dirunique2.pl < dirs.txt
$VAR1 = {
'a' => {
'b' => {
'c' => 1
}
},
'g' => {
'h' => 1
},
'x' => {
'y' => {
'bar' => 1,
'z' => {
'foo' => 1
}
}
}
};
/a/b/c
/g/h
/x/y/bar
/x/y/z/foo
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample Input:
/a
/a/b/
/a/b/c
/g/
/g/h
/x/y/z/foo
/x/y/bar
/x/y
/a