ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
i still a newbie of perl script, but want to perl script to make my work easier.
command:
hack.pl input.txt target.txt
,where input.txt is user input file, eg the net to be replaced etc
target.txt is the file i want to modify
method #1,
-read in input.txt & process
-read in target.txt & save all the lines into memory
-read line by line & make changes in the memory if matched.
-after that, print to another file target.txt.hack
method #2
-read in input.txt & process
-copy target.txt tp target.txt.hack
-read the target.txt.hack line by line
-no need to save all the line into memory
-modify if matched
(same principle as like user modify in vi)
which one is better? Is the method #2 doable?
could perl script modidy the file directly? i saw one similar example, it uses method #1
I believe in method 1 you're talking of producing a copy of the contents of the target.txt file into memory, and then your substitutions as dictated by your input.txt are performed to this copy, and then the result is written to target.txt.hack.
I fail to see how this is modifying the file directly, and how this matches with your similar example statement where this method was used.
Method 2 seems to require a mapping mechanism as Sergei provides, and then I'd question why you'd only map one line at a time, as most systems using Perl would surely be capable of mapping more/all of the file, even if your substitutions are performed per line.
I'm going to question the need to edit the file directly. IIRC under Linux, any process that reads a file will recieve a copy of the contents, and your changes will not be automatically picked up unless the other program is monitoring for alteration timestamp changes or actual file contents changes. You seem to describe producing a second file, and no mention of replacing the first with the second.
I'll also mention you might want to consider how any subsequent program will know when you're done modifying the .hack file, especially with the 2nd/mapped method. With the 1st method, you'd need only consider if the file exists in the filesystem before all its contents is readable by someone immediately opening it, whether if it starts empty and grows or won't be listed until all output is written.
AFAIK the usual method is write out to a tmp file, then lastly rename to the filename you wish others to use.
Last edited by Proud; 08-03-2011 at 05:58 AM.
Reason: and->any
I accept with method Two will be the most efficient way.But in Perl method one is possible but it may fail in some cases.
open(FILE,"+<hello.dat");
# "+<" option has to be used for read and writing to the file
@lines=<FILE>;
foreach(@lines)
{
chop;
print; # it is same as print $_;
print "\n";
}
print FILE "\n"."karthik";
close(FILE)
This program can help you to understand better.
The file examples in the site:[url]http://linux-forum-karthik.blogspot.com
can give you a better understanding.
this is about method #1 (simplified one)
is it the common way of writing perl?
Code:
system(clear);
open($INPUT, $ARGV[0]) || die('No input_net file');
@INPUT_FILE=<$INPUT>; #save the entire file into memory
$INPUT_LINE=@INPUT_FILE; #total line
close($INPUT);
$line=0;
while($line < $INPUT_LINE){
do_something...and change the content in @INPUT_FILE
$line++;
}
my $outfile = "$ARGV[0]".".hack" ;
open( OUTFILE, "> $outfile" ) || die("Can't open file for write.\n");
#print out all after the all changes
foreach(@INFILE)
{
print(OUTFILE "$_\n");
}
close(OUTFILE);
this is about method #1 (simplified one)
is it the common way of writing perl?
Code:
system(clear);
open($INPUT, $ARGV[0]) || die('No input_net file');
@INPUT_FILE=<$INPUT>; #save the entire file into memory
$INPUT_LINE=@INPUT_FILE; #total line
close($INPUT);
$line=0;
while($line < $INPUT_LINE){
do_something...and change the content in @INPUT_FILE
$line++;
}
my $outfile = "$ARGV[0]".".hack" ;
open( OUTFILE, "> $outfile" ) || die("Can't open file for write.\n");
#print out all after the all changes
foreach(@INFILE)
{
print(OUTFILE "$_\n");
}
close(OUTFILE);
It is easier to edit. (i know this method, at least the test case work if i load into memory since i edit from memory)
Besides this, I have tried other method which writing to a new file but it does not work
Flow:
-Open input file
-read input file line by line #step2
-open target file
-read target line by line
-if matching --> do replacement
-print the line into a new file
-repeat the step 2
## 2nd iterations
-read input file line by line
-open target file
-read target line by line
-if matching -->do replacement
-print the line in to a new file (the problem is here , it only changes the current edit, the previous changes is not written)
-repeat the step2
(the flow is the simplified one, I use simple replacement as test case.)
I think i got to overwrite target file after printing a new file.
Last edited by casualzone; 08-04-2011 at 11:51 PM.
I still need to improve the flow because my script for my work need some manipulations rather than simple substitution.
For simple substitution, i can manage to achieve by using vi, instead of perl script.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.