We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

[Q] Search/replace within text file

Dyrewolf
Dyrewolf asked
on
Medium Priority
242 Views
Last Modified: 2012-05-04
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
Comment
Watch Question

$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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
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)
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
(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++ }

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.

Author

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
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");
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
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;
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.