Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Data extraction from text file

by usadhikari (Initiate)
on Aug 09, 2011 at 09:35 UTC ( #919440=perlquestion: print w/ replies, xml ) Need Help??
usadhikari has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I am novice with perl, trying to extract data from a text file, given below is input and required output, could you please help me with a simple program. INPUT PATTERN ------- url: www.google.com
name: Umesh
address: New Delhi
room no: 001
phone no: 34738472
-------

url: www.yahoo.com
name: Raj
address: Sahadara
room no: 002
phone no:
-------

url: www.rediff.com
name: Hem
address: mumbai
room no: 003
phone no: 84398419
-------

url: www.google.com
name: Mukesh
address:
room no: 004
phone no:
-------

name: abcxyz
address:
room no:
phone no: 4814314783
-------

name: Rakesh
address: jrieiqrjqq
room no:
phone no:
----------
output would be: 4814314783
url nameaddressroom nophone no
www.google.comUmeshNew Delhi134738472
www.yahoo.com RajSahadara 2
www.rediff.comHemmumbai384398419
www.google.comMukeshabcxyz

Comment on Data extraction from text file
Re: Data extraction from text file
by JavaFan (Canon) on Aug 09, 2011 at 09:37 UTC
    Please provide <code> or <pre> tags for data.

    And what have you tried? What isn't working as you expected?

      Actually i m trying to extract data from a text file.. but i m not able to do so.. kindly help me :(
        I'm sure your teacher gave you some pointers. Start with those, then have a look at what your textbook has to say on the matter.
Re: Data extraction from text file
by Anonymous Monk on Aug 09, 2011 at 09:38 UTC
Re: Data extraction from text file
by Utilitarian (Vicar) on Aug 09, 2011 at 09:49 UTC
    So you want to
    • open a file
    • read a series of records from the file (No guaranteed unique field, so perhaps an array of arrays? or an array of hashes might be more suitable)
    • Print a fixed field width record from each array entry.
    Have a go at coding that and get back to us with any problems you have with your Perl

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: Data extraction from text file
by blindluke (Beadle) on Aug 09, 2011 at 10:25 UTC

    This should extract your data, if I understand your input file format correctly (please, read the links that were kindly provided to you and format the post before you post it).

    #!/usr/bin/perl -w use strict; my @records; my %record; my $recordinprogress = 0; while (<>) { if (/-------/) { if ($recordinprogress) { push @records, { %record }; %record = (); $recordinprogress = 1; } } elsif (/([^:]+): (.*)/) { $recordinprogress = 1; $record{$1} = $2; } }

    The code above, when used on your data, produces the following array of hashes:

    $VAR1 = [ { 'phone no' => '34738472 ', 'url' => 'www.google.com ', 'name' => 'Umesh ', 'address' => 'New Delhi ', 'room no' => '001 ' }, { 'phone no' => '', 'url' => 'www.yahoo.com ', 'name' => 'Raj ', 'address' => 'Sahadara ', 'room no' => '002 ' }, { 'phone no' => '84398419 ', 'url' => 'www.rediff.com ', 'name' => 'Hem ', 'address' => 'mumbai ', 'room no' => '003 ' }, { 'phone no' => '', 'url' => 'www.google.com ', 'name' => 'Mukesh ', 'address' => '', 'room no' => '004 ' }, { 'phone no' => '4814314783 ', 'name' => 'abcxyz ', 'address' => '', 'room no' => '' }, { 'phone no' => '', 'name' => 'Rakesh ', 'address' => 'jrieiqrjqq ', 'room no' => '' } ];

    Now, it's up to you to print it out. I suggest using CSV if you want to import the data to an application like Gnumeric or Excel.

    If you have any further questions, show something that you managed to do. You'll find many Monks willing to help you, but that does not mean someone will do everything for you.

      Alternatively, you could set the input record separator ($/) to "-------", which simplifies things a bit:

      $/ = "-------\n"; my @records; while (<>) { my @entries = map { /([^:]+):\s*(.*)/ } split /\n/; push @records, { @entries } if @entries; }
Re: Data extraction from text file
by Anonymous Monk on Aug 09, 2011 at 20:55 UTC
    You can do this with Perl or simply with "awk." There are two parts to this problem. The first is recognizing each type of input-line and grabbing the piece that you want from each, storing it in memory. The second is recognizing when the line that you now have (don't forget end-of-file) means that you now have enough information to generate an output-line. "Awk" was built expressly for this, and "perl," long long ago, derived from "awk."

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://919440]
Approved by mertserger
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (16)
As of 2014-04-17 17:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (453 votes), past polls