Solved

Reading selective values from *.csv file in Perl

Posted on 2003-12-08
3
757 Views
Last Modified: 2012-06-21
Hi,
I have a *.csv file that looks like this. I need to ignore Rows 1-5
and start reading from Row 6. From Row 6 I need only Column1, Column
3, Column4 and so on....(has about 35 values in each row).


RED - Letters in Red auto-populate columns from left to right.            
Color Code - Yellow Field name boxes in yellow are fields required
Color Code - Gray Field name boxes in gray are fields are optional            
Color Code - Green Field name boxes in green are optional fields
Color Code - Orange Field name boxes in orange indicate d - refer etc
44383      Ontario Dork Canal      288      1398 Malheur Ave      TAC31NT
44384      Ontario Dork Canal      288      1398 Malheur Ave      TAC31NT       
44385      Ontario Dork Canal      288      1398 Malheur Ave      TAC31NT


Previously, I used to copy manually the values I wanted to the
input.csv file. But, now I want to read directly from the source file
and avoid the extra step.

My code looks like this

open FILE, "input.csv";

while (<FILE>) {
      chomp;
      my ($col1, $col2, $col3, $col4, $col6) = split (/\,/);
}
0
Comment
Question by:nparthi
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
josephfluckiger earned 20 total points
ID: 9898901
How about this:
-------------------------------
open FILE, "input.csv";
my $rowCount=0;
while (<FILE>) {
     $rowCount++;
     next if $rowCount < 6; # this skips the first 5 rows.
     chomp;
     my ($col1, $col2, $col3, $col4, $col6) = split (/\,/);
}
-------------------------------

Does the raw input file have commas in it, or tab characters (your sample file doesn't , but your code does).
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 9899344
Your example csv file doesn't have any commas!

open FILE, "input.csv";

for (1..5) {$junk = <DATA>; undef $junk;}  # Throw away the first 5 rows

while (<DATA>) {
   chomp;
# use this next line only if you don't use the previous for loop
   next if ($. < 6)
   @col = split ",";  # split the row and put it into an array
# process the array
}
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 9899398
Oops, I used the built-in DATA file handle and forgot to change that part of the code to FILE when I posted.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Strange perl issue 6 125
Shell script to bind my job to half the number of vCPUs in Solaris 8 114
File Find regex problem 4 60
Perl Untar File 1 25
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…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

943 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

Need Help in Real-Time?

Connect with top rated Experts

1 Experts available now in Live!

Get 1:1 Help Now