Edit file in place using perl script

K

khan

Hi,

I have a requirement where i need to edit the file, based on some
tokens.
My requirement is something like this:
1. If token1 is found delete that line from the file.
2. If token2 is found replace some part of that line with replacement
string.
3. If token3 is found delete that line and next line in the file.

I wrote a script to do this, but since file-pointer is moved after
reading it is not editing the file at correct positions.
Please let me know a solution.

Thanks,
-Mushtaq Khan
 
T

Tad J McClellan

khan said:
Hi,

I have a requirement where i need to edit the file, based on some
tokens.
My requirement is something like this:
1. If token1 is found delete that line from the file.
2. If token2 is found replace some part of that line with replacement
string.
3. If token3 is found delete that line and next line in the file.

I wrote a script to do this, but since file-pointer is moved after
reading it is not editing the file at correct positions.
Please let me know a solution.


You need to modify line 17 in your program.
 
J

Jürgen Exner

khan said:
I have a requirement where i need to edit the file, based on some
tokens.
My requirement is something like this:
1. If token1 is found delete that line from the file.

next if index ($_, $token1) >= 0
2. If token2 is found replace some part of that line with replacement
string.

s/\Q$somepart\E/$replacementstring/ if index ($_, $token2) >= 0
3. If token3 is found delete that line and next line in the file.

if (index ($_, $token3) >= 0) {
undef = <>; #skip next line
next;
}
I wrote a script to do this,

You have an error on line 42.
but since file-pointer is moved after
reading it is not editing the file at correct positions.
Please let me know a solution.

perldoc -q "delete a line":
How do I change one line in a file/delete a line in a file/insert a
line in the middle of a file/append to the beginning of a file?

jue
 
T

Tim Greer

khan said:
Hi,

I have a requirement where i need to edit the file, based on some
tokens.
My requirement is something like this:
1. If token1 is found delete that line from the file.
2. If token2 is found replace some part of that line with replacement
string.
3. If token3 is found delete that line and next line in the file.

I wrote a script to do this, but since file-pointer is moved after
reading it is not editing the file at correct positions.
Please let me know a solution.

Thanks,
-Mushtaq Khan

Please show the code from the script that you said you have already
created to do this, and people can point out the problem with it.
 
S

smallpond

Hi,

I have a requirement where i need to edit the file, based on some
tokens.
My requirement is something like this:
1. If token1 is found delete that line from the file.
2. If token2 is found replace some part of that line with replacement
string.
3. If token3 is found delete that line and next line in the file.

I wrote a script to do this, but since file-pointer is moved after
reading it is not editing the file at correct positions.
Please let me know a solution.

Thanks,
-Mushtaq Khan

perldoc perlrun

The -i switch explains how to do this
1) rename the original file to file.bak
2) if successful, open a new file for output
3) if successful, read the original file, and write to the new file
4) loop through all lines
5) if successful, unlink file.bak

Using -i saves writing the file and loop code.
 
K

khan

Please show the code from the script that you said you have already
created to do this, and people can point out the problem with it.
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting.  24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!- Hide quoted text -

- Show quoted text -

Hi,
I want change the file without using the temporary file. Following is
script but it is not working

$File = "D:\\PerlProgramming\\Serach-pattren\\file.txt";
open(FILE, "+<",$File) or die "can't open $File: $!";

while (<FILE>) {
if(m/Anand/) {
$_ =~ s/Football/Cricket/s; #Susbst Football with Cricket
print FILE $_;
}
elsif(m/Jack/) {
$_ =~ s/^.*\n$//s; #Delete the Line
print FILE $_;
}
}
close (FILE);
exit;

#Input file: file.txt
Anand Football
Adam Football
Jack Tennis
Jhon Hockey
#Output with the Above script : file.txt
Anand Football
Adam Football
Jack Tennis
Jhon Hockey
Anand Football
Adam Cricket
Jhon Hockey

Script is adding modified data to the end of file, i really didn't
understand the behaviour of the script.
#Output what i was expecting : file.txt should have lines
Anand Football
Adam Cricket
Jhon Hockey

The following script works but it uses a temporary file for storing
modified data.
I think this is not an efficient way to do this.

$File = "D:\\PerlProgramming\\Serach-pattren\\file.txt";
$FileOut = "D:\\PerlProgramming\\Serach-pattren\\fileout.txt";

open(FILE, "+<",$File) or die "can't open $File: $!";
open(OUT, ">",$FileOut) or die "can't open $FileOut: $!";

while (<FILE>) {
if(m/Anand/) {
$_ =~ s/Football/Cricket/s;
print OUT $_;
}
elsif(m/Jack/) {
$_ =~ s/^.*\n$//s; #Delete the Line
print OUT $_;
}
else {
print OUT $_;
}
}
close (FILE);
close (OUT);
system ("del D:\\PerlProgramming\\Serach-pattren\\file.txt /Q");
system ("ren D:\\PerlProgramming\\Serach-pattren\\fileout.txt
file.txt");
exit
 
J

John W. Krahn

khan said:
I want change the file without using the temporary file. Following is
script but it is not working

$File = "D:\\PerlProgramming\\Serach-pattren\\file.txt";
open(FILE, "+<",$File) or die "can't open $File: $!";

while (<FILE>) {
if(m/Anand/) {
$_ =~ s/Football/Cricket/s; #Susbst Football with Cricket

The /s option affects the behaviour of the . character class but you are
not using . in the pattern so the /s is superfluous.
print FILE $_;
}
elsif(m/Jack/) {
$_ =~ s/^.*\n$//s; #Delete the Line
print FILE $_;

If you want to delete the line why are you printing it out?
}
}
close (FILE);
exit;

You are opening the file for input *AND* output ("+<") so you have one
filehandle and one pointer to the data in the file. When you read the
first record the pointer moves to the end of that record and then you
write a record, overwriting the second record in the file and moving the
pointer to the end of what you have just written, and then the next read
starts at that point and moves the pointer, and the next write which
again moves the pointer, etc., etc.

#Input file: file.txt
Anand Football
Adam Football
Jack Tennis
Jhon Hockey
#Output with the Above script : file.txt
Anand Football
Adam Football
Jack Tennis
Jhon Hockey
Anand Football
Adam Cricket
Jhon Hockey

Script is adding modified data to the end of file, i really didn't
understand the behaviour of the script.
#Output what i was expecting : file.txt should have lines
Anand Football
Adam Cricket
Jhon Hockey

The following script works but it uses a temporary file for storing
modified data.
I think this is not an efficient way to do this.

Why do you think that it is not efficient?
$File = "D:\\PerlProgramming\\Serach-pattren\\file.txt";
$FileOut = "D:\\PerlProgramming\\Serach-pattren\\fileout.txt";

open(FILE, "+<",$File) or die "can't open $File: $!";
open(OUT, ">",$FileOut) or die "can't open $FileOut: $!";

while (<FILE>) {
if(m/Anand/) {
$_ =~ s/Football/Cricket/s;
print OUT $_;
}
elsif(m/Jack/) {
$_ =~ s/^.*\n$//s; #Delete the Line
print OUT $_;
}
else {
print OUT $_;
}
}
close (FILE);
close (OUT);
system ("del D:\\PerlProgramming\\Serach-pattren\\file.txt /Q");

perldoc -f unlink
system ("ren D:\\PerlProgramming\\Serach-pattren\\fileout.txt

perldoc -f rename
file.txt");
exit


John
 
X

xhoster

khan said:
Hi,
I want change the file without using the temporary file.

I want a million bucks, peace on Earth, and the ability to fly. That is
unlikely to work out as I want, either.

You could try Tie::File. It will be slow on big files, as your changes are
not length-preserving.

Why not use the temp file? Use the thing that works.

And see perldoc -f seek, where it discusses switching between reading and
writing.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top