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

Replacing numbers in afile with a text search 1

Status
Not open for further replies.

hill007

Technical User
Mar 9, 2004
60
US
I have a large data set and I need to replace the content of the data set by a number based on a text search. However, I want to keep the same format as it was in the original file.

Here is a small example of the file:

15 1.000(10e12.4) -1 CONSTANT
0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00
0.0000e+00 0.0000e+00 0.0000e+00 0.0000e+00
15 1.000(10e12.4) -1 CONSTANT
2.7500e-04 1.5000e-04 1.5000e-04 1.5833e-04

I want to replace the above data set by searching for the word "CONSTANT" and replacing the numbers below the word "CONSTANT" with 2.2283e-03.

Here is how the final data set should look like. One of the most important part is keeping the format intact, i.e., the spacings between the numbers should be as it is with the original data set and also the way the number is written, i.e, 2.2283e-03.

15 1.000(10e12.4) -1 CONSTANT
2.2283e-03 2.2283e-03 2.2283e-03 2.2283e-03
2.2283e-03 2.2283e-03 2.2283e-03 2.2283e-03
15 1.000(10e12.4) -1 CONSTANT
2.2283e-03 2.2283e-03 2.2283e-03 2.2283e-03


Thank you always for any help.
 
something like this ?
sed '/CONSTANT$/!s/[0-9][-+0-9.e]*/2.2283e-03/g' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks for your response. However, I do not have access to SED. I only use AWK. Is there anyway that the above program me modified for AWK?

Thanks.
 
I do not have access to SED
Windows ?
Create a file named, say, hill007.awk:
Code:
/CONSTANT$/{print;next}
{x=$0;gsub(/[0-9][-+0-9.e]+/,"2.2283e-03",x);print x}
awk -f hill007.awk input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top