Search and Replace a column of the csv file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vasuki Masilamani
    New Member
    • Dec 2006
    • 18

    Search and Replace a column of the csv file

    Hi,

    I have a csv file which has "1" in the column 5. I need to replace all "1" in column 5 to "1030".

    The Pattern which would search for "1" in 5th column is given below.

    '^"[^,]*","[^,]*","[^,]*","[^,]*","1"'

    Can anyone help me with a Search and Replace step for this in Perl Script?

    Any solution will be helpful.

    Thanks,
    Vasuki.
  • Vasuki Masilamani
    New Member
    • Dec 2006
    • 18

    #2
    Please find the below code.

    $f1 = 'C:\Vasuki\Repl acePerl\chm_com mitmt.csv';

    open(FILE, "$f1") || die "Could not open file chm_commitmt.cs v \n";

    $f2= 'C:\Vasuki\Repl acePerl\chm_com mitmt_new.csv';

    my @outLines1;

    while($data = <FILE>)

    {
    if($data =~ '^"[^,]*","[^,]*","[^,]*","[^,]*","1"')

    {

    $data =~ s/^"[^,]*","[^,]*","[^,]*","[^,]*","1"/"[^,]*","[^,]*","[^,]*","[^,]*","1030"/g;

    push(@outLines, $data);

    }

    }

    open(FILE2, ">$f2") || die "Could not open file chm_commitmt_ne w.csv \n";

    print (FILE2 @outLines);

    close (FILE);

    close (FILE2);

    undef( @outLines );


    This code not properly replacing 1 in 5th column to 1030. Can anyone correct the mistake in this??


    Thanks,
    Vasuki.

    Comment

    • miller
      Recognized Expert Top Contributor
      • Oct 2006
      • 1086

      #3
      For easy parsing of CSV files, use this CPAN module or any of the other ones freely available. This will simplify your code and avoid any needless bugs.

      Comment

      • vmalagi
        New Member
        • Dec 2006
        • 2

        #4
        Hope this may be help

        Code:
        #!/usr/bin/perl
        
        open(FILE,"test.csv");
        open(OUTFILE, "newfile.csv");
        
        while($line=<FILE>){
        
          chomp($line);
          $line =~ s/(.*)?\,(.*)?\,(.*)?\,(.*)?\,(1)(.*)/$1\,$2\,$3\,$4\,1030$6/g;
          print OUTFILE $line ."\n";
        }
        
        close FILE, OUTFILE;
        One more way to do this

        Code:
        #!/usr/bin/perl
        
        open(FILE,"test.csv") or die $!;
        open(TARGET,"> new.csv") or die $!;
        
        while($line = <FILE>){
        
          chomp($line);
          next unless($line =~ m/\S+/);
        
          @array = split(",",$line);
        
          if($array[4] == 1){
            $array[4] = 1035;
          }
        
          $line = join(",",@array);
        
          print TARGET $line ."\n";
        }
        close FILE;
        close TARGET;

        Comment

        • GunnarH
          New Member
          • Nov 2006
          • 83

          #5
          Originally posted by vmalagi
          Hope this may be help
          Code:
          #!/usr/bin/perl
          
          open(FILE,"test.csv");
          open(OUTFILE, "newfile.csv");
          
          while($line=<FILE>){
          
            chomp($line);
            $line =~ s/(.*)?\,(.*)?\,(.*)?\,(.*)?\,(1)(.*)/$1\,$2\,$3\,$4\,1030$6/g;
            print OUTFILE $line ."\n";
          }
          
          close FILE, OUTFILE;
          I doubt that that buggy code is at all helpful. There is about one error or other kind of obvious shortcoming per line:
          • No check of the return value from open
          • Opening OUTFILE in read mode, even if it's intended for writing
          • Unnecessary escaping of commas in both PATTERN and REPLACEMENT
          • Unnecessary /g modifier
          • Assuming that each column is delimited with dubbelquotes (which is indicated by Vasuki's pattern), the PATTERN does not match
          • Incorrect use of the close() function


          This is a better attempt (I hope):
          Code:
          #!/usr/bin/perl
          use strict;
          use warnings;
          
          my $f1 = 'C:\Vasuki\ReplacePerl\chm_commitmt.csv';
          my $f2 = 'C:\Vasuki\ReplacePerl\chm_commitmt_new.csv';
          
          open my $file1, '<', $f1 or die "Couldn't open $f1: $!";
          open my $file2, '>', $f2 or die "Couldn't open $f2: $!";
          
          while (<$file1>) {
          	print $file2 $_ if s/^((?:"[^,]*",){4})"1"/$1"1030"/;
          }

          Comment

          Working...