By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,834 Members | 2,234 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,834 IT Pros & Developers. It's quick & easy.

Assembling small files in a large file but format is shifted

P: 4
Hi Perl Experts:
I am relatively new to perl and did try to solve the problem by searching books and web but could not exactly solve it. Here is the problem. I want to gather "*.igf" files and put all the lines from these files in a larger file. However, each small file has a header on the first line. I want header only once in a bigger file. My current code does everything fine except content from file other than first file is shifted and it shifts the format. My question is how to discard first header line except when it is the first file but still maintain the format. Below is the code.

Thank you for your help.
Expand|Select|Wrap|Line Numbers
  1. my @igf_files = glob("*.igf");
  2. my $all_igf = "all_igf.igf";
  3. my $counter = 0;
  4. foreach my $fileigf (@igf_files) {
  5. if ($counter==0){
  6. open (IGF,"<$fileigf");
  7.     open (ALLIGF,"+>>$all_igf");
  8.     while(<IGF>)
  9.     {
  10.      print ALLIGF $_;
  11.     }
  12. close (IGF  );
  13. close ( ALLIGF );
  14. $counter++;
  15. }
  16. else{
  17. open (IGF,"<$fileigf");
  18. my @lines =(<IGF>);
  19. open (ALLIGF,"+>>$all_igf");
  21.     {
  22.     print ALLIGF @lines[1..$#lines];
  23.      }
  24. close (IGF);
  25. close ( ALLIGF );
  26. }
  27. }
Feb 11 '10 #1
Share this Question
Share on Google+
5 Replies

Expert Mod 100+
P: 589
Use the shift function to remove the first filename off of the array and assign it to a scalar.

Process that file, then start your loop over the remaining files.
Feb 11 '10 #2

P: 4
Thank you Ron for your reply. However, yes, I can get the same functionality with shift operator but the formatting is still shifted. In the sense, content from the second small file is shifted by one space.

Feb 12 '10 #3

Expert Mod 100+
P: 589
In what way is it shifted by 1 space?

Do you end up with a single space between the contents of file1 and file2?
Do you end up with a single space between between file2 and file3?
Do you end up with a single space between each and every file?

Or, does it shift the other way by stripping off 1 character?

Can you attach a couple sample files so I can test and duplicate your issue?
Feb 12 '10 #4

Expert Mod 100+
P: 589
You should open the $all_igf file only once. Open it prior to opening the other files and keep it open until all files have been processed.
Feb 12 '10 #5

P: 4
Hi Ron:
Thanks for sharing possibilities. It seems to me first, I should look into the way files were created. I create these files using a function and then process them. I am going to look into the way files are created. If I am unable to figure it out even after manipulating function, I will surely post sample files.

Feb 12 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.