[Q] Search/replace within text file

I am working on a script to open up a semi-database -- actually a text file set up like this:
    value 1a <TAB> value 2a <TAB> value 3a
    value 1b <TAB> value 2b <TAB> value 3b

Once a certain action is performed upon one of the database entries (i.e. value 1b, 2b, 3b), I would like to delete that line from the text file. The first value on each line is unique within the whole file, replication of that first value is not a concern in writing the function.

When I import each line into the script, I set it up as such:
        $fields[0]
        .
        .
        $fields[n]

So, if (based on my first example) I was done with the line containing values 1b, 2b and 3b, I would like the entire second row deleted or replaced by a series of empty spaces.
Any help would be immensely appreciated.

Thanks
Adam Grayson
DyrewolfAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ahoffmannCommented:
$match='1b';
open(FID,"<text_file");
while(<FID>) {
   if (grep( /^$match/, $_ )) { continue; }
   $count++;
   $db[$count]=$_;
}

This examples reads your file into array $db, ignoring lines beginning with $match.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ozoCommented:
you should usually check your open statements to be sure they succeed.
grep is usually used on a list, not a scalar.
As written the regular expression needs to be recompiled for each line of <FID>.
In Perl, "continue" is usualy spelled "next".
$count++; $db[$count]=$_; might be written as push(@db,$_)
(except that you're skipping $db[0])
Maybe ahoffmann was thinking of
@db = grep !/^$match/,<FID>;
(but this will also ignore lines begining with 1bb <TAB>,
and you'd also want to be carefull about metacharacters in $match)
0
ozoCommented:
(continue does have a meaning in Perl, but it's not the same as C's continue.)
it's main use is to define the for(;;){} loop rigorously.
for( $i=1; $i<10; $i++ ){ ... next; ... }
is the same as
$i=1; while( $i<10 ){ ... next; ... }continue{ $i++ }

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

ahoffmannCommented:
ozo, I was lazy.
dyrewolf, here're the reasons:
  - the open check is in all **my** scripts :-))
  - grep or if or ??, the Camel book somewhere recomments grep
  - yes, the pattern should have the o flag
  - continue should be next
  - using arrays as in C or perl's push,
      perls's (Larry Wall's) slogan: There's More Than One Way To Do It.
  - 1bb: no, I didn't write a regex for all possible patterns (that was not the subject of this question)

ozo, I've seen that you are more used to perl than I. Lets talk
about such things  at  hoagascht@gmx.net, if you like.
0
DyrewolfAuthor Commented:
Thanks a lot, both of you. To print the results back to the text file without the deleted line, can I just overwrite the whole file and print @db? If so, how?

Thanks
Adam
0
ahoffmannCommented:
If you want to replace strings on the fly, you have to use perl's read() and write() function (they are direct interfaces to the corresponding system calls !).

But I recommend that you write the result to a new file and then move it to the original one:
  system("rm old_file");      # or unlink( old_file )
  system("mv new_file old_file");
0
ozoCommented:
ahoffmann, where in the Camel book does it recommend grep on a scalar?
On "More Than One Way To Do It", Lary has an interesting sermon at
http://london.wall.org/larry/keynote/keynote.html
one of the points of which is that we shouldn't complain about people using Perl in a C or other style.
(Even if I was merely pointing out "another way to do it",
which I think is appropriate if this forum is meant to help people learn Perl)
On 1bb, the point was that the regexp does match more patterns than the given value.

Since this question is now a PAQ that costs points to visit, I'd
rather not continue the discussion here, you could start a new 0 point question,
or ask contact@experts-exchange.com to forward a message.

Dyrewolf, one way to write out @db might be:

open(F,">new_text_file") || die "can't write file $!";
print F @db;
close F;
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.