Results 1 to 4 of 4

Thread: Best Practice for Iterating Over Two Files at the Same Time?

  1. #1
    Join Date
    Apr 2011
    Location
    Maryland
    Beans
    1,461
    Distro
    Kubuntu 12.04 Precise Pangolin

    Question Best Practice for Iterating Over Two Files at the Same Time?

    Tonight I was working on a problem where I have two similar files named something like 'file_n_something_x.xls' and 'file_n_something_x.vcf'. I was trying to extract a subset of the data from each and compare it to find out if it was unique to one file or the other. Because the data is similar, but different, and I had a few other things that I needed to do, I wrote a quickie Perl script to compare the two. The issue is, though, that I have around 100 of these to compare side by side, and I don't know the best way to iterate over the pairs of files, passing them into the Perl script. I ended up loading all of the xls files in to one array and then the vcf file into another:

    Code:
    $ declare -a vcf=(*.vcf)
    $ declare -a xls=(*.vcf)
    Then I just iterated over the two arrays, passing them into the script:
    Code:
    $ for (( i=0; i<${#xls[@]}; i++ )); do perl vcf_comparison.pl ${xls[i]} ${vcf[i]}; done
    I figured that was fairly safe as the arrays should have been loaded in asciibetical order and I know all the files are indeed in pairs, and it did seem to work. But, this doesn't seem like the right way to do this, and I'm wondering what the right way is. I probably could have read all the files into an array in the Perl script and then more explicitly compared them before processing. Maybe that's the only way?

    What's the better way to do this, or is what I did the right way?
    Last edited by drmrgd; July 20th, 2013 at 03:48 PM.

  2. #2
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Best Practice for Iterating Over Two Files at the Same Time?

    Could you not just replace the suffix? something like

    Code:
    for f in *.xls; do perl vcf_comparison.pl "$f" "${f%.xls}.vcf"; done

  3. #3
    Join Date
    Apr 2011
    Location
    Maryland
    Beans
    1,461
    Distro
    Kubuntu 12.04 Precise Pangolin

    Re: Best Practice for Iterating Over Two Files at the Same Time?

    Quote Originally Posted by steeldriver View Post
    Could you not just replace the suffix? something like

    Code:
    for f in *.xls; do perl vcf_comparison.pl "$f" "${f%.xls}.vcf"; done
    Brilliant! I would have never thought of using a substitution like that to solve the problem. I was stuck on trying to figure out how to iterate through the numbers in the filenames. This worked perfectly! Great suggestion, steeldriver!

  4. #4
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Best Practice for Iterating Over Two Files at the Same Time?

    yup, sometimes it's hard to see the forest for the trees
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •