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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
perl script to add a line into a file karthikn7974 Shell Programming and Scripting 2 06-03-2008 02:11 AM
help on a perl script to edit file meghana Shell Programming and Scripting 9 05-14-2008 03:42 PM
grep ^M in file using perl script.... zedex Shell Programming and Scripting 12 02-06-2008 08:43 AM
Have a shell script check for a file to exist before processing another file heprox Shell Programming and Scripting 3 11-14-2006 03:26 AM
File processing on perl garric Shell Programming and Scripting 2 09-02-2006 12:25 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-23-2008
SEEHTAS SEEHTAS is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 4
Post perl script for file processing

Aim:

To scan a file and ignore all characters that has an ASCII value from 0 to 31 and 127 to 255 and accept only those characters having an ASCII between 32 and 126.

Script:

#!/usr/local/bin/perl
$filename = "$ARGV[0]";
if (-e $filename)
{
open(OUT, "${filename}") || die "can't open $filename\n";
while (<OUT>){
$found= "";
$stat=0;
chomp $_;
my @charArray = split(//, $_);
my $ref = \@charArray;
foreach (@charArray) {
$val = ord($$ref[$stat]);
if(($val>31)&&($val<127)){
$found = "$found$$ref[$stat]";
}
$stat++;
}
$found = "$found\n";
print $found;
}
close(OUT);
}

Problem:
The code mentioned above runs for 20-25 mins for a 500 MB file. This is very slow.


Can someone let me know if this can be done in a more efficient way so as to reduce the file processing duration?
  #2 (permalink)  
Old 03-23-2008
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,958
try this,


Code:
#! /opt/third-party/bin/perl

open(FILE, "<", $ARGV[0]) || die ("unable to open <$!>\n");

while( read(FILE, $data, 1) == 1 ) {
  $ordVal = ord($data);
  print "$ordVal"  if( $ordVal >= 32 && $ordVal <= 126 );
}

close(FILE);

exit(0);

  #3 (permalink)  
Old 03-24-2008
SEEHTAS SEEHTAS is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 4
Hi Madhan,

Corrected code:

#!/usr/local/bin/perl
open(FILE, "<", $ARGV[0]) || die ("unable to open <$!>\n");
while( read(FILE, $data, 1) == 1 ) {
if((ord($data)>=32)&&(ord($data)<=126)){
print "$data";
}
if(ord($data)==10){
print "\n";}
}
close(FILE);

Its great it takes just 10 mins now. Is there anything else that can be done to reduce the duration further?
  #4 (permalink)  
Old 03-24-2008
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,958
Minor change but this will make a difference

change the following

Quote:
if((ord($data)>=32)&&(ord($data)<=126)){
print "$data";
}
if(ord($data)==10){
print "\n";}
}
to


Code:
print "$data" if((ord($data)>=32)&&(ord($data)<=126));

print "\n" if(ord($data)==10);

  #5 (permalink)  
Old 03-24-2008
SEEHTAS SEEHTAS is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 4
Hi Madhan,

Thanks..It still takes 10 mins. I have one question here, if we are reading the entire file and moving one character by character won't it consume valuable memory? For eg in C we can take certain bytes (as first batch) from the file and process it and then follow with the next batch of the file.
Can anything be done here?

Please correct me if I am wrong.
  #6 (permalink)  
Old 03-24-2008
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,958
Quote:
It still takes 10 mins.
Are you testing with the same input file ?
And probably with the same load each time ?
  #7 (permalink)  
Old 03-24-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Quote:
Originally Posted by SEEHTAS View Post
I have one question here, if we are reading the entire file and moving one character by character won't it consume valuable memory? For eg in C we can take certain bytes (as first batch) from the file and process it and then follow with the next batch of the file.
Can anything be done here?
Perl actually has pretty efficient internal optimizations for this kind of stuff.

If you are really into optimization, you can calculate a baseline by running just perl -ne 1 on the file and then see how much your additional processing takes time. Add some more steps piecemeal and see if there are any really big jumps in the stats. If there are, figure out if you are disabling some internal optimization and if rephrasing the code can get it back.

Can you split the processing, like tr -d '\000-\037\200-\377' <file | perl ... and get away with it?

(Or '\000-\011\013-\037\200-\377' if you want to preserve the newlines, like matrixmadhan observed.)

Last edited by era; 03-29-2008 at 06:53 PM.. Reason: newline observation
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:10 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0