473,471 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Accessing a file and modifying the contents

5 New Member
I am trying to access a file with perl and use substitution on that files contents and print the result of that substitution.

I also need the the file I am accessing to be input on the command line like this: keirnna$ perl_script.pl file.txt

Here is what I have so far:

#!/usr/bin/perl -w

open(INPUT, "+<", "/pretend/path/file.txt")
or die "Couldn't open file..txt for reading: $!\n";
while (<INPUT>) {
$_ =~ s/really/REALLY/gi;
print;
}
close(INPUT);

However all I can get this to do is print the original file and as you can see I am forcing the use of file.txt in the script instead of naming it when I execute the program. I did a lot of searching and reading, but I haven't stumbled upon a solution yet. Sorry if this is a newb question.
Jan 17 '07 #1
5 1610
keirnna
5 New Member
Actually I see now that it is printing the edited file now all I need to do is figure out how to make the unix command: perl_sript.pl text.txt to edit text.txt.
Jan 17 '07 #2
miller
1,089 Recognized Expert Top Contributor
As a one-liner:

Expand|Select|Wrap|Line Numbers
  1. perl -pi -e "s/really/REALLY/gi" /pretend/path/file.txt
  2.  
Jan 17 '07 #3
keirnna
5 New Member
Thanks but I need it to be a script and not a one liner.
Jan 17 '07 #4
miller
1,089 Recognized Expert Top Contributor
Then I would suggest you use two things:

http://perldoc.perl.org/Tie/File.html
http://perldoc.perl.org/perlvar.html - Search for @ARGV

I would write it for you in a couple minutes, but those are all the links that you need and I need to go to bed.

Post if you have any problems.
Jan 17 '07 #5
KevinADC
4,059 Recognized Expert Specialist
since I have nothing to do at the moment:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2.  
  3. my $file = $ARGV[0] || die "no file entered on command line";
  4.  
  5. {
  6.    local @ARGV = ($file);
  7.    local $^I = '.bac';
  8.    while (<>) {
  9.       s/really/REALLY/gi;
  10.       print;
  11.    }
  12. }
  13. print "finished";
  14.  
although it's odd that you are using the "i" option unless you are looking for possible mixed-case matches of "really" instead of all lower-case. "i" tells perl to ignore case in the search pattern.
Jan 20 '07 #6

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

Similar topics

2
by: siliconmike | last post by:
What would happen if PHP script tried to read a .txt file from disk but a cron job is concurrently modifying the same .txt file ? I'm going to use Linux. Thanks Mike
2
by: Generic Usenet Account | last post by:
I am trying to create a Java application that reads a list of URLs from a file and stores their contents on the local file system. I have succeeded in accessing normal websites, but I am unable to...
7
by: Joseph | last post by:
Hi, I'm having bit of questions on recursive pointer. I have following code that supports upto 8K files but when i do a file like 12K i get a segment fault. I Know it is in this line of code. ...
33
by: Jason Heyes | last post by:
I would like to modify the contents of a file, replacing all occurances of one string with another. I wrote these functions: bool read_file(std::string name, std::string &s); bool...
2
by: kelly | last post by:
Hi, I don't have a code to show you, what I need are references or algorithms so that I'm in a right track. By the way, thanks for introducing me the array or arrays. Now I can continue my...
0
by: sonu | last post by:
I have following client side code which i have used in my asp.net project SummaryFeatured Resources from the IBM Business Values Solution Center WHITEPAPER : CRM Done Right Improve the...
3
by: Nathan Sokalski | last post by:
When I attempt to access a Microsoft Access database from my website, I recieve the following error: Server Error in '/' Application....
24
by: allpervasive | last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems in reading and modifying the contents of a file,, hope you can help to solve this problem. Here i attach the file to be...
3
by: FerrisUML | last post by:
Im writing a small web application and what I'd like to do is open a file, read the contents into memory and then close the file as fast as I can to free up the file for the next user. Once the...
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...
0
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...
0
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...
1
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...
0
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.