Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove line in flat data file? 2

Status
Not open for further replies.

bidabob

Technical User
Dec 24, 2005
2
US
I'm trying to remove a line from a flat data file with form input. The file is a list of URLs.
The file is similar to this
Code:
[URL unfurl="true"]www.link1.com[/URL]
[URL unfurl="true"]www.link2.com[/URL]
[URL unfurl="true"]www.link3.com[/URL]
[URL unfurl="true"]www.link4.com[/URL]
[URL unfurl="true"]www.link5.com[/URL]
[URL unfurl="true"]www.link6.com[/URL]
[URL unfurl="true"]www.link7.com[/URL]
[URL unfurl="true"]www.link8.com[/URL]
www.link9.com

I tried the code below to delete a specific link

sub delete_link {
my @lines;
my $line;
my $oldstring = $form{'link'};
chomp $oldstring;
open FILE, "+<$config{'basepath'}$config{'regdir'}/$form{'handle'}.dat" or die ("unable to read: $!");
my @txt = <FILE>;
foreach (@txt){
s/$oldstring//ig;
}
seek(FILE, 0, 0) or die ("unable to seek: $!");
print FILE @txt or die ("unable to print: $!");
truncate(FILE, tell(FILE)) or die ("unable to truncate: $!");
close FILE or die ("unable to close: $!");
}

and it deletes the entry, but leaves a blank line in its place. It leaves a data file like this
Code:
[URL unfurl="true"]www.link1.com[/URL]
[URL unfurl="true"]www.link2.com[/URL]
[URL unfurl="true"]www.link3.com[/URL]
[URL unfurl="true"]www.link4.com[/URL]

[URL unfurl="true"]www.link6.com[/URL]
[URL unfurl="true"]www.link7.com[/URL]
[URL unfurl="true"]www.link8.com[/URL]
www.link9.com

I then added to the subroutine as follows, trying to get rid of the blank line

sub delete_link {
my @lines;
my $line;
my $oldstring = $form{'link'};
chomp $oldstring;
open FILE, "+<$config{'basepath'}$config{'regdir'}/$form{'handle'}.dat" or die ("unable to read: $!");
my @txt = <FILE>;
foreach (@txt){
s/$oldstring//ig;
}
seek(FILE, 0, 0) or die ("unable to seek: $!");
print FILE @txt or die ("unable to print: $!");
truncate(FILE, tell(FILE)) or die ("unable to truncate: $!");
close FILE or die ("unable to close: $!");


open(READFILE,"<$config{'basepath'}$config{'regdir'}/$form{'handle'}.dat");
while(<READFILE>){

next if $line eq '';

push (@lines,$_);
}
close READFILE;

my $lines = join(/\n/,@lines);
open(OUT,">$config{'basepath'}$config{'regdir'}/$form{'handle'}.dat");
print OUT $lines;
close OUT;
}

This code deleted all of the lines, whether blank or not.
I'm at a loss...any help would be appreciated.

The above file should look like this after deletion.
Code:
[URL unfurl="true"]www.link1.com[/URL]
[URL unfurl="true"]www.link2.com[/URL]
[URL unfurl="true"]www.link3.com[/URL]
[URL unfurl="true"]www.link4.com[/URL]
[URL unfurl="true"]www.link6.com[/URL]
[URL unfurl="true"]www.link7.com[/URL]
[URL unfurl="true"]www.link8.com[/URL]
www.link9.com

Thanks, Bob
 
if a newline (\n) came in on $form{'link'}, you've chomped it off in 'chomp $oldstring;' , so it's not there to be substituted in 's/$oldstring//ig;'

would

foreach $line (@txt){
if ($line =~ /$oldstring/i){
$line = '';
}
}

work?
 
Hi automatic2,

That little snippet worked perfectly.
I can't express my appreciation for not
only solving my problem, but doing on
Christmas! Good Holidays to you.

Bob
 
Code:
[b]#!/usr/bin/perl[/b]

$form{'link'} = '[URL unfurl="true"]www.link5.com';[/URL]

foreach (<DATA>) {
  print unless /^$form{'link'}$/;
}

__DATA__
[URL unfurl="true"]www.link1.com[/URL]
[URL unfurl="true"]www.link2.com[/URL]
[URL unfurl="true"]www.link3.com[/URL]
[URL unfurl="true"]www.link4.com[/URL]
[URL unfurl="true"]www.link5.com[/URL]
[URL unfurl="true"]www.link6.com[/URL]
[URL unfurl="true"]www.link7.com[/URL]
[URL unfurl="true"]www.link8.com[/URL]
www.link9.com

Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top