Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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

by ysth (Canon)
on Dec 28, 2006 at 16:42 UTC ( [id://592139]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


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

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

Replies are listed 'Best First'.
Re^2:Sorting names using Regular Expressions and placing them in different Files.
by santhi (Monk) on Dec 28, 2006 at 23:58 UTC
    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/$_"); }

      The following updated Code extracts all the

      filenames with the data <FileName>-**************

      (Filename being ‘BSC’,‘SBSCSubsystem’,

      ‘MCBTSSubsystem’),in the given list of Filenames

      in the Input File ‘bsm1_LogFiles’and place

      corresponding appropriate Filenames in the Output

      Files created viz. ‘BSC’, ‘SBSCSubsystem’ and

      ‘MCBTS’, respectively. The below updated Script

      gives the appropriate Output when the command

      “perl –w Test.pl” is executed from the Root :-

       #!/usr/bin/perl -w  use strict;

      ## Open I/P File "bsm1_LogFiles" for reading.

          open(INPUT,'<','bsm1_LogFiles') or die $!;

      ## Read from the Input File.

          my $line  = <INPUT>;

      ## Open the O/P File "BSC" for writing.

          open(BSC,'>','BSC') or die $!;

      ## Open the O/P File "SBSCSubsystem" for writing.

        open(SBSCSubsystem,'>','SBSCSubsystem') or die $!;

      ## Open the O/P File "MCBTSSubsystem" for writing.

      open(MCBTSSubsystem,'>','MCBTSSubsystem')or die $!;

      ## Search each Line in the Input File.

            for my $searchline(<INPUT>) {

      ## Search for parameter 'BSC-'.

                 if( $searchline =~ /BSC-/) {

      ## Write to the Output File 'BSC'.

                      my $line  = 'BSC';                 print BSC $searchline;                                       }

      ## Search for parameter 'SBSCSubsystem-'.

                 elsif( $searchline =~ /SBSCSubsystem-/) {

      ## Write to Output File 'SBSCSubsystem'.

                     my $line  = 'SBSCSubsystem';                print SBSCSubsystem $searchline;                                                    }

      ## Search for parameter 'MCBTSSubsystem-'.

                if( $searchline =~ /MCBTSSubsystem-/) {

      ## Write to Output File 'MCBTSSubsystem'

                     my $line  = 'MCBTSSussystem';                 print MCBTSSubsystem $searchline;                                                 }                               }

      ## Close the O/P File "BSC";

          close (BSC) or die $!;

      ## Close the O/P File "SBSCSubsystem";

          close (SBSCSubsystem) or die $!;

      ## Close the O/P File "MCBTSSubsystem";

          close (MCBTSSubsystem) or die $!;

        The following updated Code extracts all the filenames with the data <FileName>-**************(Filename being ‘BSC’,‘SBSCSubsystem’, ‘MCBTSSubsystem’),in the given list of Filenames in the Input File ‘bsm1_LogFiles’and place corresponding appropriate Filenames in the Output Files created viz. ‘BSC’, ‘SBSCSubsystem’ and ‘MCBTS’, respectively. The below updated Script gives the appropriate Output when the command “perl –w Test.pl” is executed from the Root :-

        #!/usr/bin/perl -w use strict; ## Open I/P File "bsm1_LogFiles" for reading. open(INPUT,'<','bsm1_LogFiles') or die $!; ## Read from the Input File. my $line = <INPUT>; ## Open the O/P File "BSC" for writing. open(BSC,'>','BSC') or die $!; ## Open the O/P File "SBSCSubsystem" for writing. open(SBSCSubsystem,'>','SBSCSubsystem') or die $!; ## Open the O/P File "MCBTSSubsystem" for writing. open(MCBTSSubsystem,'>','MCBTSSubsystem')or die $!; ## Search each Line in the Input File. for my $searchline(<INPUT>) { ## Search for parameter 'BSC-'. if( $searchline =~ /BSC-/) { ## Write to the Output File 'BSC'. my $line = 'BSC'; print BSC $searchline; } ## Search for parameter 'SBSCSubsystem-'. elsif( $searchline =~ /SBSCSubsystem-/) { ## Write to Output File 'SBSCSubsystem'. my $line = 'SBSCSubsystem'; print SBSCSubsystem $searchline; } ## Search for parameter 'MCBTSSubsystem-'. if( $searchline =~ /MCBTSSubsystem-/) { ## Write to Output File 'MCBTSSubsystem' my $line = 'MCBTSSussystem'; print MCBTSSubsystem $searchline; } } ## Close the O/P File "BSC"; close (BSC) or die $!; ## Close the O/P File "SBSCSubsystem"; close (SBSCSubsystem) or die $!; ## Close the O/P File "MCBTSSubsystem"; close (MCBTSSubsystem) or die $!;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://592139]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.