473,805 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
+ 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 1637
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
1405
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
3597
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 access the secured websites (using the HTTPS protocol) using this approach. I would greatly appreciate if someone could suggest a way out. I have looked at the HttpsURLConnection class, but unfortunately this class is abstract.
7
3542
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. How do i make the last pointer in the indirect sector that has another level of indirect pointer, and be defined recursively to support infinite large files? -code-
33
2906
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 write_file(std::string name, const std::string &s); void find_replace(std::string &s, std::string first, std::string second); bool find_replace_file(std::string name, std::string first, std::string second) {
2
15996
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 script. Now I want to delete a line from a file. Line being the strings I got/saved to/from array of arrays.
0
12090
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 likelihood of CRM success from less than 20 percent to 60 percent. WHITEPAPER :
3
5648
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. -------------------------------------------------------------------------------- Security Exception Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the...
24
2994
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 modified and the program code. In the attached file below i just want to change the value of data(only float value) after the line 1 P V T 1 15 till 2 G TT, from positive to negative and vice versa, and wire the date in other file. can someone help...
3
1478
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 file is loaded into memory and subsequently closed, i would use the contents of the file (now loaded into memory) to perform some quick processing. Any idea how best to do this? Thanks in advance for your help!
0
9716
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...
1
10364
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
10104
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
9182
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...
1
7645
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6875
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
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3843
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3007
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.