If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > Perl and the DBI > How to read a one line at a time from a gunzip file in perl

LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Registered User
Join Date: Apr 2007
Posts: 3
How to read a one line at a time from a gunzip file in perl


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.

Reply With Quote
  #2 (permalink)  
Registered User
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:

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

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.
Reply With Quote
  #3 (permalink)  
Registered User
Join Date: Apr 2007
Posts: 3
Thanks for help
Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On