in reply to How do i take the first 4 characters of any file name in a specified directory?

As the number of columns provided in ls -l are constant, how about splitting out each element of the column in to a variable and then matching the first four characters of the filename?
@namearr = `ls -l`; foreach $nameline (@namearr) { chomp $nameline; print ("'$nameline'\n"); ($var1, $var2, $var3, $var4, $var5, $var6, $var7, $var8, $name) = ( +split " ", $nameline); $name =~ s/^(\w{4}).+/$1/; print ("NAME '$name'\n"); }
The regex part matches the first four characters and substitutes the match in to the $name variable
  • Comment on Re: How do i take the first 4 characters of any file name in a specified directory?
  • Download Code