找到一个子字符串,替换并乘以1024 [英] find a substring, replace and multiply by 1024

查看:110
本文介绍了找到一个子字符串,替换并乘以1024的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个内容如下的文件:

  03:14.27,31K
03: 13.59,50M
04:11.51,435K

问题是如何获得数字以字节为单位,并用旧值替换,以便我可以得到(也可以删除引号):

  03 :14.27,31744
......

使用哪个更好? grep还是awk?
Thanks!

解决方案

perl!

  fg @ erwin $ cat t.pl 
#!/ usr / bin / perl -W

use strict;

my%suffixes =(
K=> 10,
M=> 20,
G=> 30
);

while(my $ line =< STDIN>){
$ line =〜s /(\ d +)(\ w)/''。($ 1< ;< $ suffixes {$ 2})。''/ ge;
print $ line;
}
fge @ erwin〜$ cat<<< EOF | perl t.pl
> 03:14.27,31K
> 03:13.59,50M
> 04:11.51,435K
> EOF
03:14.27,31744
03:13.59,52428800
04:11.51,445440

(编辑:新输入)

I have a file with the content like that:

03:14.27,"31K" 
03:13.59,"50M" 
04:11.51,"435K" 

Question is how to get numbers in bytes and replace with the old values so that I can get (also getting rid of quotes would be useful):

03:14.27,"31744"
...... 

What to use better ? grep or awk? Thanks!

解决方案

perl!

fg@erwin $ cat t.pl
#!/usr/bin/perl -W

use strict;

my %suffixes = (
        "K" => 10,
        "M" => 20,
        "G" => 30
);

while (my $line = <STDIN>) {
    $line =~ s/"(\d+)(\w)"/ '"' . ($1 << $suffixes{$2}) . '"'/ge;
    print $line;
}
fge@erwin ~ $ cat <<EOF | perl t.pl
> 03:14.27,"31K" 
> 03:13.59,"50M" 
> 04:11.51,"435K"
> EOF
03:14.27,"31744" 
03:13.59,"52428800" 
04:11.51,"445440"

(edit: new input)

这篇关于找到一个子字符串,替换并乘以1024的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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