?
Solved

replace }else{ append to text file...?

Posted on 2002-03-14
7
Medium Priority
?
141 Views
Last Modified: 2010-03-05
I'm trying to read a text file, each line being a record with a unique name at the beginning, and comparing to a new input line. Based on whether the name of the record finds a match or not, either replace the matching line, otherwise append that line to the end of the file. Also to delete a line if indicated.

I've played with some code I found, but the results are either it duplicates the lines read in and appends them all, or it just writes the same (new) line out for every record.

I won't muck things up with the code I've started with, I want a fresh start, but basically it needs to look like:

file content example:
name=value|value|value|value|...continues....
monkey=something|anotherthing|anything|........
bear=problem|anything|manythings|duplicate|tree|.......
harvey=speaker|duplicate|something|........


$saveline = $ENV{QUERY_STRING}  # new line input
  open the existing file.
  read each line already there.
  compare 'name' value of $saveline to file records.
  if names match AND value='delete' kill this record.
    }elsif{
    names match AND value!='delete' replace the older record with the new one.
    }else{
    leave the older record as-is.
  }
  write all records back out to the file.
  close file.

So (using data above) if $saveline is:  
buick=old|blue|something|quite|carpet|........
it would append as a new record.

But if $saveline is:
bear=pink|orange|blue|green|red|......
it would overwrite the existing 'bear' line.

Or if $saveline is:
bear=delete|
this record is removed from the file.

Hope I'm making sense here, sorry for the weird thumbnail layout, but I'm in a hurry, I'm at work and have no reference material in front of me.

thanks!
0
Comment
Question by:nascar_3
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 6866574
Does the order of the lines in the file matter?
0
 

Author Comment

by:nascar_3
ID: 6866669
No, there is nothing depending on the sort order.
0
 
LVL 84

Accepted Solution

by:
ozo earned 800 total points
ID: 6866783
$oldfile = "oldfilename";
$newfile = "tempfile.$$";
$saveline = $ENV{QUERY_STRING} || "bear=pink|orange|blue|green|red\n";
open O,"<$oldfile" or die "can't read $oldfile because $!";
open N,">$newfile" or die "can't write $newfile because $!";
chomp $saveline;
$saveline .= $/;
($name,$value) = (split/\|/,$saveline)[0,1];
if( $value eq "delete" ){ $saveline = '';
while( <O> ){
    $oldname=(split/\|/)[0];
    if( $oldname eq $name ){
     print N $saveline;
        $saveline = '';
    }else{
     print N;
    }
}
print N $saveline;
close N or die "Can't close $!";
close O;
rename $newfile,$oldfile or die "Can't rename $!";

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 6866787
#sorry, that should have been
($name,$value) = (split/[=|]/,$saveline)[0,1];
0
 

Author Comment

by:nascar_3
ID: 6867052
thanks ozo-

I'll play with this later when I'm home, but a couple of questions:

Could you explain a little bit about what these 2 lines are doing?
chomp $saveline;
$saveline .= $/;

Why the []and | in the split function?
I would have thought just (split/=/,$saveline)
($name,$value) = (split/[=|]/,$saveline)[0,1];


0
 
LVL 84

Expert Comment

by:ozo
ID: 6867087
chomp $saveline;
$saveline .= $/;
#I wanted to make sure $saveline ended with one input  record separator
split/=/,$saveline would be fine, except that you then have to compare $value eq "delete|\n"
0
 

Author Comment

by:nascar_3
ID: 6867780
Thanks ozo!

I pretty much just plugged it in and it worked!
I needed to add : $saveline=~ s/%20/ /g;
because my form was passing the spaces as '%20', but then it performed just as I wanted it to.

Much Appreciated!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question