Reputation:
I have a file which contains values in KB
and MB
. How can I, in a for
-loop calculate the sum of the values in MB for both MB
and KB
?
For MB, This gives me the values and then I need to sum those:
cat usage_1.12.2015_31.12.2015.xls.csv | grep MB | awk -F '"' '{print $4}' | awk '{print $1}'
Output:
17
16
13
17
14
3
3
12
10
8
6
10
KB
student@student-vm:~/Downloads$ cat usage1.12.2015_31.12.2015.xls.csv | grep KB
"2015/12/28 14:15:46","109 KB",,0
"2015/12/28 14:13:47","331 KB",,0
"2015/12/21 11:01:34","762 KB",,0
"2015/12/20 19:51:40","253 KB",,0
"2015/12/07 07:18:50","81 KB",,0
"2015/12/03 14:56:06","407 KB",,0
MB
student@student-vm:~/Downloads$ cat usage1.12.2015_31.12.2015.xls.csv | grep MB
"2015/12/06 15:54:47","10 MB",,0
"2015/12/06 09:43:02","4 MB",,0
"2015/12/05 21:39:04","21 MB",,0
"2015/12/05 09:17:11","18 MB",,0
"2015/12/04 21:57:49","70 MB",,0
"2015/12/04 17:09:45","1 MB",,0
"2015/12/04 16:47:05","17 MB",,0
Upvotes: 1
Views: 145
Reputation: 74615
I would recommend using awk here:
awk -F'"?,"?' '{ split($2, a, / /); if (a[2] == "KB") a[1] /= 1000; sum += a[1] }
END { print sum }' file
The -F
switch is used to split the input on commas, optionally surrounded by quotes. Split the second field $2
on spaces / /
into the array a
. Divide the first part a[1]
(the number) by 1000 if the second part a[2]
is KB
. Accumulate a sum of the values and once the file has been fully processed, print the value in the END
block.
Upvotes: 1
Reputation: 58808
First get the column values so that you have just lines like 1 MB
, 50 KB
etc., then you can do this kind of thing to get the number of bytes:
Replace spaces with empty strings (using sed
notation):
s/ //
Convert megabytes to kilobytes (add three zeros if using ISO units, *1024KB
and send the result of step 3 through bc
if it's kibibytes; you can also do this for GB, TB etc.):
s/MB/000KB/
Convert kilobytes to bytes:
s/KB/000/
Convert bytes to megabytes:
bc <<< "${sum} / 1000000"
Upvotes: 0