Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

shell - Multiply text file in Unix by a constant

How can a text file be multiplied in by a constant using shell commands? For e.g there is a text file consisting the following numbers

    -255.9641842033 
    -255.9667588863 
    -256.9777650145 
    -258.9777662459 
    -259.9777661194 

This needs to be multiplied with a constant 19.123456789123 and save in new text file. How can the above be accomplished? Looking forward to a reply.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This might be close, using Perl:

perl -pe 's/([+-]?[0-9.]+)/$1*19.123456789123/ge' YourFile

Sample Output

-4894.92001617493 
-4894.96925301402 
-4914.3031850202 
-4952.55012214707 
-4971.67357651707

That kind of says... "capture anything that starts with an optional plus or minus and has a bunch digits and decimal points and call it capture group 1. Replace that with whatever it was multiplied by your magic number. The e at the end, says to evaluate the right side as an expression rather than take it as a literal. The g at the end says to do it each and every time it occurs on each line, rather than just the first time."


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...