gpt4 book ai didi

regex - sed/awk : replace megabytes with bytes (insert zeros)

转载 作者:行者123 更新时间:2023-12-02 04:19:14 24 4
gpt4 key购买 nike

我有 .csv 文件,其中字段以逗号分隔,行以\n 分隔。在某些行中我有兆字节符号。我想用零替换它以获得(或多或少)正确的字节大小。

我拥有的是

,2.6 M,

我想要

,2600000,

示例

2015-06-01 00:04:52.736,10.0.0.2,10.0.0.4,443,443,56923,2.6 M,10.156.119.1
2015-06-01 00:04:56.736,10.0.0.2,10.0.0.4,443,58935,55658,1.3 M,10.156.126.1
2015-06-01 00:04:56.736,10.0.0.2,10.0.0.4,443,86,54801,1256,10.156.119.1
2015-06-01 00:04:52.736,10.0.0.2,10.0.0.4,443,49652,443,1.6 M,10.156.119.1
2015-06-01 00:04:53.732,10.0.0.2,10.0.0.4,443,443,55770,4.9 M,10.156.119.1
2015-06-01 00:04:54.732,10.0.0.2,10.0.0.4,443,80,45980,639,10.156.119.1
2015-06-01 00:04:54.732,10.0.0.2,10.0.0.4,443,63951,27058,1.2 M,10.156.119.1
2015-06-01 00:04:54.732,10.0.0.2,10.0.0.4,443,80,41035,13.8 M,10.156.119.1
2015-06-01 00:04:55.736,10.0.0.2,10.0.0.4,443,80,40078,7.9 M,10.156.119.1
2015-06-01 00:04:56.732,10.0.0.2,10.0.0.4,443,42008,4.5 M,10.156.119.1

目标

2015-06-01 00:04:52.736,10.0.0.2,10.0.0.4,443,443,56923,2600000,10.156.119.1
2015-06-01 00:04:56.736,10.0.0.2,10.0.0.4,443,58935,55658,1300000,10.156.126.1
2015-06-01 00:04:56.736,10.0.0.2,10.0.0.4,443,86,54801,1256,10.156.119.1
2015-06-01 00:04:52.736,10.0.0.2,10.0.0.4,443,49652,443,1600000,10.156.119.1
2015-06-01 00:04:53.732,10.0.0.2,10.0.0.4,443,443,55770,4900000,10.156.119.1
2015-06-01 00:04:54.732,10.0.0.2,10.0.0.4,443,80,45980,639,10.156.119.1
2015-06-01 00:04:54.732,10.0.0.2,10.0.0.4,443,63951,27058,1200000,10.156.119.1
2015-06-01 00:04:54.732,10.0.0.2,10.0.0.4,443,80,41035,13800000 M,10.156.119.1
2015-06-01 00:04:55.736,10.0.0.2,10.0.0.4,443,80,40078,7900000,10.156.119.1
2015-06-01 00:04:56.732,10.0.0.2,10.0.0.4,443,42008,4500000,10.156.119.1

最佳答案

由于示例数据的最后一行缺少一列,这让情况变得复杂。

awk 'BEGIN {FS=OFS=","} {$(NF-1)=$(NF-1)*1000000} 1' file

如果您有时有“M”,有时有“K”,我们可以满足:

awk '
BEGIN {
FS=OFS=","
mult[""]=1
mult["K"]=1000
mult["M"]=1000000
mult["G"]=1000000000
}
{
split($(NF-1), a, " ")
$(NF-1) = a[1] * mult[a[2]]
print
}
'

关于regex - sed/awk : replace megabytes with bytes (insert zeros),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31724155/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com