Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

data extraction perl

Posted on 2007-10-15
6
Medium Priority
?
575 Views
Last Modified: 2012-08-14
sample data.csv
05/03/2001,73,110,abc
05/04/2001,73,120,def
05/05/2001,99,120,ghi
05/06/2001,99,130,jkl
05/07/2001,99,140,mno

open(DAT,"data.csv");
while(<DAT>)
{
@data=split(',',$_);
print "some @data\n";
}
print "some @data\n";

If I do this
I get the output as
some abc
some def
some ghi
some jkl
some mno
some mno


I want the output to be only

abc

or  put abc in a variable say $v
and on doing print "$v\n";
the output should be
abc

Please suggest changes to the perl script.

Thanks
Begunix
0
Comment
Question by:Begunix
5 Comments
 

Expert Comment

by:tbolick
ID: 20080228
Try this:

open(DAT,"data.csv");
while(<DAT>)
{
($d,$i1,$i2,$v) = split(',',$_);
print "$v\n";
}
print "$v\n";

This will put the data into the variables $d, $i1, $i2, and $v.  It is loading a LIST of variables.
0
 

Author Comment

by:Begunix
ID: 20080604
The output I get is
abc
def
ghi
jkl
mno
mno

I want only abc.
Please advise.
0
 

Assisted Solution

by:tbolick
tbolick earned 664 total points
ID: 20080907
Oh, then do this:

open(DAT,"data.csv");
$linein = <DAT>;
($d,$i1,$i2,$v) = split(',',$linein);
print "$v\n";

You could also just do ($d,$i1,$i2,$v) = split(',',<DAT>);

0
 
LVL 9

Assisted Solution

by:ghostdog74
ghostdog74 earned 664 total points
ID: 20082358
#!/usr/bin/perl
while (<>) {
    @a = split(/,/, $_, -1);
    if ($. == 1) {
        print $a[$#a];
    }
}

usage: perl myscript.pl csvfile
0
 
LVL 28

Accepted Solution

by:
FishMonger earned 672 total points
ID: 20082797
open(DAT,"data.csv") || die $!;

my $row1_last_field = (split(/,/,<DAT>))[-1];
print $row1_last_field;

while (<DAT>) {

    # process the rest of the file as needed
}
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question