sed / awk:用字节替换兆字节(插入零)

时间:2015-07-30 12:44:53

标签: regex bash csv awk sed

我有.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

3 个答案:

答案 0 :(得分:2)

由于样本数据中的最后一行缺少一列,这很复杂。

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
    }
'

答案 1 :(得分:1)

sed 's/\([0-9]*\)\.\([0-9]*\) M/\1\200000/' file

答案 2 :(得分:1)

sed 's/ \([KMG]\)/000000000\1/
     s/\.\([0-9]\{3\}[0-9]*K/\1/
     s/\.\([0-9]\{6\}[0-9]*M/\1/
     s/\.\([0-9]\{9\}[0-9]*G/\1/
    ' YourFile
  • 基于您的样本,其中只有1个字母的数字,我们应该更改1000个单位的倍数

可以使用sed 's/\.\([^,]*\) M/\1000000/' YourFile进行简化,如果只有M出现在您的样本中(1位数字仅在使用M的值后点)