CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
grep or filtering in perl

 



anujoamon
New User

Dec 5, 2012, 1:53 PM

Post #1 of 3 (755 views)
grep or filtering in perl Can't Post

HI,

I am a beginner for perl usage.

I have a file that has multiple lines. For me the two lines of use are :
..... blah .... blah ....
The tool described : Hammer
..... blah .... blah ....
See Also : Wrench screw pliers nail
..... blah .... blah ....

I want to assign a variable to 4th variable (Hammer) based of the search string "the tool described"
and
an array for the See Also list (Wrench, screw, pliers, nail) based on the search string see also


I was trying to do such scripting with shell first, but want to move to perl because of its pros.

Thanks is advance.
Attachments: sample_file.txt (1.03 KB)


BillKSmith
Veteran

Dec 5, 2012, 7:38 PM

Post #2 of 3 (747 views)
Re: [anujoamon] grep or filtering in perl [In reply to] Can't Post

Use regular expressions to find and parse each line of interest.

Code
use strict; 
use warnings;
open my $IN, '<', 'sample_file.txt' or die "Cannot open sample_file.txt:$!";
my $variable;
my @array;
while (<$IN>) {
chomp;
if (/The tool described : (\w+)/) {
$variable = $1;
}
if (/See Also : (.+)/) {
@array = split /\s+/, $1;
}
}
close $IN;
print "Tool described: $variable\n";
print "See Also: @array", "\n";

Good Luck,
Bill


anujoamon
New User

Dec 6, 2012, 8:54 AM

Post #3 of 3 (741 views)
Re: [BillKSmith] grep or filtering in perl [In reply to] Can't Post

Thanks a lot Bill, it worked exactly as i wanted :)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives