cabrao
September 15, 2010, 5:58am
1
Hello all,
From the following input :
/path/to/file.txt
/path/to/file2.txt
/another/path/to/another/f.txt
/x/y/z/FILE.txt
/x/y/z/FILE2.txt
can someone help me having the following output with a awk command ?
/path/to/ file.txt file2.txt /another/path/to/another/ f.txt /x/y/z/ FILE.txt FILE2.txt
Thanks for your help.
Klashxx
September 15, 2010, 6:13am
2
One of the many ways:
awk '{a=match($0,/\/[^\/]+$/);
first=substr($0,1,a)
if ( first != last )
printf("%s ",first)
last=first
printf("%s ",substr($0,a+1))}END{print}' inputfile
1 Like
rdcwayx
September 15, 2010, 7:08am
3
awk '
BEGIN{FS=OFS="/"}
{t=$NF;NF--;a[$0]=a[$0]" " t}
END {for (i in a) printf i " " a" "}
' infile
cabrao
September 15, 2010, 7:21am
4
Great, thanks for your help guys
while(<DATA>){
my ($dir,$file)=$_=~/(.*\/)(.*)/;
$hash{$dir}->{SEQ}=$.;
$hash{$dir}->{VAL}=$hash{$dir}->{VAL}." ".$file;
}
foreach my $key (sort {$hash{$a}->{SEQ}<=>$hash{$b}->{SEQ}} keys %hash){
print $key,$hash{$key}->{VAL}," ";
}
__DATA__
/path/to/file.txt
/path/to/file2.txt
/another/path/to/another/f.txt
/x/y/z/FILE.txt
/x/y/z/FILE2.txt