|
Hii,
I am creating a perl script to read a text and remove the ' at the end of each line only. But I am not getting any sucess. The text file is as follows.
exec ''sp_prepexec'
exec ''sp_prepexec'
exec ''sp_prepexec'
exec ''sp_prepexec'
exec ''sp_prepexec'
Request your help to solve the same in perl.
Thanks in Advance
|
|
|
This script will get the content from the file and edit it as needed.
I like to use file lock( flock() since its good practice ) and sysopen for reading and writing.
# Start
use strict;
use warnings;
use Fcntl qw(:DEFAULT :flock);
# File to edit
my $path_file = '/home/some/path/text.txt';
# get file content
sysopen(FH, $path_file, O_RDONLY) or die($!);
flock(FH, LOCK_EX);
my @content = <FH>;
close(FH);
# Overwrite file with new content
sysopen(FH, $path_file, O_WRONLY | O_TRUNC) or die($!);
flock(FH, LOCK_EX);
foreach my $line (@content) {
if ($line =~ m/\'\n/i) {
# remove ' from the end
$line =~ s/\'\n//g;
# print new stuff
print FH $line, "\n";
}
else {
# no edit needed
print FH $line;
}
}
close(FH);
# End
Any questions?
|
|
|
|
|
|
|
// |