Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Welcome to the Monastery
 
PerlMonks  

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

by Kiran Kumar K V N (Initiate)
on Dec 28, 2006 at 12:24 UTC ( [id://592084]=perlquestion: 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.

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

Hi All,

I am a real real beginner of Pearl.

I am trying to write a Perl Script to extract all the filenames with the date <FileName>.20041204****** in the given list of Filenames as below and place BSC, SBSCSubsystem and MCBTS filenames in separate files:

-rw-rw-rw- 1 bsmbin bsmbin 0 Dec 3 00:31 arun2-2004112712 +0100 -rw-rw-rw- 1 bsmbin bsmbin 0 Dec 3 00:31 abc2-20041127130 +812 -rw-rw-rw- 1 bsmbin bsmbin 0 Dec 3 00:31 abc1-20041127131 +046 -rw-rw-rw- 1 bsmbin bsmbin 10065 Dec 3 02:06 SBSCSubsystem-20 +041202143000 -rw-rw-rw- 1 bsmbin bsmbin 10065 Dec 3 02:06 SBSCSubsystem-20 +041202140000 -rw-rw-rw- 1 bsmbin bsmbin 10065 Dec 3 02:06 SBSCSubsystem-20 +041202133000 -rw-rw-rw- 1 bsmbin bsmbin 7018 Dec 3 02:06 MCBTSSubsystem-2 +0041202143000 -rw-rw-rw- 1 bsmbin bsmbin 7019 Dec 3 02:06 MCBTSSubsystem-2 +0041202140000 -rw-rw-rw- 1 bsmbin bsmbin 7020 Dec 3 02:06 MCBTSSubsystem-2 +0041202133000 -rw-rw-rw- 1 bsmbin bsmbin 303 Dec 3 02:06 BSC-200412021430 +00 -rw-rw-rw- 1 bsmbin bsmbin 303 Dec 3 02:06 BSC-200412021400 +00 -rw-rw-rw- 1 bsmbin bsmbin 303 Dec 3 02:06 BSC-200412021330 +00 -rw-rw-rw- 1 bsmbin bsmbin 10065 Dec 3 03:00 SBSCSubsystem-20 +041202150000 -rw-rw-rw- 1 bsmbin bsmbin 7020 Dec 3 03:00 MCBTSSubsystem-2 +0041202150000 -rw-rw-rw- 1 bsmbin bsmbin 303 Dec 3 03:00 BSC-200412021500 +00 -rw-rw-rw- 1 bsmbin bsmbin 10065 Dec 3 04:00 SBSCSubsystem-20 +041202160000 -rw-rw-rw- 1 bsmbin bsmbin 10065 Dec 3 04:00 SBSCSubsystem-20 +041202153000

The Algorithm I thought of is like this :-

1) Create 3 separate Files with names BSC, SBSCSubsystem and MCBTSSubsystem using File I/O.

2) First, check in the given list of filenames starting from the top, files with name BSC-20041202******. For each filename found/sorted out in this Format, move the corresponding filename to the File BSC.Continue this process, until you reach the end of the List.

3) Second, check in the given list of filenames starting from the top, files with name SBSCSubsystem- 20041202******. For each filename found/sorted out in this Format, move the corresponding filename to the File SBSCSubsystem. Continue this process, until you reach the end of the List.

4) Third, check in the given list of filenames starting from the top, files with name MCBTSSubsystem- 20041202******. For each filename found/sorted out in this Format, move the corresponding filename to the File MCBTSSubsystem. Continue this process, until you reach the end of the List.

In this way, all the respective filenames are transferred to the corresponding Files. Think that Regular Expressions might be required to implement the Logic of File Sorting.

Can anyone help me with a Perl Script for the same.

Thanks,
Kiran Kumar K.

Replies are listed 'Best First'.
Re: Sorting names using Regular Expressions and placing them in different Files.
by marto (Cardinal) on Dec 28, 2006 at 12:39 UTC
Re: Sorting names using Regular Expressions and placing them in different Files.
by jZed (Prior) on Dec 28, 2006 at 12: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.

      Hi jZed,

      I assumed the following Code but it seems vague and there is some real problem with it. I am not able to implement Regular Expressions Logic. Other things are also messed up.

      As such, can you please explain the Logic you mentioned in your Algo by giving your own Actual Perl Script for the Scenario.

      #!/usr/bin/perl -w #List.plx $file_name="/home/Wireless/kkiran/PERL/Files"; $files="file_name1, file_name2, file_name3"; $file_name1="BSC"; $file_name2="SBSCSubsystem"; $file_name3="MCBTS"; $dir_name = "/home/Wireless/kkiran/PERL"; @files=`ls -prt $file_name |grep -v / `; # Extract all files with the date file_name1.20041204********* and pla +ce in the file named BSC. $count=0, max_count; foreach $eachline(@file_name1.20041204*********) { chomp($eachline); print"\n$eachline"; } for (count=1; count=max_count;count++) { print "BSC.20041204*********\n : $file_name1.20041204*********\n"; } if($count > max_count) { return(0); } exit; # Extract all files with the date file_name2.20041204********* and pla +ce in the file named SBSCSubsystem. $count=0, max_count; foreach $eachline(@file_name2.20041204*********) { chomp($eachline); print"\n$eachline"; } for (count=1; count=max_count;count++) { print "SBSCSubsystem.20041204*********\n : $file_name2.20041204******* +**\n"; } if($count > max_count) { return(0); } exit; # Extract all files with the date file_name3.20041204********* and pla +ce in the file named MCBTS. $count=0, max_count; foreach $eachline(@file_name3.20041204*********) { chomp($eachline); print"\n$eachline"; } for (count=1; count=max_count;count++) { print "MCBTS.20041204*********\n : $file_name3.20041204*********\n"; } if($count > max_count) { return(0); } exit;
        Please follow marto's advice and reformat both of your postings using code tags so that they are legible.
        wow man $count=0, max_count; this is deep, and you should probably a) listen to marto and jZed, and b) start Reading the Friendly Manuals like perlfaq.
Re: Sorting names using Regular Expressions and placing them in different Files.
by alpha (Scribe) on Dec 28, 2006 at 12: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 alpha (Scribe) on Dec 28, 2006 at 14: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; }
      Hi Alpha,

      The Script is just creating the 3 Files BSC, MCBTSSubsystem, SBSCSubsystem. It is not able to move all the filenames in the list to these newly created files. Also, Files don't have proper access permissions after creation.

      Request you to please revisit the Problem Scenario I posted once again and if you could please propose a proper complete Perl Solution for the same.

      Thanks a lot in advance.

      Best Regards,

      Kiran

        OK you have my code an you're free to modify it any way you want. I've tested it, and it works, just has to be placed in the same dir as your files are. Good Luck.
Re: Sorting names using Regular Expressions and placing them in different Files.
by ysth (Canon) on Dec 28, 2006 at 16: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/$_"); }

        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 $!;
Re: Sorting names using Regular Expressions and placing them in different Files.
by mr_mischief (Monsignor) on Jan 03, 2007 at 12: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
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.