Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert bytes to GB in each line

Tags:

bash

sed

awk

How can I convert bytes to gigabytes in each line in single column in Bash? I have tried this:

echo "scale=2; $(cat file.log) / 1024^2" | bc

but that convert only last value.

Example of file.log content:

   2171863040
   1693491200
   1984045056

(without spaces between)

like image 983
DisplayName Avatar asked Nov 15 '25 22:11

DisplayName


1 Answers

You could use awk:

$ cat file.log
1073741824 1073741824

$ awk '{print $1/1024/1024/1024 " GB " $2/1024/1024/1024 " GB"}' file.log
1 GB 1 GB

or using the sample data you just posted:

$ awk '{print $1/1024/1024/1024 " GB "}' file.log
2.02271 GB
1.57719 GB
1.84779 GB
like image 161
cdub Avatar answered Nov 18 '25 11:11

cdub