且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

找到一个子字符串,替换并乘以1024

更新时间:2023-02-06 17:39:47

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&lt ;< $ 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)