basename & dirname in Perl
sub basename($) { my $file = shift; $file =~ s!^(?:.*/)?(.+?)(?:\.[^.]*)?$!$1!; return $file; } sub dirname($) {my $file = shift; $file =~ s!/?[^/]*/*$!!; return $file; }
DZone Snippets > Minimiscience > unix
12114 users tagging and storing useful source code snippets
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
sub basename($) { my $file = shift; $file =~ s!^(?:.*/)?(.+?)(?:\.[^.]*)?$!$1!; return $file; } sub dirname($) {my $file = shift; $file =~ s!/?[^/]*/*$!!; return $file; }
grep killed /usr/games/lib/nethackdir/logfile | awk -F killed '{print"killed"$NF}' | sort | uniq -c | sort -r
/* Put this line at the top of the file: */ #include <sys/time.h> /* Put this right before the code you want to time: */ struct timeval timer_start, timer_end; gettimeofday(&timer_start, NULL); /* Put this right after the code you want to time: */ gettimeofday(&timer_end, NULL); double timer_spent = timer_end.tv_sec - timer_start.tv_sec + (timer_end.tv_usec - timer_start.tv_usec) / 1000000.0; printf("Time spent: %.6f\n", timer_spent);