andiOak
andiOak

Reputation: 396

Multiplying all values in a txt file with another value

My aim is to multiply all values in a text file with a number. In my case it is 1000.

Original text in file:

0.00493293814
0.0438981727
0.149746656
0.443125129
0.882018387
0.975789607
0.995755374
1

I want the output to look like: (so, changing the contents of the file to...)

4.93293814
43.8981727
149.746656
443.125129
882.018387
975.789607
995.755374
1000

Or even rather:

4.9
43.8
149.7
443.1
882.0
975.7
995.7
1000

I am using bash on macOS in the terminal.

Upvotes: 0

Views: 1854

Answers (5)

Socowi
Socowi

Reputation: 27255

Use sed to prefix each line with 1000*, then process the resulting mathematical expressions with bc. To show only the first digit after the decimal point you can use sed again.

sed 's/^/1000*/' yourFile | bc | sed -E 's/(.*\..).*/\1/'

This will print the latter of your expected outputs. Just as you wanted, decimals are cut rather than rounded (1.36 is converted to 1.3).

To remove all decimal digits either replace the last … | sed … with sed -E 's/\..*//' or use the following command

sed 's:^.*$:1000*&/1:' yourFile | bc

With these commands overwriting the file directly is not possible. You have to write to a temporary file (append > tmp && mv tmp yourFile) or use the sponge command from the package moreutils (append | sponge yourFile).

However, if you want to remove all decimal points after the multiplication there is a trick. Instead of actually multiplying by 1000 we can syntactically shift the decimal point. This can be done in one single sed command. sed has the -i option to overwrite input files.

sed -i.bak -E 's/\..*/&000/;s/^[^.]*$/&.000/;s/\.(...).*/\1/;s/^(-?)0*(.)/\1\2/' yourFile

The command changes yourFile's content to

4
43
149
443
882
975
995
1000

A backup yourFile.bak of the original is created.

The single sed command should work with every input number format too (even for things like -.1-100).

Upvotes: 0

agc
agc

Reputation: 8406

Using num-utils. For answers to 8 decimal places:

numprocess '/*1000/' n.txt

For rounded answers to 1 decimal place:

numprocess '/*1000/' n.txt | numround -n '.1'

Upvotes: 1

stack0114106
stack0114106

Reputation: 8711

Using Perl

perl -lpe ' $_=$_*1000 '

with inputs and inline replacing

$ cat andy.txt
0.00493293814
0.0438981727
0.149746656
0.443125129
0.882018387
0.975789607
0.995755374
1

$ perl -i -lpe ' $_=$_*1000 ' andy.txt

$ cat andy.txt
4.93293814
43.8981727
149.746656
443.125129
882.018387
975.789607
995.755374
1000

$

One decimal place

perl -lpe ' $_=sprintf("%0.1f",$_*1000 ) '

Zero decimal place and rounding off

perl -lpe ' $_=sprintf("%0.0f",$_*1000 ) '

Zero decimal place and Truncating

perl  -lpe ' $_=sprintf("%0.0f",int($_*1000) ) ' 

Upvotes: 2

ctac_
ctac_

Reputation: 2491

If you have dc :

cat infile | dc -f - -e '1k1000sa[la*Sdz0!=Z]sZzsclZx[Ld1/psblcd1-sc1<Y]sYlYx'

Upvotes: 2

karakfa
karakfa

Reputation: 67507

awk to the rescue!

$ awk '{printf "%.1f\n", $1*1000}' file > tmp && mv tmp file

Upvotes: 1

Related Questions