http://www.perlmonks.org?node_id=930294

oolmos has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks. I’m running into a fairly simple problem but I have not been able to solve it. I have decided to look for some help. I’m not very good scripting and I was hoping I would find a Samaritan on this website. I have files in the following format: /test1/da/0/1/nnn.dat /test1/da/0/2/nnn.dat /test1/da/0/3/nnn.dat … /test1/da/y/n/nnn.dat /test1/da/y+1/n+1/nnn.dat /test1/da/y+1/n+2/nnn.dat … /test1/da/y+n/n+n/nnn.dat I want to copy all nnn.dat files that are located in every subfolder into a /newfolder/nnn.dat in such a way that each nnn.dat gets renamed according to its location for example: /newfolder/0_1_nnn.dat /newfolder/0_2_nnn.dat /newfolder/0_3_nnn.dat … /newfolder/y_n_nnn.dat Also, I want to replace an specific line number of the nnn.dat file with an specific set of characters. Please HELP!

Replies are listed 'Best First'.
Re: copy and rename depending on location
by Khen1950fx (Canon) on Oct 08, 2011 at 15:27 UTC
    I'll be your Good Samaritan. First, let's cleanup your post.

    Dear Monks:

    I'm running into a fairly simple problem, but I have not been able to solve it. I have decided to look for some help. I'm not very good at scripting, and I was hoping that I would find a Good Samaritan on this website.

    I have files in the following format:

    /test1/da/0/1/nnn.dat /test1/da/0/2/nnn.dat /test1/da/0/3/nnn.dat /test1/da/y/n/nnn.dat /test1/da/y+1/n+1/nnn.dat /test1/da/y+1/n+2/nnn.dat /test1/da/y+n/n+n/nnn.dat

    I want to copy all nnn.dat files that are located in every subfolder into a /newfolder/nnn.dat in such a way that each nnn.dat gets renamed according to its location.

    For example:
    /newfolder/0_1_nnn.dat /newfolder/0_2_nnn.dat /newfolder/0_3_nnn.dat /newfolder/y_n_nnn.dat

    I don't know where to start. What would be a good starting point? Thank you for your help.

    ===============================================================

    How did you do that?

    For "Dear Monks:", I wrapped the expression in paragraph and italic tags:
    <p><i>Dear Monks</i></p>
    For each paragraph, use tags like this:
    <p>I'm running into a fairly simple problem but I have not been able t +o solve it. I have decided to look for some help. I'm not very good a +t scripting , and I was hoping I would find a Good Samaritan on this +website.</p>
    For your list of files, I wrapped the paths with code tags.
    <code> /test1/da/0/1/nnn.dat /test1/da/0/2/nnn.dat /test1/da/0/3/nnn.dat /test1/da/y/n/nnn.dat /test1/da/y+1/n+1/nnn.dat /test1/da/y+1/n+2/nnn.dat /test1/da/y+n/n+n/nnn.dat </code>
    Looking good... Now what? You'll need to follow Grandfather's advice and try to put a script together. You can start by studying the documentation for:

    glob
    rename
    File::Copy
    File::GlobMapper

    Also, read and reread Writeup Formatting Tips

    Now it's up to you. Good luck and keep trying!

      Thank you Grandfather! I understand the rules now. First time on this website as you can tell. I will study the documentation you shared.

Re: copy and rename depending on location
by GrandFather (Saint) on Oct 08, 2011 at 08:58 UTC

    Help us help you by formatting your node so we can read it and show us what you have tried already. Laziness is considered a virtue in Perl circles, but by avoiding redoing work rather than being too lazy to do it yourself.

    True laziness is hard work

      Laziness or ignorance (?) Just learning, not a programer and not at school. I do appreciate help from a lot of you.

        Definitely laziness. You didn't bother reading any of the information that would have helped you format your node. Ignorance is fine and we are generally completely happy to help dispel it, so long as supplicants are prepared to go and do the suggested reading or exercises and especially when we can see they've put a little effort into asking their question in the first place.

        Bowling up with a "I have a problem and if you work really hard you may figure out with it is. Fix it for me." is less likely to inspire great effort to assist!

        True laziness is hard work
Re: copy and rename depending on location
by Marshall (Canon) on Oct 08, 2011 at 12:16 UTC
    Sounds like you want to descend through various directory trees, looking for files with names like nnn.dat and then do something with them.

    The File::Find module is designed to facilitate this. One quick search found: Beginners guide to File::Find. Take a look, try some code.

    When writing code use the <code>...</code> tags, otherwise it is really hard to understand your path names.

Re: copy and rename depending on location
by choroba (Cardinal) on Oct 08, 2011 at 17:44 UTC
    For such a simple task, I'd probably use just bash:
    cd /test1/da/ for file in */*/*.dat ; do mv $file /newfolder/${file////_} done
Re: copy and rename depending on location
by Anonymous Monk on Oct 08, 2011 at 09:56 UTC
Re: copy and rename depending on location
by pvaldes (Chaplain) on Oct 08, 2011 at 10:10 UTC
    use File::Spec::Functions; # i.e. catfile