Home: Perl Programming Help: Regular Expressions:
how can I extract from file with info separated by newline



jujea
New User

Jun 4, 2008, 2:33 PM


Views: 8137
how can I extract from file with info separated by newline

how can I extract the $firstname, $lastname and rest from a file that has each value on a newline. the file contains more than one entry and are separated by ----------- for example


First Name : James
Last Name : Segrove
DOB : 07/26/1970
Email : jsegrove@ntlworld.com
Password : tramontana
Address : 50 Chesilton Crescent
City : Fleet
Zip : GU52 6PB
State : Hanmshire
Country : United Kingdom
Phone : 01252 616106
---------------------------------------------------------------------------------------------------------


(This post was edited by perlguru_admin on Jun 8, 2008, 9:57 AM)


rajiv_chennai
Novice

Jun 4, 2008, 10:07 PM


Views: 8114
Re: [jujea] how can I extract from file with info separated by newline

Lets try this code,i think this what you are expecting.


Code
#!/usr/bin/perl 

my @array=<DATA>;

my @first_array=grep(/First Name :(.)/,@array);

map{~s/First Name :(.)/$1/}@first_array;

my @last_array=grep(/Last Name :(.)/,@array);

map{~s/Last Name :(.)/$1/}@last_array;


print @first_array;

print "\n";

print @last_array;


__END__

First Name : James
Last Name : Segrove
DOB : 07/26/1970
Email : jsegrove@ntlworld.com
Password : tramontana
Address : 50 Chesilton Crescent
City : Fleet
Zip : GU52 6PB
State : Hanmshire
Country : United Kingdom
Phone : 01252 616106
---------------------------------------------------------------------------------------------------------


Regards
Rajiv


(This post was edited by perlguru_admin on Jun 8, 2008, 9:57 AM)


KevinR
Veteran


Jun 5, 2008, 11:56 AM


Views: 8092
Re: [rajiv_chennai] how can I extract from file with info separated by newline

Define the input record seperator and perl will read the records into an array properly from the file. Then parse each element of the array into fields.
-------------------------------------------------