The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl question regarding [ ] hankooknara Shell Programming and Scripting 2 07-01-2007 01:21 PM
perl question BG_JrAdmin Shell Programming and Scripting 1 09-09-2005 04:46 PM
Perl Question Gary Dunn Shell Programming and Scripting 2 12-03-2004 09:37 AM
PERL question frank Shell Programming and Scripting 1 06-18-2002 12:13 AM
Perl question Jubba Shell Programming and Scripting 1 04-01-2002 11:24 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 09-27-2005
Registered User
 

Join Date: Feb 2005
Location: Columbus OH
Posts: 133
PERL question

Hello,

pkzipc of a certain zip file yeilds the following in shell

Code:
PKZIP(R)  Version 6.0  FAST!  Compression Utility for AIX
Copyright 1989-2002 PKWARE Inc.  All Rights Reserved. Registered Version
PKZIP Reg. U.S. Pat. and Tm. Off.  Patent No. 5,051,745

Viewing .ZIP: test.zip

  Length Method     Size  Ratio    Date     Time   CRC-32  Mode   Name
  ------ ------     ----  -----    ----     ----   ------  ----   ----
  1002KB DeflatN    226KB 77.5% 09/26/2005  3:16p a7f6d4e9 0777   M0511514944/file1
   474KB DeflatN     91KB 80.8% 09/26/2005  3:16p fbc1c376 0777   M0511514944/file2
   476KB DeflatN     91KB 80.9% 09/26/2005  3:16p 9fac9cf5 0777   M0511514944/file3
   113KB DeflatN     92KB 18.5% 09/26/2005  3:16p 5b5b5521 0777   M0511514944/file4
  ------           ------ -----                                   ----
  2065KB            500KB 75.8%                                      4
In perl I want to be able to do the following.

issue the following shell command.

Code:
pkipc test.zip
which would result in something like

Code:
PKZIP(R)  Version 6.0  FAST!  Compression Utility for AIX
Copyright 1989-2002 PKWARE Inc.  All Rights Reserved. Registered Version
PKZIP Reg. U.S. Pat. and Tm. Off.  Patent No. 5,051,745

Viewing .ZIP: test.zip

  Length Method     Size  Ratio    Date     Time   CRC-32  Mode   Name
  ------ ------     ----  -----    ----     ----   ------  ----   ----
  1002KB DeflatN    226KB 77.5% 09/26/2005  3:16p a7f6d4e9 0777   M0511514944/file1
   474KB DeflatN     91KB 80.8% 09/26/2005  3:16p fbc1c376 0777   M0511514944/file2
   476KB DeflatN     91KB 80.9% 09/26/2005  3:16p 9fac9cf5 0777   M0511514944/file3
   113KB DeflatN     92KB 18.5% 09/26/2005  3:16p 5b5b5521 0777   M0511514944/file4
  ------           ------ -----                                   ----
  2065KB            500KB 75.8%                                      4
I need the take only the columns and values output by the pkzip command and add the constant "COMMENT ;" in front of it to make the following

Code:
COMMENT       ;  Length Method     Size  Ratio    Date     Time   CRC-32  Mode   Name
COMMENT       ;  ------ ------     ----  -----    ----     ----   ------  ----   ----
COMMENT       ;  1002KB DeflatN    226KB 77.5% 09/26/2005  3:16p a7f6d4e9 0777   M0511514944/file1
COMMENT       ;   474KB DeflatN     91KB 80.8% 09/26/2005  3:16p fbc1c376 0777   M0511514944/file2
COMMENT       ;   476KB DeflatN     91KB 80.9% 09/26/2005  3:16p 9fac9cf5 0777   M0511514944/file3
COMMENT       ;   113KB DeflatN     92KB 18.5% 09/26/2005  3:16p 5b5b5521 0777   M0511514944/file4
COMMENT       ;  ------           ------ -----                                   ----
COMMENT       ;  2065KB            500KB 75.8%                                      4
Finally I need the above appended to an existing log file.
Please advise me on how to achieve this using PERL. Thanks for your help.

Jerardfjay
Reply With Quote
Forum Sponsor
  #2  
Old 09-27-2005
Registered User
 

Join Date: Jul 2005
Posts: 137
Code:
pkipc test.zip |awk '"Length"==$1,0{print "COMMENT  ; " $0}' >>logfile
Reply With Quote
  #3  
Old 09-27-2005
Moderator
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,477
In Perl, something along the lines of

Code:
#!/usr/bin/perl 

$out = `pkzipc test.zip`;
$start = 0;
$buf = '';
foreach $line (split /\n/, $out) {
	if ($line =~ /^\s*Length/) { $start = 1; }
	if ($start) {
		$buf .= ('COMMENT   ; ' . $line . "\n");
	}
}
open FILE, ">>file.txt" or die "cannot open file for writing";
print FILE $buf;
close FILE;
Reply With Quote
  #4  
Old 09-28-2005
Registered User
 

Join Date: Feb 2005
Location: Columbus OH
Posts: 133
Thanks cbkihong,

Its exactly what I was looking for. Although I understand the piece where you concatenate the string "COMMENT ;" to the existing lines after filtering unwanted lines, what is the logic that you are using to filter the lines that start with PKZIP,Copyright and Viewing?

The other question that I have is that how can I use a variable that holds the name of the logfile instead of hardcoding the line

Code:
open FILE, ">>file.txt" or die "cannot open file for writing";
Thanks for your help.
Jerardfjay

Last edited by jerardfjay; 09-28-2005 at 05:19 AM. Reason: claifying the question
Reply With Quote
  #5  
Old 09-28-2005
Registered User
 

Join Date: Feb 2005
Location: Columbus OH
Posts: 133
Quote:
Originally Posted by jerardfjay
The other question that I have is that how can I use a variable that holds the name of the logfile instead of hardcoding the line

Code:
open FILE, ">>file.txt" or die "cannot open file for writing";
Jerardfjay
Just using the variable $LogFile that holds ths name of the logfile seems to do the trcik. Thanks.
Reply With Quote
  #6  
Old 09-28-2005
Moderator
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,477
Quote:
Originally Posted by jerardfjay
Its exactly what I was looking for. Although I understand the piece where you concatenate the string "COMMENT ;" to the existing lines after filtering unwanted lines, what is the logic that you are using to filter the lines that start with PKZIP,Copyright and Viewing?
There's no trick. Just find the line that contains "Length" and process those thereafter, so a sed run will do the same trick.

But a warning though. The routine will not notice if the command fails for whatever reason. Maybe you will want to check $? (if pkzipc obeys so) for return value, e.g.

Code:
$out = `pkzipc test.zip`;
if ($? >> 8) {
    # error (assume non-zero return status is error)
}
As a sidenote, did you try the Archive::Zip module (of course you will need to download and install it)? It seems to let you retrieve such information programmatically:

http://search.cpan.org/~smpeters/Arc...mple_accessors
Reply With Quote
  #7  
Old 09-30-2005
Registered User
 

Join Date: Feb 2005
Location: Columbus OH
Posts: 133
Quote:
Originally Posted by cbkihong
There's no trick. Just find the line that contains "Length" and process those thereafter, so a sed run will do the same trick.
Thanks cbikong. I was actually referring to my question of trying to use the variable name instead of the hardcode log file name in the line of code


Code:
open FILE, ">>file.txt" or die "cannot open file for writing";
Thanks for the Archive::Zip module for perl compression-decompression. However, like I have mentioned before, I would have to create my own perl environment in order to get this installed and have it working.
Moreover, there is logic in the original program to perform a pkzipc only when the contents of the zip file is greater than zero.

Thanks for your help.
Jerardfjay
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:20 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0