0

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.

andiOak
  • 356
  • 3
  • 9
  • note that `975.78` is closer to `975.8`, you may want to round the numbers instead of truncating. – karakfa Feb 12 '19 at 21:08
  • What DO you want done wrt rounding? rounded up or down or unbiased or truncated or something else or do you really not care? – Ed Morton Feb 12 '19 at 21:18
  • Is `1000` fixed (make it a string operation moving the dot after adding some `0`) ? – Walter A Feb 12 '19 at 22:51
  • 1
    If one of the answers here resolved your question please accept that answer. Accepting an answer closes this questions and rewards the author of the accepted answer. If your question was not resolved consider explaining why. – Socowi Feb 15 '19 at 12:07

5 Answers5

2

If you have dc :

cat infile | dc -f - -e '1k1000sa[la*Sdz0!=Z]sZzsclZx[Ld1/psblcd1-sc1<Y]sYlYx'
ctac_
  • 2,413
  • 2
  • 7
  • 17
2

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) ) ' 
stack0114106
  • 8,534
  • 3
  • 13
  • 38
1

awk to the rescue!

$ awk '{printf "%.1f\n", $1*1000}' file > tmp && mv tmp file
karakfa
  • 66,216
  • 7
  • 41
  • 56
1

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'
agc
  • 7,973
  • 2
  • 29
  • 50
0

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

Socowi
  • 25,550
  • 3
  • 32
  • 54
  • Used `sed` before and this one is working, except for it not destructively writing over the file. While I´d want that for some files, the current need is only to wrangle the numbers. This one did that perfectly. Is there a way to get rid of all of the decimals and the "." (dot) as well? – andiOak Feb 13 '19 at 03:47
  • Re *"overwrite ... not possible"*: with *GNU* `sed`'s **`e`**_valuate_ command it can be done like `sed -i 's/.*/'"dc -e '1000 & * p'"'/e' yourFile`. It's more inefficient, (using a separate instance of `dc` for each line), but doesn't require `sponge`. – agc Feb 13 '19 at 08:12
  • @agc good to know. However OP is using mac os which does not come with GNU sed. – Socowi Feb 13 '19 at 08:17
  • @Socowi, True, but `gsed` can be had should that seem necessary. See [How to use GNU sed on Mac OS X](https://stackoverflow.com/questions/30003570/how-to-use-gnu-sed-on-mac-os-x) – agc Feb 13 '19 at 10:11