regex - I want specific word to be stored in a CSV file -


i need specific word in file name should pulled out file names among directory

!/usr/bin/perl -w  $directory = "/home/grds/datafiles"; opendir(dir, $directory) or die "couldn't open $directory: $!\n"; @files = grep("exp", readdir(dir)); closedir(dir);  foreach $file (@files) {   # print "$file\n";         open ($file }    

example file name :

expresult_3d0r0000002345_test345_cache1_ind0000asd123_2014_04_12_18_56_1 

i need

3d0r0000002345, test345, cache1, ind0000asd123, 2014_04_12 

should stored in excel file separate columns.

this straightforward if want csv output

this program checks each directory item file (not directory) , name contains exp, , splits on underscores _ maximum of 6 fields. leaves trailing date-time intact single field

then first field removed, time stripped last field, , remaining fields printed, joined commas ,

i have used autodie removes need check success of opendir

use strict; use warnings; use 5.010; use autodie;  use file::spec::functions 'rel2abs';  use constant directory => '/home/grds/datafiles';  opendir $dh, directory;  while ( $node = readdir $dh ) {    $fullpath = rel2abs($node, directory);   next unless -f $fullpath , $node =~ /exp/;    @fields = split /_/, $node, 6;   next unless @fields == 6;    shift @fields;   $fields[-1] =~ s/\d+_\d+_\d+\k.*//;    print join(',', @fields), "\n"; } 

output

3d0r0000002345,test345,cache1,ind0000asd123,2014_04_12 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -