1

I want to write a script that takes a CSV file, deletes its first row and creates a new output csv file.

This is my code:

use Text::CSV_XS;
use strict;
use warnings;
my $csv = Text::CSV_XS->new({sep_char => ','});
my $file = $ARGV[0];
open(my $data, '<', $file) or die "Could not open '$file'\n";
my $csvout = Text::CSV_XS->new({binary => 1, eol => $/});
open my $OUTPUT, '>', "file.csv" or die "Can't able to open file.csv\n";
my $tmp = 0;
while (my $line = <$data>) {
    # if ($tmp==0)
    # {
    # $tmp=1;
    # next;
    # }
    chomp $line;
    if ($csv->parse($line)) {
        my @fields = $csv->fields();
        $csvout->print($OUTPUT, \@fields);
    } else {
        warn "Line could not be parsed: $line\n";
    }
}

On the perl command line I write: c:\test.pl csv.csv and it doesn't create the file.csv output, but when I double click the script it creates a blank CSV file. What am I doing wrong?

daxim
  • 39,270
  • 4
  • 65
  • 132
user1526112
  • 11
  • 1
  • 2

3 Answers3

4

Your program isn't ideally written, but I can't tell why it doesn't work if you pass the CSV file on the command line as you have described. Do you get the errors Could not open 'csv.csv' or Can't able to open file.csv? If not then the file must be created in your current directory. Perhaps you are looking in the wrong place?

If all you need to do is to drop the first line then there is no need to use a module to process the CSV data - you can handle it as a simple text file.

If the file is specified on the command line, as in c:\test.pl csv.csv, you can read from it without explicitly opening it using the <> operator.

This program reads the lines from the input file and prints them to the output only if the line counter (the $. variable) isn't equal to one).

use strict;
use warnings;

open my $out, '>', 'file.csv' or die $!;

while (my $line = <>) {
  print $out $line unless $. == 1;
}
Borodin
  • 126,100
  • 9
  • 70
  • 144
2

Yhm.. you don't need any modules for this task, since CSV ( comma separated value ) are simply text files - just open file, and iterate over its lines ( write to output all lines except particular number, e.g. first ). Such task ( skip first line ) is so simple, that it would be probably better to do it with command line one-liner than a dedicated script.

quick search - see e.g. this link for an example, there are numerous tutorials about perl input/output operations

http://learn.perl.org/examples/read_write_file.html

PS. Perl scripts ( programs ) usually are not "compiled" into binary file - they are of course "compiled", but, uhm, on the fly - that's why /usr/bin/perl is called rather "interpreter" than "compiler" like gcc or g++. I guess what you're looking for is some editor with syntax highlighting and other development goods - you probably could try Eclipse with perl plugin for that ( cross platform ).

http://www.eclipse.org/downloads/

http://www.epic-ide.org/download.php/

this

user@localhost:~$ cat blabla.csv | perl -ne 'print $_ if $x++; '

skips first line ( prints out only if variable incremented AFTER each use of it is more than zero )

Ahk4iePaiv8u
  • 108
  • 1
  • 8
  • Your line of bash probably will not work for @user1526112, as he unfortunately doesn't seem to be using a unixoid system. `perl -ne 'print $_ if $x++;' infile.csv` should do the trick. I don't know much "cmd.exe", but appending `> outfile.csv` might put the output where you want it. – amon Jul 14 '12 at 21:58
0

You are missing your first (and only) argument due to Windows.

I think this question will help you: @ARGV is empty using ActivePerl in Windows 7

Community
  • 1
  • 1
Craig Treptow
  • 834
  • 7
  • 19