473,378 Members | 1,388 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Search and Replace a column of the csv file

Vasuki Masilamani
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.
Dec 13 '06 #1
4 8691
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.
Dec 13 '06 #2
miller
1,089 Expert 1GB
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
Dec 14 '06 #3
Hope this may be help

Expand|Select|Wrap|Line Numbers
  1.  
  2. #!/usr/bin/perl
  3.  
  4. open(FILE,"test.csv");
  5. open(OUTFILE, "newfile.csv");
  6.  
  7. while($line=<FILE>){
  8.  
  9.   chomp($line);
  10.   $line =~ s/(.*)?\,(.*)?\,(.*)?\,(.*)?\,(1)(.*)/$1\,$2\,$3\,$4\,1030$6/g;
  11.   print OUTFILE $line ."\n";
  12. }
  13.  
  14. close FILE, OUTFILE;
  15.  
  16.  
One more way to do this

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. open(FILE,"test.csv") or die $!;
  4. open(TARGET,"> new.csv") or die $!;
  5.  
  6. while($line = <FILE>){
  7.  
  8.   chomp($line);
  9.   next unless($line =~ m/\S+/);
  10.  
  11.   @array = split(",",$line);
  12.  
  13.   if($array[4] == 1){
  14.     $array[4] = 1035;
  15.   }
  16.  
  17.   $line = join(",",@array);
  18.  
  19.   print TARGET $line ."\n";
  20. }
  21. close FILE;
  22. close TARGET;
  23.  
  24.  
Dec 15 '06 #4
GunnarH
83
Hope this may be help
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. open(FILE,"test.csv");
  4. open(OUTFILE, "newfile.csv");
  5.  
  6. while($line=<FILE>){
  7.  
  8.   chomp($line);
  9.   $line =~ s/(.*)?\,(.*)?\,(.*)?\,(.*)?\,(1)(.*)/$1\,$2\,$3\,$4\,1030$6/g;
  10.   print OUTFILE $line ."\n";
  11. }
  12.  
  13. close FILE, OUTFILE;
  14.  
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):
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. my $f1 = 'C:\Vasuki\ReplacePerl\chm_commitmt.csv';
  6. my $f2 = 'C:\Vasuki\ReplacePerl\chm_commitmt_new.csv';
  7.  
  8. open my $file1, '<', $f1 or die "Couldn't open $f1: $!";
  9. open my $file2, '>', $f2 or die "Couldn't open $f2: $!";
  10.  
  11. while (<$file1>) {
  12.     print $file2 $_ if s/^((?:"[^,]*",){4})"1"/$1"1030"/;
  13. }
Dec 15 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Jane Doe | last post by:
Hi, I need to search and replace patterns in web pages, but I can't find a way even after reading the ad hoc chapter in New Rider's "Inside JavaScript". Here's what I want to do: function...
0
by: DaveJG | last post by:
I have a couple of questions involving the search/replace in the IDE and regular expressions. Any help would be greatly appreciated. 1. How would I replace two line feeds with one? That is ...
6
by: joltman | last post by:
I am trying to find out how to search for a file. I am making a switchboard program with buttons pointing to programs, but I want to make it so that if the program a button points to ever moves, it...
1
by: Andrew | last post by:
Hi, im trying to create a small function which can create a binary tree from the entries in a text file and after that we can perform all the usual operations like search, insert and delete etc....
2
by: Michael Peters | last post by:
is there a way to replace a certain sequence of characters by line feed (vbCrLf ), for all text columns in a table, using Search+Replace? -Michael
3
by: mse07 | last post by:
hi all i search about statement in text file by this code : LineFlag = LineFlag + 1 Line Input #1, strLine If LineFlag = 15 Then If InStr(1, strLine, " Transfer completed...
13
by: ravindarjobs | last post by:
hello friends, i want to seach for a file in the computer. i dont know the exact location where it is. so the requirement is to search entire hard disk and get the path of the specified file. ...
2
by: yxq | last post by:
Hello, I want to update A program file from A. First startup A, then startup B process from A, close A, B will copy new file to replace A file. But system states that A is using. A code:...
3
by: Sabine | last post by:
Hi there, I would need your help again. I am using Dev Avish's API function "Search for a file" http://www.mvps.org/access/api/api0006.htm to find a file on my computer which works well. As...
1
by: ahmed222too | last post by:
i use vb6 i want to make a backword search in text file i mean (by backword search) to begin the search from the last for example (i will go to school to learn more....) i want to find the last...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.