473,781 Members | 2,491 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

simple find replace

58 New Member
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 1940
numberwhun
3,509 Recognized Expert Moderator Specialist
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 New Member
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_re place.pl line 14, <READ> line 5.
print() on closed filehandle WRITE at D:\temp\find_re place.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 Recognized Expert Moderator Specialist
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 New Member
Thank you sooooo much! that did the trick!
much indebted!
eric
Nov 27 '07 #5
numberwhun
3,509 Recognized Expert Moderator Specialist
Any time! Glad I could help!!!

Regards,

Jeff
Nov 27 '07 #6
KevinADC
4,059 Recognized Expert Specialist
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
1374
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 after the page loads again. Does anyone have simple code that can do this, or know of a place that has this code? I've scoured a bunch of sites, but, because this is probably a simple task, couldn't find any scripts that do exactly this.
2
14934
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 in the format that I need it in order to create the reports that we use. So far this has proven to be successful for the reports that I am doing and the data that I am pulling into it. I just have one challenge that may require a lot of work and I...
9
4406
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 again! I must be doing something wrong but I don't really know how to change my approach to getting it working. For example, if I type this in the vba immediate window: ?replace("HELLO","ELLO","ello")
2
1143
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 parsed_senders = sender = "" for item in senders: if isinstance(item,tuple): item= ''.join(item)
2
8375
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 try and find the answer to this, but I had no luck anywhere. It's a little different situation than I found anywhere else. I would simply put javascript into the <a> tag, but like I said, It's delivered via PHP, and I don't have access to it. I...
7
3772
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 understand it I need a PHP script that pulls the query and dumps the data into XML format, that is called by triggering a javascript event that reads that file with XMLhttprequest.
4
2628
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 = document/disco/album/hello.doc i want it to become : document/disco/album/hello_large.xls
9
1178
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 ":" ( in this example will find both) e.g that function must return this:
1
972
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 location maybe d:\test.txt) I need to search all subdirectories and evertime I find test.txt replace it with the new one from my D:\ Old school dir test.txt /s finds them all for me, but that's as far as dos seems to go!!! Please please help...
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10143
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10076
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9939
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8964
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5507
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4040
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2870
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.