在bash中将kB和GB转换为MB [英] Converting kB and GB to MB in bash

查看:33
本文介绍了在bash中将kB和GB转换为MB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用awk和sed获取表中分区的列表及其大小,我想用它们来计算单个表的每日增量.这是我的输出,现在我正在努力将所有大小转换为MB.

Im using awk and sed to get a list of partitions in table with their size, which I want to use to calculate the daily increment for individual tables. This is the output I have, now Im struggling to convert all of the sizes to MBs.

最好的bash方式是匹配第二个字段中的数字并根据"MB"或"GB"字符串将其相乘

What would be the best bash way to match the number in the second field and multiply it depending on the "MB" or "GB" string

2017061505,482.46MB,hdfs://user/hive/warehouse/cz_prd_ntw_op.db/diameter__24_/pr_comp_ver=0/pr_start_time=2017061505,
2017061505,722.58MB,hdfs://user/hive/warehouse/cz_prd_ntw_op.db/diameter__24_/pr_comp_ver=0/pr_start_time=2017061506,
2017061507,1.03GB,hdfs://user/hive/warehouse/cz_prd_ntw_op.db/diameter__24_/pr_comp_ver=0/pr_start_time=2017061507,
2017061507,1.25GB,hdfs://user/hive/warehouse/cz_prd_ntw_op.db/diameter__24_/pr_comp_ver=0/pr_start_time=2017061508,

所需的输出将是:

2017061505,482.46MB,hdfs://MORPHEUS/user/hive/warehouse/cz_prd_ntw_op.db/diameter__24_/pr_comp_ver=0/pr_start_time=2017061505,
2017061506,722.58MB,hdfs://MORPHEUS/user/hive/warehouse/cz_prd_ntw_op.db/diameter__24_/pr_comp_ver=0/pr_start_time=2017061506,
2017061507,1030MB,hdfs://MORPHEUS/user/hive/warehouse/cz_prd_ntw_op.db/diameter__24_/pr_comp_ver=0/pr_start_time=2017061507,
2017061508,1250MB,hdfs://MORPHEUS/user/hive/warehouse/cz_prd_ntw_op.db/diameter__24_/pr_comp_ver=0/pr_start_time=2017061508,

推荐答案

awk 解决方案:

awk solution:

awk 'BEGIN{ FS=OFS="," }{ s=substr($2,1,length($2)-1); u=substr($2,length($2)-1); 
     if(u=="KB") $2=(s/1024)"MB"; else if(u=="GB") $2=(s*1024)"MB" }1' yourfile

输出:

2017061505,482.46MB,hdfs://user/hive/warehouse/cz_prd_ntw_op.db/diameter__24_/pr_comp_ver=0/pr_start_time=2017061505,
2017061505,722.58MB,hdfs://user/hive/warehouse/cz_prd_ntw_op.db/diameter__24_/pr_comp_ver=0/pr_start_time=2017061506,
2017061507,1054.72MB,hdfs://user/hive/warehouse/cz_prd_ntw_op.db/diameter__24_/pr_comp_ver=0/pr_start_time=2017061507,
2017061507,1280MB,hdfs://user/hive/warehouse/cz_prd_ntw_op.db/diameter__24_/pr_comp_ver=0/pr_start_time=2017061508,


请注意,信息学中的标称单位值为 1024

这篇关于在bash中将kB和GB转换为MB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆