extract string from filename

Kevin D. Clark kevin_d_clark at comcast.net
Thu Jan 12 20:31:01 EST 2006


Zhao Peng writes:

> I'm back, with another "extract string" question. //grin


find FOLDERNAME -name \*sas7bdat -print | sed 's/.*\///' | cut -d _ -f 2 | sort -u > somefile.txt

or

perl -MFile::Find -e 'find(sub{$string2 = (split /_/)[2]; $seen{$string2}++; }, @ARGV); map { print "$_\n"; } keys(%seen)' FOLDERNAME

(which looks more readable as:

  perl -MFile::Find -e 'find(sub{ $string2 = (split /_/)[2];
                                  $seen{$string2}++;
                                 }, @ARGV);
  
                         map { print "$_\n"; } keys(%seen)' \
          FOLDERNAME > somefile.txt

)

Either of which solves the problem that you describe.  Actually, they
solve more than the problem that you describe, since it wasn't
apparent to me if you had any subdirectories here, but this is solved too)

(substitute FOLDERNAME with your directory's name)


Honestly, the first solution I present is the way I would have solved
this problem myself.  Very fast this way.

Regards,

--kevin
-- 
(There are also also 228 babies named Unique during the 1990s alone,
and 1 each of Uneek, Uneque, and Uneqqee.)

    -- _Freakonomics_, Steven D. Levitt and Stephen J. Dubner


[but no Unix folks named their kids "uniq", apparently.  --kevin]




More information about the gnhlug-discuss mailing list