Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    removing blank row from csv file using PERL

    hi all,
    I am trying to remove the first row which is blank from a csv file and output to another csv file.
    i am able to remove the line however when opening the file it is no longer comma delimited. attached is the code below:

    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    my $infile = 'rave-people_in.csv';
    my $outfile = 'rave-people.csv';
    $. = 0;
    open(my $fh, '<:encoding(UTF-8)', $infile)
    or die "Could not open file '$infile' $!";
    open(my $fh2, '>:encoding(UTF-8)', $outfile) or die "Could not open file '$outfile' $!";
    while (<$fh>) {
    
    my $line = $_;
    my @row;
    @row = split( /,/, $line);
    print $line;
    if ($. > 1){
    	print $fh2 $line;
    $.++;
    }
    }
    close ($infile);
    close ($outfile);
    any insight would be greatful.
    thanks in advance.

    leon
    Last edited by FishMonger; Oct 31st, 2018 at 09:02 PM. Reason: Added code tags

  2. #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    3,362
    Thanks
    2
    Thanked 234 Times in 227 Posts
    Why are you creating the @row array but never use it?

    Why are you manually assigning/incrementing $.? That's a global var which perl will automatically increment as you loop over the file.

    If you need to skip the first line, then the easiest method would be to read it in void context before the while loop. Also, please use proper indentation and the code tags.

    Code:
    #!/usr/bin/perl
    
    use strict;
    use warnings;
    use autodie;  # replaces your "or die" clauses on the open calls
    
    my $infile  = 'rave-people_in.csv';
    my $outfile = 'rave-people.csv';
    
    open(my $fh,  '<:encoding(UTF-8)', $infile);
    open(my $fh2, '>:encoding(UTF-8)', $outfile);
    
    <$fh>;  # throw away the 1st line
    while (my $line = <$fh>) {
        print $line;
        print $fh2 $line;
    }
    
    close ($infile);
    close ($outfile);

  3. #3
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok thank you for your criticism. however what i need is assistance.
    the @row was there as i was trying to do different things and forgot to remove it. what is happening is that while i can remove the lines at the top, when writing to the outfile csv, it is not delimiting the data and placing it in one cell. can anyone give advice on how to correctly remove the top line of a csv file while keeping the csv format?

  4. #4
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    3,362
    Thanks
    2
    Thanked 234 Times in 227 Posts
    The code I posted will do exactly what you say you want and the code you posted will also. If you are seeing different results, then you must be running a different version of the code.
    Code:
    [email protected]:~$ cat lparker.pl 
    #!/usr/bin/perl
    use strict;
    use warnings;
    my $infile = 'rave-people_in.csv';
    my $outfile = 'rave-people.csv';
    $. = 0;
    open(my $fh, '<:encoding(UTF-8)', $infile)
    or die "Could not open file '$infile' $!";
    open(my $fh2, '>:encoding(UTF-8)', $outfile) or die "Could not open file '$outfile' $!";
    while (<$fh>) {
    
    my $line = $_;
    my @row;
    @row = split( /,/, $line);
    print $line;
    if ($. > 1){
    	print $fh2 $line;
    $.++;
    }
    }
    close ($infile);
    close ($outfile);
    
    [email protected]:~$ cat rave-people.csv
    cat: rave-people.csv: No such file or directory
    [email protected]:~$ cat rave-people_in.csv
    
    sample,csv,data
    sample,csv,data
    sample,csv,data
    sample,csv,data
    [email protected]:~$ ./lparker.pl 
    
    sample,csv,data
    sample,csv,data
    sample,csv,data
    sample,csv,data
    [email protected]:~$ cat rave-people.csv
    sample,csv,data
    sample,csv,data
    sample,csv,data
    sample,csv,data
    [email protected]:~$
    If that is not the result you want, then you need to provide more info on what you are trying to accomplish and include your test csv file as well as the desired output to be put into rave-people.csv.
    Last edited by FishMonger; Nov 1st, 2018 at 05:12 PM.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •