Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find a substring, replace and multiply by 1024

Tags:

grep

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!

like image 215
Ali Bek Avatar asked Dec 01 '25 10:12

Ali Bek


1 Answers

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)

like image 139
fge Avatar answered Dec 04 '25 23:12

fge