Find the substring, replace and multiply by 1024

I have a file with the following contents:

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

The question is how to get the numbers in bytes and replace the old values ​​so that I can get (also removing quotes would be helpful):

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

Which is better to use? grep or awk? Thanks!

+3
source share
3 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)

+4
source

awk:

  awk 'BEGIN{k=1024;m=1024*k;g=1024*m;FS=OFS="\""} {x=substr($2,1,length($2)-1)*1} $2~/[Kk]$/{x*=k} $2~/[mM]$/{x*=m} $2~/[Gg]$/{x*=g} {print $1,x"\""} yourFile 

check your example:

 kent$ cat tt 03:14.27,"31K" 03:13.59,"50M" 04:11.51,"435K" kent$ awk 'BEGIN{k=1024;m=1024*k;g=1024*m;FS=OFS="\""} {x=substr($2,1,length($2)-1)*1} $2~/[Kk]$/{x*=k} $2~/[mM]$/{x*=m} $2~/[Gg]$/{x*=g} {print $1,x"\""}' tt 

exit:

 03:14.27,"31744" 03:13.59,"52428800" 04:11.51,"445440" 

if you don't need quotes:

  awk 'BEGIN{k=1024;m=1024*k;g=1024*m;FS="\""} {x=substr($2,1,length($2)-1)*1} $2~/[Kk]$/{x*=k} $2~/[mM]$/{x*=m} $2~/[Gg]$/{x*=g} {print $1,x} yourFile 
+2
source

Grep does not perform replacements, for this you will need sed. But sed cannot do math or conventions, so if you need full x1024 K / M, you'll need awk. If you can live with x1000, you can easily use sed to replace K / M with the appropriate number of zeros:

 sed -es/K/000/ -es/M/000000/ 

Awk code for full 1024 if you have gawk or another interpreter with switch :

 #!/usr/bin/awk -f BEGIN { FS = "\""; OFS = "\"" } { N = $2+0 if(N == 0) { print; next } M = substr($2,length($2),1) switch(M) { # Add T, P, X, etc. if you need them. Or just for fun. case "G": N *= 1024 case "M": N *= 1024 case "K": N *= 1024 } $2 = N print } 

If you can add more quotes before this field, change $2 to $NF . If your interpreter does not have a switch , you can use if with multiplied products or use Kent's answer. I just wanted to show using switch separator and the proper use of switch failures.

+1
source

Source: https://habr.com/ru/post/1205747/


All Articles