Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Sorting names using Regular Expressions and placing them in different Files.

by Kiran Kumar K V N (Initiate)
on Dec 28, 2006 at 17:24 UTC ( [id://592084]=perlquestion: print w/replies, xml ) Need Help??

Kiran Kumar K V N has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Sorting names using Regular Expressions and placing them in different Files.
by jZed (Prior) on Dec 28, 2006 at 17:40 UTC
    1. open the input file, checking for file-open error
    2. open the output files, checking for file-open errors
    3. for each line in the input file
    4. use a regex or split to see if it has the date you want
    5. write it to the appropriate output when appropriate
    6. close the files
    Please try to do that yourself and if you have problems, feel free to come back and tell us which steps are giving you problems.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Sorting names using Regular Expressions and placing them in different Files.
by ysth (Canon) on Dec 28, 2006 at 21:42 UTC
    It sounds to me like you don't want files "BSC", "SBSCSubsystem", and "MCBTSSubsystem", but rather directories, and to have the 20041202 files prefixed with those moved into the directories? If so, try doing one category at a time:
    for my $category ("BSC", "SBSCSubsystem", "MCBTSSubsystem") { }
    Inside the loop you'll need to:
    1. create the directory if it doesn't exist, like:
      use Errno "EEXIST"; if (! mkdir("foo") && $! != EEXIST) { die "unable to create directory +foo: $!" }
    2. find any matching files and put them in @files. I wouldn't use a regular expression; I'd just use glob.
    3. move @files into your directory. No File I/O required, rename($file, "dirname/$file") should work to move $file from the current directory into directory dirname.

    Update: added use Errno

      I think you need to move the files that start with BSC, SBSCSubsytem and MCBTSSubSystem into separate directories.

      1. Create the separate directories for BSD, SBSCSubsystem and MCBTSSubsytem under the directory you want to search or at nay other path
      chdir("test") ## For eg:test can be directory where the files exist. unless (-d "BSD"){ mkdir("BSD") or warn ("unable to create directory BSD"); } unless (-d "SBSCSubsystem"){ mkdir("SBSCSubsystem") or warn ("unable to create directory SBSCSu +bsystem"); } unless (-d "MCBTSSubsytem"){ mkdir("MCBTSSubsytem") or warn ("unable to create directory MCBTSS +ubsytem"); }
      Get the lsit of files that matches the BSD, MCBTSSubsytem etc into separate arrays.
      opendir(DIR, "test") @files_BSD = grep(/^BSD-\d{14}/, readdir(DIR)); @files_SBSC = grep(/^SBSCSubsystem-\d{14}/, readdir(DIR)); @files_MCBTS = grep(/^MCBTSSubsytem-\d{14}/, readdir(DIR));
      Then move the files into respective directories.
      foreach (@files_BSD){ rename($_, "BSD/$_" ); } foreach (@files_SBSC){ rename($_, "SBSCSubsystem/$_"); } foreach (@files_MCBTS){ rename($_, "MCBTSSubsytem/$_"); }
      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Sorting names using Regular Expressions and placing them in different Files.
by alpha (Scribe) on Dec 28, 2006 at 19:55 UTC
    Here you go :)
    #!/usr/bin/perl -w use strict; my $prefix = '-20041202'; my @types = qw(BSC SBSCSubsystem MCBTSSubsystem); eval 'open ('.$_.',">'.$_.'") or die($!)' for @types; die $@ if $@; while(<*>) { my $t_fname = $_; next if ($t_fname eq '.' or $t_fname eq '..'); $t_fname =~ /^$_$prefix/ and eval('print ('.$_.' $t_fname.chr 10)' +) for @types; }
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Sorting names using Regular Expressions and placing them in different Files.
by marto (Cardinal) on Dec 28, 2006 at 17:39 UTC
Re: Sorting names using Regular Expressions and placing them in different Files.
by alpha (Scribe) on Dec 28, 2006 at 17:51 UTC
    The question itself is very hard to understand. I am trying to write a Perl Script to extract all the filenames with the date <FileName>.20041204****** - does it have something to do with stat() ?
Re: Sorting names using Regular Expressions and placing them in different Files.
by mr_mischief (Monsignor) on Jan 03, 2007 at 17:31 UTC
    Does this:
    #!/usr/bin/perl use strict; use warnings; my %files = ( 'BSC' => 1, 'SBSCSubsystem' => 1, 'MCBTSSubsystem' => 1 +); no strict 'refs'; ( open ( $$_, '>', $_ ) or die "$_: $!\n") for ( keys %files ); my ( $dest, $date ); while ( <> ) { ( $dest, $date ) = split /-/, ((split /\s+/)[8]); print "dest: $dest\n"; ( print ${$dest} ( $_ ) ) if exists $files{$dest}; }
    do what you need? If not, please try to clearly state what part of your spec it does not meet and what needs to be changed.

    Note: the preceeding code takes the input on STDIN or as a file named as the first argument.


    Christopher E. Stith

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://592084]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (8)
As of 2024-03-28 11:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found