Linux Stall
  • Home
  • Android
  • How to
  • Perl
  • Tips
  • Tutorials
No Result
View All Result
Linux Stall
No Result
View All Result
Home Perl

How to read a CSV file in Perl?

Chankey Pathak by Chankey Pathak
January 10, 2012
in Perl, Tutorials
45 1
6
read csv file perl

read csv file perl

15
SHARES
759
VIEWS
Share on FacebookShare on Twitter

Perl is used for manipulating text files and in this article I will teach how to read a CSV file in Perl? First of all let’s start with

What is a CSV file?

A comma-separated values (CSV) file stores tabular data (numbers and text) in plain-text form. As a result, such a file is easily human-readable (e.g., in a text editor).

CSV is a simple file format that is widely supported by consumer, business, and scientific applications. Among its most common uses is to move tabular data between programs that naturally operate on a more efficient or complete proprietary format. For example: a CSV file might be used to transfer information from a database program to a spreadsheet. -Wikipedia

Using Perl to read a CSV file

Now suppose we have a CSV file which has 4 rows and each row has fields which are separated by comma as given below:

Linux,Unix,1,Ubuntu
RHEL,Kubuntu,2,Fedora
Mint,Puppy,3,ArchLinux
Mandriva,OpenSUSE,4,CentOS

If you want to extract out the data from 3rd column and calculate the sum of all the numbers then the approach to do this will be as follows:

Read the first line of the CSV file and extract out the 3rd column. Assign the extracted value to a scalar variable and then read the next line, extract out the value of 3rd column, add it to the scalar variable which contains the first value and store result in the same variable. Then read the 3rd line and so on…

Below is the Perl code for this purpose using split function.

  #!/usr/bin/perl
  use strict;
  use warnings;

  my $filename = $ARGV[0];
  my $total = 0;

  open(my $FH, '<', $filename) or die "Couldn't open $filename $!";
  
  while (my $fetchline = <$FH>){
    chomp $fetchline;
    my @elements = split "," , $fetchline;
    $total += $elements[2];
  }

  print "$total\n";

Save the file as text.plx and if the CSV file name is filename.csv then you may run the code as

$ perl text.plx filename.csv

In this case we get 10 (1+2+3+4) as output.

In this article we used split function to extract out the columns from CSV file using comma (,) as separator. But this code is error prone because of 2 reasons.

Reason 1: If the filename.csv contains something like

Linux,Unix,1,Ubuntu
RHEL,Kubuntu,2,Fedora
Mint,"Puppy,Linux",3,ArchLinux
Mandriva,OpenSUSE,4,CentOS

Notice the 3rd row. The 2nd field of it has a comma “Puppy,Linux”. If we use the same approach of splitting fields by comma separator then it will throw an error because split function will split out Puppy and Linux since they are separated by a comma. Then it will do something like 1+2+Linux+4 which is wrong.

Reason 2: If the filename.csv contains something like

Linux,Unix,1,Ubuntu
RHEL,Kubuntu,2,Fedora
Mint,"Puppy,
Linux",3,ArchLinux
Mandriva,OpenSUSE,4,CentOS

Here the 3rd row contains multi-line fields. When the above code will be used it will treat the CSV file as it contains 5 lines instead of 4.

So we can conclude that if the CSV file is in good format AND if it does not contain field separators within quote AND if there are no multi-line fields in CSV file then our code (which we have written above) will work perfectly and will give correct result. Take use of Text::CSV_XS module which is used for reading and writing CSV files, it will deal with those error prone situations very efficiently.

Tags: CSVfile handlinghow toPerlsplitTutorials
Previous Post

How to install rpm package on linux ?

Next Post

Linux Command Line tips that every Linux user should know

Chankey Pathak

Chankey Pathak

Data Scientist at Morgan Stanley. I've been using Linux since past 12 years. I plan to share what I know about Linux in this blog.

Related Posts

case of computer server workstation technology wireless vector illustration
Tutorials

Port Forwarding in OpenSSH

July 25, 2020
softlinking linux
Tutorials

Softlinks vs. Hardlinks: A Quick Explanation

July 23, 2020
write chroot read stat
Tutorials

Strace in Linux: History, Structure and Usage

July 21, 2020
case of computer server workstation technology wireless vector illustration
Tutorials

Brocade: Health Check Commands

July 12, 2020
A programmer coding
Tutorials

Using Basic Loops In Bash Scripts

July 12, 2020
uname
How to

How to find Linux distribution name and version?

June 22, 2014
Next Post
Linux Command Line tips that every Linux user should know

Linux Command Line tips that every Linux user should know

Comments 6

  1. Tobias says:
    9 years ago

    Something mangled your perl code sample above. “open” is wrong and “$fetchline” assignment is missing entirely.

    Reply
    • Chankey Pathak says:
      9 years ago

      Oops! HTML related problem. The complete code was not visible. I have edited it. Now it is fine. Thanks for letting me know :)

      Reply
  2. shawnhcorey says:
    9 years ago

    You should write a follow up on how to use Text::CSV_XS

    Reply
    • Chankey Pathak says:
      9 years ago

      Yes my next post will extend this one :)

      Reply
  3. Aravind says:
    8 months ago

    Are you still using perl

    Reply
    • Chankey Pathak says:
      8 months ago

      Yes, I still use it to write some quick scripts or oneliners.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Terms and Conditions
  • Contact Us
  • About Us

© 2012 - 2020 Linux Stall - A place for all your Linux needs.

No Result
View All Result
  • Home
  • Android
  • How to
  • Perl
  • Tips
  • Tutorials

© 2012 - 2020 Linux Stall - A place for all your Linux needs.

Welcome Back!

Login to your account below

Forgotten Password?

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In