Search and Replace a column of the csv file  | Newbie | | Join Date: Dec 2006
Posts: 18
| | |
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.
|  | Newbie | | Join Date: Dec 2006
Posts: 18
| | | re: Search and Replace a column of the csv file
Please find the below code.
$f1 = 'C:\Vasuki\ReplacePerl\chm_commitmt.csv';
open(FILE, "$f1") || die "Could not open file chm_commitmt.csv \n";
$f2= 'C:\Vasuki\ReplacePerl\chm_commitmt_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_new.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.
|  | Moderator | | Join Date: Oct 2006 Location: San Francisco, CA
Posts: 830
| | | re: Search and Replace a column of the csv file
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. http://search.cpan.org/search?query=Text::CSV | | Newbie | | Join Date: Dec 2006
Posts: 2
| | | re: Search and Replace a column of the csv file
Hope this may be help -
-
#!/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 -
#!/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;
-
-
| | Member | | Join Date: Nov 2006
Posts: 83
| | | re: Search and Replace a column of the csv file Quote:
Originally Posted by vmalagi Hope this may be help - #!/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
- 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): - #!/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"/;
-
}
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,353 network members.
|