I have a bunch of data that is stored in sub-directories labeled by date. I have used the Cwd command to get the Current working directory so that I can then print it to the vi file that I am writing with the recovered data from the sub-directories. I am using the Cwd as a prefix to the data strings. Is there a way to print only the current directory name and not the path?
example:
Instead of printing-
/d2/aschwa/archive_data/METAR_data/20120302KDUX 121255Z.........
Is there a way to print only-
20120302KDUX 121255Z.........
Here's the code I'm using-
use strict;
use warnings;
use file::find;
use Cwd;
my @folder = ("/d2/aschwa/archive_project/METAR_data/");
open( OUT , '>', 'KDUX_METARS.txt') or die "Could not open $!";
print OUT "Station, Day/Time, Obs Type, Wind/Gust, Vis, Sky, T/Td, Alt, Rmk\n";
print STDOUT "Finding METAR files\n";
my $criteria = sub {if(-e && /^2012/) {
open(my $file,$_) or die "Could not open $_ $!\n";
my $dir = getcwd;
while(<$file>) {
print OUT $dir,$_ if /KDUX ....55Z|KDUX ....05Z/;
}
}
};
find($criteria, @folder);
close OUT;
In Perl, you can use functions basename
or fileparse
to extract the file name from a path.
They are included in the core module File::Basename.