Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 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 Reply

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."


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...