Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Edit file in place using perl script

Reply
Thread Tools

Edit file in place using perl script

 
 
khan
Guest
Posts: n/a
 
      10-17-2008
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

 
Reply With Quote
 
 
 
 
Tad J McClellan
Guest
Posts: n/a
 
      10-17-2008
khan <(E-Mail Removed)> wrote:
> 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.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      10-17-2008
khan <(E-Mail Removed)> wrote:
>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
 
Reply With Quote
 
Tim Greer
Guest
Posts: n/a
 
      10-17-2008
khan wrote:

> 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.
--
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!
 
Reply With Quote
 
smallpond
Guest
Posts: n/a
 
      10-17-2008
On Oct 17, 8:49 am, khan <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
khan
Guest
Posts: n/a
 
      10-20-2008
On Oct 17, 10:37*pm, Tim Greer <(E-Mail Removed)> wrote:
> khan wrote:
> > 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.
> --
> 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

 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      10-20-2008
khan wrote:
>
> On Oct 17, 10:37 pm, Tim Greer <(E-Mail Removed)> wrote:
>>
>> khan wrote:
>>>
>>> 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.

>>
>> 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.

>
> 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
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      10-20-2008
khan <(E-Mail Removed)> wrote:
>
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in passing values to perl script using another perl script vikrant Perl Misc 4 08-10-2005 06:20 PM
Snapshot restraint - edit, edit, edit Alan Browne Digital Photography 24 05-10-2005 10:15 PM
Snapshot restraint - edit, edit, edit Patrick Digital Photography 0 05-06-2005 10:53 PM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM
How to make Perl Script "POST" call from another Perl Script??? Wet Basement Perl 1 07-15-2003 10:25 PM



Advertisments