I have a data file in which I need to delete a single entry value. The value will change, but its location in the file will always be the same.
Here is the section of the data file:
4.62399E+09 2.90871E+09 7.17338E+14
sb1 1.0 142r
I want to delete the 7.17338E+14 (or whatever is at that location. In my code I first open this file and find the previous line above sb1 ($oldine and $newline stuff)
#!usr/bin/perl use warnings; use strict; # my ($line)= "4.62399E+09 2.90871E+09 7.17338E+14"; my ($oldline) = "4.62399E+09 2.90871E+09 7.17338E+14"; my ($newline) = "4.62399E+09 2.90871E+09 "; # open (IN, "sdef"); while (<IN>) { if ($_ =~ /sb1 1.0 142r/ ) { $oldline = substr($line, 10, 38); $newline = substr($oldline, 0, 26); } else { $line = $_; } } close(IN); # chomp($oldline); chomp($newline); print "new = $newline\n"; print "old = $oldline\n"; # open (IN, "sdef"); open (OUT, ">sdef_steel") or die "Cannot open file for writing: + $+!" ; while (<IN>) { s/si1 L 1000 1000 /si1 L 1000 /; # works ok s/2150 2151 1035/2150 2151 /; # works ok s/$oldline/$newline/; # doesn't work s/sp1 D (\d+.\d+E[-+]\d+) /sp1 D /; # works ok print OUT $_; } close (IN); close (OUT);
Upon running, I get:
perl test.pl
new = 4.62399E+09 2.90871E+09
old = 4.62399E+09 2.90871E+09 7.17338E+14
Okay, so far so good. But if I grep the new file sdef_steel for 7.17338E+14, it still exists in the sdef_steel file.
It is not recognizing my $oldline string. I think the problem is how I am declaring it.
I have tried both my($oldline) = "txtxxt"; and my($oldline) = "1.234". Of course, the exponential is a combo.
I had thought declaring it as text (string) would be the best method.
So, what is the best way to handle subsitution of exponentials in a single data line?
In reply to how to read in a line containing exponentials by pattobw
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |