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

Search and Replace in an existing file

Status
Not open for further replies.

keid

Technical User
Aug 8, 2006
22
DE
I have 2 files

file1sample
-------------------------
NODE / 563 90. .0 .0
NODE / 564 80. .0 .0
NODE / 565 70. .0 .0
NODE / 566 60.0 .0 .0

BEAM / 563 5 5 8

file2sample
-------------------------------
563
564
568


so what I want to do is search if to read each line of file2 and check file1 lines if starting with node and matching the same number after NODE then replace the last value of this line with an added value of 2 for example


so finally file1 will be looking like this output

output
----------------------------

NODE / 563 90. .0 2
NODE / 564 80. .0 2
NODE / 565 70. .0 .0
NODE / 566 60.0 .0 .0
BEAM / 563 5 5 8

I did something but instead i printed new result without deleting the old ones
 
want to show your script?

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
sure

I tried s/// but did not do the job coz 0 is found somewhere else

Code:
open(DAT_A, "+<file1.txt") || die("Could not open file1");
open(DAT_B, "file2.txt") || die("Could not open file2");

@pc_data=<DAT_A>;
@nodal_data=<DAT_B>;

foreach $node (@nodal_data)
{
		foreach $data (@pc_data)
		{ 
		if($data=~ m!^NODE! )
				{
				$node_id = substr("$data", 8, 8);
				$x= (substr("$data", 16, 16))*1;
				$y = (substr("$data", 32, 16))*1;
				$z = (substr("$data", 48, 16))*1;
					if ($node_id == $node ) 
						{ 
						$z=$z+2; 
						printf  DAT_A "NODE  / %8s%16.2f%16.2f%16.2f\n",$node_id,$x,$y,$z;
						}
				}
		
	      }

}

close DAT_A;
close DAT_B;
 
Code:
$string=~s/\.0$/\.2$/;

this should replace the last zero, note the dollar, it means match to end of line. If there are spaces at the end of the line, just fill the number of spaces between 0 and the $

For further info, check out perlretut on
HTH

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
this file is opened for reading:

open(DAT_B, "file2.txt") || die("Could not open file2");

this file you may think is open for reading and writing but probably isn't:

open(DAT_A, "+<file1.txt") || die("Could not open file1");

either use inline editing or write the output to a new file entirely.


 
What about if the last number is not zero does it work with (\d)$?

+< for file1.txt works as expected adding new data without deleting old data. But I wish to do inline editing by changing the values, do u suggest s///?

many thanks
 
Since I suck at one-liners, here it is in long form using the inline editor:

Code:
open(DAT_B, "file2.txt") || die("Could not open file2: $!");
my %nodal_data = map {chomp; $_,$_} <DAT_B>;
close(DAT_B); 
{ 
   local ($^I, @ARGV) = ('.bak', 'file1.txt'); 
   while (<>) {
      if (/^NODE\s+\/\s+(\d+)\s+/ && exists $nodal_data{$1}) {
         s/\.\d$/2/ && print;
      }
      else {
         print;
      }
   }
}

you may need to mess around with it depending on the real file and what you are trying to do because I really am not entirely clear on what you are trying to accomplish.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top