awk question

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.

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
awk '
BEGIN{FS=OFS="/"}
{t=$NF;NF--;a[$0]=a[$0]" " t} 
END {for (i in a) printf i " " a" "}
' infile

Great, thanks for your help guys :wink:

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