suhas savkoor
suhas savkoor

Reputation: 133

Floating point division with bc in a single command

I am trying to convert bytes to TB from a certain match in a command / file.

The command I have got is:

var=$(($(cat test.txt | awk '/miscellaneous/ {print $NF}' | sed s/.$//)/1000**4)) ; printf $var

The value is supposed to be 6.182 but it prints only 6. I just cannot figure out how to use bc in this command to get the floating values.

test.txt looks something like this:

  "a": 90919780478976,
  "b": 150812851408896,
  "c": 86337338950671,
  "miscellaneous": 6182842641393,
  "d": 0,
  "e": 58292669816832

Upvotes: 0

Views: 602

Answers (1)

wef
wef

Reputation: 361

bc is the wrong tool - try awk:

awk '/miscellaneous/ { print $2 / 1.0e12; }' < test.txt

6.18284

In your original example, it is doing division in bash which only understands integer arithmetic.

Upvotes: 1

Related Questions