Editing the string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ramesh54
    New Member
    • Jul 2008
    • 37

    #1

    Editing the string

    Hello All,

    I just have a small query. I am just curious to know if I could have this desired output.

    My input file is r145_AF_FD_z_ HJ.txt

    So finally my output should be

    Code:
    /abc/ FD/ r145_AF_FD_abc_ HJ.txt
    /pqt/ FD/r145_AF_FD_ pqt_HJ.txt
    /sde/ FD/r145_AF_FD_ sde_HJ.txt
    I guess prefix and suffix should be added along with the directory name. Any help or suggestion will be helpful.

    I have tried this so far
    Code:
    #!/usr/bin/perl 
    use strict;
    use File::Path;
    use File::Copy;
    my @dirs = qw[ F:/usr/eg/abc/FD F:/usr/eg/pqt/FD F:/usr/eg/sde/FD];
    for my $dir (@dirs) {
    mkpath($dir) or die $!;
    }
    chomp(my $file = <>);
    
    for my $dir (@dirs) {
    my ($rename) = $dir =~ m!/(\w+)/FD$!;
    copy($file, "$dir/$rename.txt") or die $!;
    }
    Regards,
    Ramesh
    Last edited by numberwhun; Jul 23 '08, 01:24 PM. Reason: Please use code tags
  • nithinpes
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    First, the line
    Code:
    chomp(my $file = <>);
    will open the file and get it's first line assigned to $file. But, I beleive what you want is to get the filename.


    Replace:
    Code:
    chomp(my $file = <>);
    
    for my $dir (@dirs) {
    my ($rename) = $dir =~ m!/(\w+)/FD$!;
    copy($file, "$dir/$rename.txt") or die $!;
    }
    with:

    Code:
    chomp(my $file = $ARGV[0]);
    
    for my $dir (@dirs) {
    my $fix;
    if($dir=~/\/(.+)\/FD$/) {
    $fix= $1;
    }
    my $rename = $file;
    $rename =~ s/z/$fix/;
    copy($file, "$dir/$rename") or warn "$file:$!";
    }
    This should meet your requirement.

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      I'm not sure what he is doing either but this is one way to get the filenames how he wants:

      Code:
      my $file = 'r145_AF_FD_z_HJ.txt'; 
      my @dirs = qw[ F:/usr/eg/abc/FD F:/usr/eg/pqt/FD F:/usr/eg/sde/FD];
      for my $dir (@dirs) {
         my $t = $file;
         my ($var1,$var2) = $dir =~ m!/((\w+)/FD)$!;
         $t =~ s/z/$var2/;
         $t = "$var1$t";
         print $t,"\n";
      }
      output:

      Code:
      abc/FDr145_AF_FD_abc_HJ.txt
      pqt/FDr145_AF_FD_pqt_HJ.txt
      sde/FDr145_AF_FD_sde_HJ.txt

      Comment

      • ramesh54
        New Member
        • Jul 2008
        • 37

        #4
        My requirement was as follows.
        1. I need to create 3 new directories abc, pqt & sde with sub directories FD for these folders.
        Code:
        /abc/FD
        /pqt/FD
        /sde/FD

        2. The program should ask for the input file which is r145_AF_FD_z_ HJ.txt

        3. The file need to be copied to all the FD folders,
        Code:
        /abc/FD/r145_AF_FD_z_ HJ.txt
        /pqt/FD/r145_AF_FD_z_ HJ.txt
        /sde/FD/r145_AF_FD_z_ HJ.txt
        4. I need to replace the file name in the respective folders without altering the prefix (r145_AF_FD_) & suffix(_ HJ.txt) and replace the z with the respective directory name.
        Code:
        /abc/FD/r145_AF_FD_abc_ HJ.txt 
        /abc/FD/r145_AF_FD_pqt_ HJ.txt 
        /abc/FD/r145_AF_FD_sde_ HJ.txt
        I tried the below code, but it doesnot give the desired output,

        Code:
        #!/usr/bin/perl 
        use strict;
        use File::Path;
        use File::Copy;
        my @dirs = qw[ ../../abc/FD ../../pqt/FD ../../sde/FD];
        chomp(my $file = $ARGV[0]);      
        for my $dir (@dirs) { 
        my $fix;
        if($dir=~/\/(.+)\/FD$/) {   
        $fix= $1;      
        }   
        my $rename = $file;      
        $rename =~ s/z/$fix/;         
        copy($file, "$dir/$rename") or warn "$file:$!";   
        }
        Last edited by eWish; Jul 24 '08, 03:46 AM. Reason: Please use code tags for code and data as well

        Comment

        • nithinpes
          Recognized Expert Contributor
          • Dec 2007
          • 410

          #5
          What is the output/error that you are getting?
          Also, there is a white space between _ and HJ in the file r145_AF_FD_z_ HJ.txt. If that is how it is, you need to give the input (file name) within quotes-
          "r145_AF_FD _z_ HJ.txt"
          If that does not work, try to print $rename and post the output or error message that you are getting.
          -Nithin

          Comment

          • ramesh54
            New Member
            • Jul 2008
            • 37

            #6
            Sorry. There is no white space in the input file. It was a typing error.
            input file is

            r145_AF_FD_z_HJ .txt

            the error I am getting is illegal division by zero at line 14.

            I had given the input file without quotes.

            Comment

            • nithinpes
              Recognized Expert Contributor
              • Dec 2007
              • 410

              #7
              Originally posted by ramesh54
              Sorry. There is no white space in the input file. It was a typing error.
              input file is

              r145_AF_FD_z_HJ .txt

              the error I am getting is illegal division by zero at line 14.

              I had given the input file without quotes.
              Well, if you don't have spaces within the argument then no need go give input within quotes.
              The error message is strange. I tried the same code in my Windows machine and it works fine.
              Did you try to print out $rename and check what value it is getting?

              Comment

              • ramesh54
                New Member
                • Jul 2008
                • 37

                #8
                I have printed out $rename, The first few lines are the rror i am getting and last last line refers to the &rename.The code used is as follows,

                #!/usr/bin/perl
                use strict;
                use File::Path;
                use File::Copy;
                my @dirs = qw[ ../../abc/FD ../../pqt/FD ../../sde/FD];
                for my $dir (@dirs) {
                mkpath($dir) or die $!;
                }
                chomp(my $file = $ARGV[0]);
                for my $dir (@dirs) {
                my $fix;
                if($dir=~/\/(.+)\/FD$/) {
                $fix= $1;
                }
                my $rename = $file;
                $rename =~ s/z/$fix/;
                print $rename;
                copy($file, "$dir/$rename") or warn "$file:$!";
                }

                r145_AF_FD_z_HJ .txt:No such file or directory at sample11.pl line 18.
                r145_AF_FD_z_HJ .txt:No such file or directory at sample11.pl line 18.
                r145_AF_FD_z_HJ .txt:No such file or directory at sample11.pl line 18.
                r145_AF_FD_../abc_HJ.txtr145_ AF_FD_../pqt_HJ.txtr145_ AF_FD_../sde_HJ.txt

                Comment

                • KevinADC
                  Recognized Expert Specialist
                  • Jan 2007
                  • 4092

                  #9
                  you will have to be in the directory where $file is for it to work like you have it. Otherwise use the full directory path to $file instead of just the filename. I showed you how to convert those filename in my other post. I leave it up to you how to incorporate my suggestion into your working code.

                  Comment

                  Working...