Splitting a file and creating new files using Perl script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Splitting a file and creating new files using Perl script
# 8  
Old 06-20-2013
yes, you can use the system command :

Code:
system("your zip command here");

# 9  
Old 06-25-2013
Hi Fundix,

I tried to use this below command.But it is not working.

Code:
system( 'zip', $file )

I need to zip all the splitted files after creating each of them.
So can you please answer, where i can add the zip part in the main code.

-Deepak
# 10  
Old 06-25-2013
Hi there, hereafter the code with the zip output, working good on an Aix system.
Each file name created is stored in an array (@fileLst) and at the end of the program, all files of the array are zipped into an unique archive :
:
Code:
#!/usr/bin/perl -w
use strict;

my $cur_dir = $ENV{PWD};
my $filename = "$cur_dir/$ARGV[0]";
my ($record,$header,$i,@fields,%files,$key,@fileLst);

open(FILEIN,"<$filename") or die"open: $!";
while( defined( $record = <FILEIN> ) ) {
  chomp $record;
  $header=$record if (!defined $header);

  $i++;
  if($i > 1) {
    @fields=split(/\|/,$record);
    if(! exists( $files{$fields[0]}) ) {
      $files{$fields[0]} = "$fields[0].file";
      open (FILEOUT, "> /tmp/$files{$fields[0]}") ||
        die "FATAL: cannot open \"$files{$fields[0]}\" for writing: $!\n";
      push(@fileLst,"/tmp/$files{$fields[0]}");
      print FILEOUT "$header\n";
      close(FILEOUT);
    }
    open (FILEOUT, ">> /tmp/$files{$fields[0]}") ||
      die "FATAL: cannot open \"$files{$fields[0]}\" for writing: $!\n";
    print FILEOUT "$record\n";
    close(FILEOUT);
  }
}
close(FILEIN);

foreach (@fileLst) {
  system("zip /tmp/zipfile.zip $_");
}

Code:
%./file025.pl file026
  adding: tmp/SU.file (deflated 10%)
  adding: tmp/AR.file (deflated 9%)

Code:
dpi@%unzip -l zipfile.zip
Archive:  zipfile.zip
  Length     Date   Time    Name
 --------    ----   ----    ----
       91  06-25-13 08:51   tmp/SU.file
       93  06-25-13 08:51   tmp/AR.file
 --------                   -------
      184                   2 files

Hope this helps
# 11  
Old 06-25-2013
Better to use Archive::Zip module, than call the system command.
# 12  
Old 06-25-2013
That module is not installed on my Aix system (and am not root Smilie)

Maybe you can give us an updated vs of the program using that module.
It will be very interesting.

Thank You
# 13  
Old 06-25-2013
Hi Fundix,

Thanks a lot for your help.

Actually I need the individual splitted files to be zipped separately.

Example :

Code:
  SU.zip
  AR.zip

I am getting error like
Code:
Global symbol "@fileLst" requires explicit package name

-Deepak
# 14  
Old 06-25-2013
the array must be declared when using Strict :
Code:
my ($record,$header,$i,@fields,%files,$key,@fileLst);

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Creating script to read many files and load into database via single control file

Hi, I have many files but with only 2 names , I want to load the data of that file into database through sqlldr with single control file. how can i do that ????? Example: switch_file switch_file billing_file billing_file now these files should be loaded into same database but different... (1 Reply)
Discussion started by: niti_sharma
1 Replies

2. Shell Programming and Scripting

Script for splitting file of records into multiple files

Hello I have a file of following format HDR 1234 abc qwerty abc def ghi jkl HDR 4567 xyz qwerty abc def ghi jkl HDR 890 mno qwerty abc def ghi jkl HDR 1234 abc qwerty abc def ghi jkl HDR 1234 abc qwerty abc def ghi jkl -Need to split this into multiple files based on tag... (8 Replies)
Discussion started by: wincrazy
8 Replies

3. Shell Programming and Scripting

Splitting xml file into several xml files using perl

Hi Everyone, I'm new here and I was checking this old post: /shell-programming-and-scripting/180669-splitting-file-into-several-smaller-files-using-perl.html (cannot paste link because of lack of points) I need to do something like this but understand very little of perl. I also check... (4 Replies)
Discussion started by: mcosta
4 Replies

4. Shell Programming and Scripting

Splitting a file into several smaller files using perl

Hi, I'm trying to split a large file into several smaller files the script will have two input arguments argument1=filename and argument2=no of files to be split. In my large input file I have a header followed by 100009 records The first line is a header; I want this header in all my... (9 Replies)
Discussion started by: ramky79
9 Replies

5. UNIX for Advanced & Expert Users

Splitting a file into small files

Hi Folks, Please help me in solving the problem. I want to write script in order to split a file into small pieces and send it automatically through mail. Ex. The file name is CALM*.txt . It is around 50 MB. I want to split the file into 20 MB 2-3 smaller files and send (like uuencode) it... (6 Replies)
Discussion started by: piyushbhashkar
6 Replies

6. Shell Programming and Scripting

Data Splitting into two files from one file

I have a file as: I/P File: Ground Car 2009 Lib 2008 Lib 2003 Ground Car 2009 Ground Car 2003 Car 2005 Car 2003 Car 2005 Sita 2900 2006 Car 2007 I have to split the file into two: - one for names and second for years. O/p1 (Names): Ground Car (3 Replies)
Discussion started by: karumudi7
3 Replies

7. Shell Programming and Scripting

Splitting file into 2 files ?

Hi extending to one of my previous posted query .... I am using nawk -v invar1="$aa" '{print > ("ABS\_"((/\|/)?"A\_":"B\_")invar1"\_NETWORKID.txt")}' spfile.txt to get 2 different files based on split condition i.e. "|" Similar to invar1 variable in nawk I also need one more variable... (18 Replies)
Discussion started by: shekharjchandra
18 Replies

8. Shell Programming and Scripting

Splitting the files via shell script

Hi all, We have 102 flat files created by Informatica from 102 tables. These 102 files contain pharmcy details. There are a total of 450 pharmcyids.The naming convention for the flat file is ODS_<TABLE NAME>_yyyymmdd_timestamp.dat. Each flat file may contain data for 450 pharmacies which is... (2 Replies)
Discussion started by: Maya_Pillai
2 Replies

9. Shell Programming and Scripting

Splitting files from one file

Hi, I have an input file like: 111 abcdefgh asdfghjk dfghjkl 222 aaaaaaa bbbbbb 333 djfhfgjktitjhgfkg 444 djdhfjkhfjkghjkfg hsbfjksdbhjkgherjklg fjkhfjklsahjgh fkrjkgnj I want to read this input file and make separate output files with the header as numric value like "111"... (9 Replies)
Discussion started by: saltysumi
9 Replies

10. Shell Programming and Scripting

Creating loop for a script -Perl

Hi Guyz I designed a script that can compare 2 columns(values) of single file and gives the closest numbers to the first column by comparing the numbers in first column with second and it works in a single file. Now I'm trying to design a new script with 2 objectives for 2 files (not a single... (4 Replies)
Discussion started by: repinementer
4 Replies
Login or Register to Ask a Question