0

I have a csv file xyz.csv with contents

name,age,place,phone
xyz,12,ohio,7372829
sed,23,hrh,9890908

I need to parse the csv file and check whether it is a empty file i.e. it doesn't have the values for the headers provided.

a null file xyz.csv would contain just the headers (headers no may decrease or increase) e.g. decreased:

name,age,place,phone

increased:

name,age,place,mob,phno,ht

how do i check for a null file in below code and print whether is is null or not?

i have developed below script to parse the csv

open(my $data, '<', $file_c) or die "Could not open '$file_c' $!\n";

while (my $line = <$data>) 
{
    next if ($. == 1);
    chomp $line; 
    my @fields = split "," , $line; 

    print"$fields[0] fields[1]";
 }
Alex Brown
  • 41,819
  • 10
  • 94
  • 108
hi123
  • 117
  • 2
  • 9
  • You could use a scalar to represent the first line of the file and check if it is defined. However this does advance the filehandle and the rest of your reading will be from the second line on. – squiguy Sep 06 '12 at 06:53
  • @squiguy...how do i do that.....i jus want to skip the header and check next line is null or has any values...can pls advise – hi123 Sep 06 '12 at 06:58
  • You can check if it only contains a newline character or if it is blank. last if not defined $line; last if $line eq "\n"; – squiguy Sep 06 '12 at 07:13
  • pen(my $data, '<', $file) or die "Could not open '$file' $!\n"; while (my $line = <$data>) { next if ($. == 1); my @fields = split "," , $line; print "\n ***** @fields"; if(@fields eq " \n"){ print "empty file"; } its not printng empty file...can you pls chck } – hi123 Sep 06 '12 at 07:37

4 Answers4

2

If I were you I'd look into one of the CSV handling CPAN modules, e.g. Text::CSV, Tie::CSV_File or DBD::CSV.

To check if the file is empty would be a simple case of counting the number of parsed lines in the file. With DBI and DBD::CSV you could use a SELECT COUNT(*) FROM table_name SQL statement.

The following link provides a quick tutorial on parsing CSV files with perl: http://perlmeme.org/tutorials/parsing_csv.html

mttrb
  • 8,297
  • 3
  • 35
  • 57
1

You can use the range operator search for any lines that aren't the first line. This should be pretty efficient:

while (<$data>) 
{
  unless (1..1) { print "not null\n"; exit 0; }
}
print "null\n";
exit 1;

Or you could just pluck the lines off one by one - if the second is defined, then it's not null:

<$data>;
print (defined <$data> ? "not null" : "null");
Alex Brown
  • 41,819
  • 10
  • 94
  • 108
  • Will print "not null" if all lines beyond the header are only \n – Kenosis Sep 06 '12 at 18:55
  • In fact, if even the first one is. RFC4180 states that all lines should have the same number of fields, but the OQ did not specify field number checking, so it's reasonable to read should as 'not must' in which case a blank row is not null. – Alex Brown Sep 06 '12 at 19:56
0

You cound any how check for the number of lines and if you are sure that alwys the header is present.then the first line is always the header.

so if line 2 is not present or length of line2 is 0 then its an empty file.

Vijay
  • 65,327
  • 90
  • 227
  • 319
  • @sarathi...am new to perl...can you pls help me witha sample script for the same check... – hi123 Sep 06 '12 at 07:31
0

This should work.From your shell prompt check whether wc -l < FNAME returns only line cnt.

print "empty" if chomp(my $dummy = `wc -l < FNAME`) == 1;

It may be in-efficient, though.

tuxuday
  • 2,977
  • 17
  • 18