473,606 Members | 3,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Q: string substitution in a file

Hi,

This is what I have:

#!/usr/bin/perl -w
use strict;

sub replace {
s/one/two/;
}

open (INFILE, "ARGV[1]") || etc...
while (<INFILE>) {
replace ();
print $_;
}

but the file contents do not get replaced from one to two. Any hints?
Jul 19 '05 #1
6 6231
Troll wrote:
Hi,

This is what I have:

#!/usr/bin/perl -w
use strict;

sub replace {
s/one/two/;
}

open (INFILE, "ARGV[1]") || etc... ^^^^ that's not perl
while (<INFILE>) {
replace ();
print $_;
}

but the file contents do not get replaced from one to two. Any hints?


I guess that there is no file ARGV[1]?

--
Kind regards, virtual home: http://johnbokma.com/ ICQ: 218175426
web site hints: http://johnbokma.com/websitedesign/
John I count my toes ~ one to ten ~ I meditate ~ and feel the Zen

Jul 19 '05 #2
It looked fine to my eye, so I tried it. Your version was dying at line 8
where it was trying to read in the argument ARGV[1]; I replaced your open
command with my own way of doing things:

my $file = shift;
open (INFILE, $file) or die "Couldn't open $file: $!";

...and the script worked fine.

[kmc@spire:~]$ cat infile
I have one monkey
I have one manatee
I wish I had one bonobo

[kmc@spire:~]$ test.pl infile.txt
I have two monkey
I have two manatee
I wish I had two bonobo
"Troll" <ab***@microsof t.com> wrote in message
news:7l******** ************@ne ws-server.bigpond. net.au...
| Hi,
|
| This is what I have:
|
| #!/usr/bin/perl -w
| use strict;
|
| sub replace {
| s/one/two/;
| }
|
| open (INFILE, "ARGV[1]") || etc...
| while (<INFILE>) {
| replace ();
| print $_;
| }
|
| but the file contents do not get replaced from one to two. Any hints?
|
|
Jul 19 '05 #3

"John Bokma" <po********@cas tleamber.com> wrote in message
news:10******** *******@halkan. kabelfoon.nl...
Troll wrote:
Hi,

This is what I have:

#!/usr/bin/perl -w
use strict;

sub replace {
s/one/two/;
}

open (INFILE, "ARGV[1]") || etc...

^^^^ that's not perl
while (<INFILE>) {
replace ();
print $_;
}

but the file contents do not get replaced from one to two. Any hints?


I guess that there is no file ARGV[1]?

--
Kind regards, virtual home: http://johnbokma.com/ ICQ: 218175426
web site hints: http://johnbokma.com/websitedesign/
John I count my toes ~ one to ten ~ I meditate ~ and feel the Zen

Sorry John. I could have included more info. The script is run like
$ replace.pl DIR file.txt <<< where DIR is the directory location of the
file and file.txt is the file itself. Both exist.

What I'm trying to do is pass the directory name and the filename as command
line parameters. This file then gets modified ie. one is replaced with two.
Is this clearer?

Sorry there was a typo here. Doesn't this open the file.txt passed thru the
command line?
open (INFILE, "$ARGV[1]") || etc...



Jul 19 '05 #4
*snip*
open (INFILE, "ARGV[1]") || etc... ^^^^ that's not perl
while (<INFILE>) {
replace ();
print $_;
}

but the file contents do not get replaced from one to two. Any hints?


I guess that there is no file ARGV[1]?

--
Kind regards, virtual home: http://johnbokma.com/ ICQ: 218175426
web site hints: http://johnbokma.com/websitedesign/
John I count my toes ~ one to ten ~ I meditate ~ and feel the Zen

Sorry John. I could have included more info. The script is run like
$ replace.pl DIR file.txt <<< where DIR is the directory location of the
file and file.txt is the file itself. Both exist.

What I'm trying to do is pass the directory name and the filename as

command line parameters. This file then gets modified ie. one is replaced with two. Is this clearer?

Sorry there was a typo here. Doesn't this open the file.txt passed thru the command line?
open (INFILE, "$ARGV[1]") || etc...


Actually there's one more line in the script which I omitted. Sorry:

#!/usr/bin/perl -w
use strict;
chdir $ARGV[0]; # changes to the location of the file

sub replace {
s/one/two/;
Jul 19 '05 #5
"Kaynon McChag" <ka***********@ allstream.net> wrote in message
news:9u******** ***********@new s02.bloor.is.ne t.cable.rogers. com...
It looked fine to my eye, so I tried it. Your version was dying at line 8
where it was trying to read in the argument ARGV[1]; I replaced your open
command with my own way of doing things:

my $file = shift;
open (INFILE, $file) or die "Couldn't open $file: $!";

..and the script worked fine.

[kmc@spire:~]$ cat infile
I have one monkey
I have one manatee
I wish I had one bonobo

[kmc@spire:~]$ test.pl infile.txt
I have two monkey
I have two manatee
I wish I had two bonobo
"Troll" <ab***@microsof t.com> wrote in message
news:7l******** ************@ne ws-server.bigpond. net.au...
| Hi,
|
| This is what I have:
|
| #!/usr/bin/perl -w
| use strict;
|
| sub replace {
| s/one/two/;
| }
|
| open (INFILE, "ARGV[1]") || etc...
| while (<INFILE>) {
| replace ();
| print $_;
| }
|
| but the file contents do not get replaced from one to two. Any hints?
|
|

This is odd. I have exactly this:
#!/usr/bin/perl -w
use strict;

sub replace {
s/one/two/g;
}

open (INFILE, "$ARGV[1]") || etc... <<< there was a typo here in the
original post as it had no $
while (<INFILE>) {
replace ();
print $_;
}

I even tried what u suggested Kaynon with no luck:
my $file = shift;
open (INFILE, $file) or die "Couldn't open $file: $!";

My output from print $_ looks like:
two
two
two

but cat test.txt still gives me:
one
one
one

I may need a rest...


Jul 19 '05 #6
I think you are a little confused about what your program is doing. It will
NOT change the original file. It's simply reading the file line by line and
changing the STRING from 'one' to 'two'. If you want the file to change,
you'll have to specifically print the changed lines back to a filehandle.
If your program is printing out a bunch of 'two's, then it is behaving
correctly.
"Troll" <ab***@microsof t.com> wrote in message
news:w7******** ************@ne ws-server.bigpond. net.au...
This is odd. I have exactly this:
#!/usr/bin/perl -w
use strict;

sub replace {
s/one/two/g;
}

open (INFILE, "$ARGV[1]") || etc... <<< there was a typo here in the
original post as it had no $
while (<INFILE>) {
replace ();
print $_;
}

I even tried what u suggested Kaynon with no luck:
my $file = shift;
open (INFILE, $file) or die "Couldn't open $file: $!";

My output from print $_ looks like:
two
two
two

but cat test.txt still gives me:
one
one
one

I may need a rest...

Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6039
by: Mark | last post by:
hello! normally, if you are given binary data in a string (such as from a call to fread), you can call the strlen function on this string to get its size: $buffer = fread($file, 100000); if (strlen($buffer) < 100000) { echo "read less than 100000 bytes"; }
0
1776
by: Justin | last post by:
Hi, First off, I must apologise for cross posting. I am having difficulty creating a pdf document using perl cgi to do substitution for multiline pdf form fields. I created a pdf template/file with substitution variables. The file is then read and variables substituted using pattern matching. When I view the pdf multiline form field using acrobat, the field seems to be truncated. Upon view the properties of the multiline text field, I...
0
1595
by: EJ | last post by:
Is there a way to specify a substitution variable that can be used in several places in the app.config file (like you can do in Ant/NAnt build files)? For example, if there is part of a file path that I specify in several of the appSettings key values, can I somehow specify that path once, and just insert a substitution variable of some sort (e.g., "${path}") and have it get substituted at run-time? I could just separate them into...
12
4631
by: John Leslie | last post by:
I need to write a string to a file in EBCDIC. Do I need to do it character by character using a translation table, or is there a function to translate the whole string? (I am aware that I can convert a whole file using Unix utilities, but this file will have only a few header records in EBCDIC)
18
4607
by: james | last post by:
Hi, I am loading a CSV file ( Comma Seperated Value) into a Richtext box. I have a routine that splits the data up when it hits the "," and then copies the results into a listbox. The data also has some different characters in it that I am trying to remove. The small a with two dots over it and the small y with two dots over it. Here is my code so far to remove the small y: Private Sub Button2_Click(ByVal sender As System.Object, ByVal...
5
4412
by: Murali | last post by:
In Python, dictionaries can have any hashable value as a string. In particular I can say d = {} d = "Right" d = "Wrong" d = "test" In order to print "test" using % substitution I can say
21
2297
by: Hitesh | last post by:
Hi, I get path strings from a DB like: \\serverName\C:\FolderName1\FolderName2\example.exe I am writing a script that can give me access to that exe file. But problem is that string is not universal path, I need to add C$. Any idea how I can add $ char in that string. ServerName is not fixed length. It could be any chars length.
27
10108
by: user | last post by:
Have require file with several query stings in it. Depending on user input one of strings is selected. Everything going along smoothly until I wanted to also input a variable in string. If I put string in program works ok, but, if I use string from require file I can not seem to insert string. $cccb_id is sting..... to be inserted into $query4 and changes depending on user input.
4
12738
by: Michael Yanowitz | last post by:
Hello: If I have a long string (such as a Python file). I search for a sub-string in that string and find it. Is there a way to determine if that found sub-string is inside single-quotes or double-quotes or not inside any quotes? If so how? Thanks in advance: Michael Yanowitz
6
6328
by: Generic Usenet Account | last post by:
I was extremely surprised to learn that the extremely rich C++ string API does not have even a single menthod devoted to string substitution i.e. given a string, replace all instances of pattern-1 in the string with pattern-2. There are API methods for finding and replacing, but none on pattern substitution. Although I have developed an implementation for this (posted to the comp.sources.d newsgroup), does anyone have the background why...
0
8036
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7978
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,...
1
8126
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
8317
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
6796
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
5470
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
3948
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1572
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1313
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.