Home > Archive > PERL Beginners > March 2008 > How to replace one line in a text file with Perl?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to replace one line in a text file with Perl?
|
|
|
| Hi all,
I have a file that looks like:
*/tmp/dst/file1*234*RW*6790
*/tmp/dst/file2*568*RW*908
*/tmp/dst/file3*345*RW*746
*/test/flm/file4*354*RW*987
*/test/flm/file5*643*RW*645
I need to keep all of the lines with the exception of the one with
file1 and file3 which have in common "/tmp/dst". I need to preserve
the line /tmp/dst/file2 plus the others that start with */test
Any ideas on how to do this with perl?
Thanks,
BTNA
| |
| Gunnar Hjalmarsson 2008-03-26, 7:11 pm |
| btna wrote:
> I have a file that looks like:
>
> */tmp/dst/file1*234*RW*6790
> */tmp/dst/file2*568*RW*908
> */tmp/dst/file3*345*RW*746
> */test/flm/file4*354*RW*987
> */test/flm/file5*643*RW*645
>
> I need to keep all of the lines with the exception of the one with
> file1 and file3 which have in common "/tmp/dst". I need to preserve
> the line /tmp/dst/file2 plus the others that start with */test
>
> Any ideas on how to do this with perl?
Check out the Tie::File module.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
| |
| Sanket Vaidya 2008-03-27, 4:16 am |
|
Here is the entire code to accomplish your task. It will delete 1st & 3rd
lines.
use warnings;
use strict;
my @array;
open FH,"data.txt";
@array = <FH>;
for my $i (0..$#array)
{
$array[$i] =~ s/^(\*\/tmp\/dst\/file(1|3)\*(\d){3}\*RW\*(\d){3,4})$/ /;
#replace the lines you want to delete with " " (space)
}
my @result = grep(/[^\s]/,@array); #Take other lines in @result.
close FH;
open FH,">data.txt";
print FH @result;
Hope it is useful.
-----Original Message-----
From: btna [mailto:btna@terra.com]
Sent: Wednesday, March 26, 2008 9:21 PM
To: beginners@perl.org
Subject: How to replace one line in a text file with Perl?
Hi all,
I have a file that looks like:
*/tmp/dst/file1*234*RW*6790
*/tmp/dst/file2*568*RW*908
*/tmp/dst/file3*345*RW*746
*/test/flm/file4*354*RW*987
*/test/flm/file5*643*RW*645
I need to keep all of the lines with the exception of the one with
file1 and file3 which have in common "/tmp/dst". I need to preserve
the line /tmp/dst/file2 plus the others that start with */test
Any ideas on how to do this with perl?
Thanks,
BTNA
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
| |
| Sanket Vaidya 2008-03-27, 4:16 am |
|
Here is the entire code to accomplish your task. It will delete 1st & 3rd
lines.
use warnings;
use strict;
my @array;
open FH,"data.txt";
@array = <FH>;
for my $i (0..$#array)
{
$array[$i] =~ s/^(\*\/tmp\/dst\/file(1|3)\*(\d){3}\*RW\*(\d){3,4})$/ /;
#replace the lines you want to delete with " " i.e. (space)
}
my @result = grep(/[^\s]/,@array); #Take other lines in @result.
close FH;
open FH,">data.txt";
print FH @result;
Hope it is useful.
-----Original Message-----
From: btna [mailto:btna@terra.com]
Sent: Wednesday, March 26, 2008 9:21 PM
To: beginners@perl.org
Subject: How to replace one line in a text file with Perl?
Hi all,
I have a file that looks like:
*/tmp/dst/file1*234*RW*6790
*/tmp/dst/file2*568*RW*908
*/tmp/dst/file3*345*RW*746
*/test/flm/file4*354*RW*987
*/test/flm/file5*643*RW*645
I need to keep all of the lines with the exception of the one with
file1 and file3 which have in common "/tmp/dst". I need to preserve
the line /tmp/dst/file2 plus the others that start with */test
Any ideas on how to do this with perl?
Thanks,
BTNA
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
| |
| John W. Krahn 2008-03-27, 8:02 am |
| [ Please do not top-post your replies. TIA ]
sanket vaidya wrote:
> Here is the entire code to accomplish your task. It will delete 1st & 3rd
> lines.
>
> use warnings;
> use strict;
>
> my @array;
> open FH,"data.txt";
You should *always* verify that the file opened correctly:
open FH, '<', 'data.txt' or die "Cannot open 'data.txt' $!";
> @array = <FH>;
my @array = <FH>;
> for my $i (0..$#array)
> {
> $array[$i] =~ s/^(\*\/tmp\/dst\/file(1|3)\*(\d){3}\*RW\*(\d){3,4})$/ /;
If you have a lot of / characters in the pattern you should probably use
a delimiter other than /. A character class is better for single
character alternatives then using alternation and you don't use the
captured strings so you don't need capturing parentheses.
$array[$i] =~ s!^\*/tmp/dst/file[13]\*\d{3}\*RW\*\d{3,4}$! !;
> #replace the lines you want to delete with " " (space)
> }
> my @result = grep(/[^\s]/,@array); #Take other lines in @result.
[^\s] could be written more simply as \S.
Or you could just remove the elements from the original array:
for my $i ( reverse 0 .. $#array ) {
splice @array, $i, 1
if $array[$i] =~ m!^\*/tmp/dst/file[13]\*\d{3}\*RW\*\d{3,4}$!;
}
> close FH;
>
> open FH,">data.txt";
You should *always* verify that the file opened correctly:
open FH, '>', 'data.txt' or die "Cannot open 'data.txt' $!";
> print FH @result;
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
|
|
|
|
|