Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2007
    Posts
    3

    How to read a one line at a time from a gunzip file in perl

    Hi,

    I am newbie to perl. I have to write a script that read a file which is in .gz format. The task i have to do is to read line number 1 first which contains a header, extract the field from line one(which is a header of log format and it's gets chang everyday). This extracttion will give me an idea at which numbers (if i am copying in array) the logs of interest will be. After this i want to read one line at a time from .gz file, extract the log at specific spot in an array and copy it in .txt file and later displays in html format.

    I just needed help with the reading part of .gz file. Any help is greatly appreciated.

    The header format is for example

    num,date,time,orig,type,action,alert,i/f_name,i/f_dir,Total logs,Suppressed logs,auth_method,reason

    I am using right now

    @ta = `gzcat logtext.20070318.zrc2s0mc.gz|head -1`;
    which gives me the number of the field after comparison

    If someone can help with to read a file one line after it like

    @logfile = `gzcat logtext.20070318.zrc2s0mc.gz|head -I`;

    where -I is a varaiable and gets incremented one line at a time till the files end and also they line in logs varied every day. so can not dump the whole file in an array like @ta = `gzcat logtext.20070318.zrc2s0mc.gz|head -2000`;

    Please let me know if there is a way i can do this in perl.

    Thanks in advance.

    Regards,
    Vohra

  2. #2
    Join Date
    Oct 2002
    Location
    Baghdad, Iraq
    Posts
    697
    Really, the backtick method is not really good for general usage.

    Try this, and perldoc -f open while you're at it:

    Code:
    open my $handle, '-|', '/usr/bin/gzcat', $filename;
    Advantages:

    You get a filehandle. See perldoc IO::Handle for what you can do with it. Instead of "my $handle" you can use a fileglob, e.g. HANDLE.

    You don't have to worry about escaping the filename because any special characters are handled automatically.

    Reading the data works the same way as with a regular file, so remember to chomp or chop as needed.

  3. #3
    Join Date
    Apr 2007
    Posts
    3
    Thanks for help

Posting Permissions

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