Computing.Net > Forums > Programming > line pattern perl programming

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

line pattern perl programming

Reply to Message Icon

Name: ravi_kv123
Date: April 9, 2007 at 09:54:20 Pacific
OS: fc 6
CPU/Ram: 128MB
Product: -
Comment:

hi every one,
I need ur help for my program
I need a perl script that will search for a word in a given file and a given line number so tht i can change the line for searching.
Is it possible to code with perl.
i tried a lot with grep ..but cant get the desired output.
Any one can get me the code??
thanx in advance
ravi

hai



Sponsored Link
Ads by Google

Response Number 1
Name: FishMonger
Date: April 9, 2007 at 10:38:24 Pacific
Reply:

perl -ne 'print "$.\n" if /word/"'


0

Response Number 2
Name: ravi_kv123
Date: April 9, 2007 at 10:45:33 Pacific
Reply:

hay thanx for the reply...

Actually my aim is to display the % completed whn the search is in progress
so i thought of dividing in to lines n searchng each line n caluclating the %
so is there any way for tht..

hai


0

Response Number 3
Name: FishMonger
Date: April 9, 2007 at 11:08:11 Pacific
Reply:

To get the percentage done, you first need to know how much there is to do.

Are you planning on outputting the percentage only when matching the "word", or at set intervals? Outputting at set intervals would be more appropriate.

What have you tried so far? Please post your code?


0

Response Number 4
Name: FishMonger
Date: April 9, 2007 at 20:38:16 Pacific
Reply:

There are several methods to find out how many lines are in a file. The most common is to read-in the file into an array and assign a scalar the size (number of elements) of the array. Then you can use that scalar as the base to calculate the percentage done while looping through the array.


0

Response Number 5
Name: ravi_kv123
Date: April 9, 2007 at 23:45:15 Pacific
Reply:

thanx for the reply
Outputting at set intervals is better i think
but if i use a array it may take lot of time and memory depending on the size of the file
My aim is to display the % completed ramdomly while searching n display the results after search.
here is the code tht am working with

#!/usr/bin/perl
print "Enter a File name :";
chomp ($file = <STDIN>);
print "\n Searching file :";
if (system ("ls $file")==0)
{
print "File Found\n";

$lines = `wc -l $file`;
@personal = split(/ /, $lines);
$lines = @personal[0];

print "Total number of lines in the file = $lines \n";

print "Enter the pattern to search :";
chomp ($pattern = <STDIN>);
print "\n";

# to search the no of words (pattern search)
$abc=`grep $pattern $file | wc -l`;
print "Total number of results found $abc \n";
print "here are the results ...\n";
system("grep $pattern $file");


}
else{
print "File not Found\n";
}


in place of system("grep $pattern $file");
i wann use a code tht can search by line so tht i can keep tht in a loop n calculcate the %.
i you think this cant work can u gv me any idea or a sample code??
thanx agn
ravi


hai


0

Related Posts

See More



Response Number 6
Name: FishMonger
Date: April 10, 2007 at 09:28:44 Pacific
Reply:

Why are you writting a Perl script when instead of using Perl, you're shelling out every command? That makes no sense. You should either use Perl or a shell script, but please don't use Perl to simply execute shell commands and nothing else.

Every Perl script you write should, without execption, start with loading the warnings and strict pragmas.
http://search.cpan.org/~nwclark/per...
http://search.cpan.org/~nwclark/per...

If you're worried about the memory usage on LARGE files, you can use the Tie::File module.
http://search.cpan.org/~mjd/Tie-Fil...

===========================================
This doesn't meet all of your requirements, but it covers most of what you need.

#!/usr/bin/perl

use warnings;
use strict;

print 'Enter a File name: ';
chomp (my $file = <STDIN>);
die "$file can't be found\n" unless -e $file;

print 'Enter a search pattern: ';
chomp (my $pattern = <STDIN>);

open (F, '<', $file) || die $!;
my @lines = <F>;
my $cnt = @lines;
my $found = 0;

foreach my $line (@lines) {
     print $line and $found++ if $line =~ /\Q$pattern\E/;
}
print "\nTotal lines matched: $found\n";


0

Response Number 7
Name: ravi_kv123
Date: April 10, 2007 at 23:39:19 Pacific
Reply:

thanx man
Actually i am new to perl.
but i know bash scripting well
so i mixed up as per my task
thanx for the code
i can get to read the line by using
awk 'NR == $line' $file
thanx a lot for the reply and the code
bye


ravi


0

Response Number 8
Name: ravi_kv123
Date: April 10, 2007 at 23:43:44 Pacific
Reply:

i mixed ur code with mine n got the result :-))
thanx agn
bye

ravi


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: line pattern perl programming

Simple perl program www.computing.net/answers/programming/simple-perl-program/5985.html

Need Perl Programming Help!!!!! www.computing.net/answers/programming/need-perl-programming-help/762.html

Basic PERL programming script www.computing.net/answers/programming/basic-perl-programming-script/16791.html