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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to construct a simple FIND and REPLACE script

Status
Not open for further replies.

domanicap

IS-IT--Management
Sep 21, 2007
6
US
Here's what I'm trying to accomplish. (Pardon the simplicity of my request, but I'm not very good at perl yet...)

I have a very large space (20+ mb) delimited text file that looks something like this

20510 78136 77936 78133 etc...
20626 78114 78115 78116 etc...

And I'd like to find a little set of numbers down in row #367

20510 78136 77936 78133 etc...

and change it to this

20510 78136 99999 78133 etc...


The 77936 is not unique (it occurs in the same column in the row above it), so I'm trying to write a script that finds this:
"78136 77936"
and replaces it with
"78136 99999"

It's just one spot in the file. And the 78136 is unique.

I'm assuming this is pretty simple, but so far I haven't been able to get it to work. Any thoughts?

Thanks!

 
to clarify, i can do a simple search and replace
BUT
the problem i'm having is the space in between the 2 sets of numbers in my unique string to search for
 
this is the code i'm using by the way:

#!/usr/bin/perl



#######################################
##### PUT THE DIRECTORY YOU WANT TO START THE ADD REPLACE FUNCTION
##### NO TRAILING SLASH
#######################################

$startDirectory = "/mydirectory";

#################################################
##### THIS IS THE EXTENSION OF THE FILES YOU WISH TO EDIT.
##### AS WRITTEN, IT WILL EDIT ALL .txt FILES
#################################################

$extension=".txt";

#################################################
##### THIS IS THE TEXT STRING TO FIND - THE ONE YOU
##### WANT CHANGED TO SOMETHING ELSE. Change to fit your needs.
#################################################

$find = "78136 77936";


#################################################
##### THIS IS THE TEXT THAT SHOULD BE USED AS A REPLACEMENT
##### Change to fit your needs.
#################################################

$replace = "78136 99999";


##########MAIN PROGRAM BEGINS HERE DO NOT EDIT ########
#######################################################




$extension= "\\".$extension;

use File::Find;
find(\&runMain, "$startDirectory");

sub runMain {

if (/$extension/ ) {
$file= $_;
next unless (-r $file);

open(PAGE,"$File::Find::name") || die "I can't open $file";

while(<PAGE>){

$thisPage= $thisPage . $_;

}#while

close(PAGE);


$thisPage =~ s#$find#$replace#g;


open(PAGENEW,">$File::Find::name") || die "I can't write the new stuff to $file";

print PAGENEW $thisPage;

close(PAGENEW);
$thisPage="";
}#if


}#runMain






------------------------------------------------

I run it, it thinks for 5 min, then it looks like it's done. no error messge. But it doesn't do the job right.
 
Code:
open(PAGE,"$File::Find::name") || die "I can't open $file";
while(<PAGE>){
$thisPage= $thisPage . $_;
}#while
close(PAGE);

can be re-written as
Code:
open(PAGE,"$File::Find::name") || die "I can't open $file";
@data = <PAGE>;
close(PAGE);

Then just loop through that file to do your replaces

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
You don't need to even read this into an array.
This can be performed using the in place editor.

Code:
{
  local($^I, @ARGV) = ('.orig','filename1.txt','filename2.txt');
  while (<>) {
    s/78136 77936/78136 99999/;
    print;
    close ARGV if eof;
  }
}

This will make backups with an ".orig" extension.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top