473,385 Members | 1,392 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,385 software developers and data experts.

simple find replace

58
Hey all! been searching around the forum and google this morning looking for a simple way to parse thru a tab delinated file and do a find and replace.

Here is what I've come up with so far as a script (Im still a newbie)

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3.  
  4. my $fileInput='d:/temp/test.txt';
  5. my $fileOutput='d:/temp/Newtest.txt';
  6.  
  7. open (READ, $fileInput);
  8. open (WRITE, $fileOutput);
  9.  
  10. while(<READ>){
  11.     my $line=$_;
  12.     chomp($line);
  13.     $line =~ s/','/'.'/g;
  14.     print WRITE $line ."\n";
  15. }
  16.  
  17. close (READ);
  18. close (WRITE);
and no it does not work.
basically I just want to find all the comma's and replace them with periods
Normally this would be easy to do in textpad but the file is about 2 gb and textpad runs out of memory.
Thanks ahead of time for any thoughts and or direction.
Cheers,
Eric
Nov 27 '07 #1
6 1919
numberwhun
3,509 Expert Mod 2GB
Well, in a regex, you wouldn't need the single quotes and a period is a special character in regex's so you would need to escapee it.

Try this:

Expand|Select|Wrap|Line Numbers
  1. $line =~ s/,/\./g;
  2.  
Also, next time you are parsing and working on a file, please be sure to post a sample of the data so we can see what you are working with.

Regards,

Jeff
Nov 27 '07 #2
erbrose
58
Thanks Jeff,
So changed the code to reflect your input
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3.  
  4. my $fileInput='d:/temp/test.txt';
  5. my $fileOutput='d:/temp/Newtest.txt';
  6.  
  7. open (READ, $fileInput);
  8. open (WRITE, $fileOutput);
  9.  
  10. while(<READ>){
  11.     my $line=$_;
  12.     chomp($line);
  13.     $line =~ s/,/\./g;
  14.     print WRITE $line ."\n";
  15. }
  16.  
  17. close (READ);
  18. close (WRITE);
its still not running. the error I am getting (by running the tool from text pad)
print() on closed filehandle WRITE at D:\temp\find_replace.pl line 14, <READ> line 5.
print() on closed filehandle WRITE at D:\temp\find_replace.pl line 14, <READ> line 6.
etc, etc,etc

the data is tab delineated and looks something like this

1 24/9/2007 00:10:36 0,00 4,6931 48,23323 PL
1 24/9/2007 00:15:36 4,00 4,6931 48,23323 PL
1 24/9/2007 00:20:35 0,00 4,692167 48,23102 PL
1 24/9/2007 06:45:35 0,00 4,692167 48,23102 PL
1 24/9/2007 06:49:39 0,00 4,692167 48,23102 PL
1 24/9/2007 06:49:44 0,00 4,692167 48,23102 PL

the problem is whomever created this file put 'commas' in where the should have been periods.
should look like this
1 24/9/2007 00:10:36 0.00 4.6931 48.23323 PL
1 24/9/2007 00:15:36 4.00 4.6931 48.23323 PL
1 24/9/2007 00:20:35 0.00 4.692167 48.23102 PL
1 24/9/2007 06:45:35 0.00 4.692167 48.23102 PL
1 24/9/2007 06:49:39 0.00 4.692167 48.23102 PL
1 24/9/2007 06:49:44 0.00 4.692167 48.23102 PL

honestly, i don't care if I write to a new file too, just thought it would be easier.
Again thanks ahead of time for your help.
Eric
Nov 27 '07 #3
numberwhun
3,509 Expert Mod 2GB
Try changing the open() functions to be the following:

Expand|Select|Wrap|Line Numbers
  1. open (READ, "<$fileInput");
  2. open (WRITE, ">>$fileOutput");
  3.  
The default action is to read from an open file, that is why you couldn't write to it. You have to specifically open it for writing to it. The above opens the file for appending to.

Regards,

Jeff
Nov 27 '07 #4
erbrose
58
Thank you sooooo much! that did the trick!
much indebted!
eric
Nov 27 '07 #5
numberwhun
3,509 Expert Mod 2GB
Any time! Glad I could help!!!

Regards,

Jeff
Nov 27 '07 #6
KevinADC
4,059 Expert 2GB
The dot is just a dot on the replacement side of a substitution regexp:

Expand|Select|Wrap|Line Numbers
  1. $_ = 'this,is,test';
  2. s/,/./g;
  3. print;
it is a wild card match only on the search (pattern) side. It is alo a wild card when used in a matching regexp because there is only a search side.
Nov 27 '07 #7

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

Similar topics

4
by: Monty | last post by:
Hi, I have the following in my HTML document... <span id='prompt'>File Saved</span> All I want to do is replace the above "File Saved" text on my page with "Upload File" after about 5 seconds...
2
by: Daniel | last post by:
I use an Access database to basically take data exports, import them, manipulate the data, and then turn them into exportable reports. I do this using numerous macros, and queries to get the data...
9
by: Not Me | last post by:
Hi, I'm having bother with the replace function in access 2002, a while back I remember not being able to get it to work... then suddenly the next time I tried it did work. Now it doesn't...
2
by: Kun | last post by:
hey guys, here's my code, senders = {46}', 'From: Friend <anon@anon.wharton.com>\r\n\r\n'), ')', ('462 (BODY {37}', 'From: Kun <neurogasm@gmail.com>\r\n\r\n'), ')'] print senders...
2
by: Evan | last post by:
Hey, I posted this yesterday, but no one had any ideas? C'mon now, I know this isn't that hard, i'm just a little new to javascript, and I can't quite figure this out. I searched and searched to...
7
by: Ivan Marsh | last post by:
Hey Folks, I'm having a heck of a time wrapping mind around AJAX. Anyone know of a simple, straight-forward example for pulling a simple query from mysql with PHP using AJAX? As I...
4
by: SM | last post by:
Hello, I have a simple question, but can't find the answer. I have a string that contains a path to a file I want to add another string to the end of that string So, if i have : path =...
9
by: vedrandekovic | last post by:
Hello, I have one question about string.I am trying to make an function to analyze line of some text, this is my example: "HELLO;HELLO2:WORLD:", if that function in this text find ";" and ":" (...
1
by: lmarmstrong | last post by:
Hello, this is probably nothing to you seasoned guys I want to search the whole c:\ of a computer to find all instances of a file (i.e test.txt) and replace that file with one from a different...
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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.