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