I have some data which is in the following format:
bruce 434355/512000 (84.8349609375%)
oliver 217542/512000 (42.488671875%)
kareem 236778/563200 (42.0415482954545%)
The data in the second column is in KB and I want to extract the first part (before the /) and display that only, like so:
bruce 424.17MB (84.8349609375%)
oliver 212.44MB (42.488671875%)
kareem 231.24MB (42.0415482954545%)
Is there a way in sed or awk to do that?
Alternately, what I have done is extract the second column using awk, wrote it into another file and the display one line from each file within a while loop.
awk '$2=$2/1024"MB"' file
如果只想保留两位小数,请执行以下操作:
awk '{printf "%s %.2fMB %s\n", $1, $2/1024, $3 }' file
You can use the following awk script:
function human(x) {
s="kMGTEPYZ";
while (x>=1000 && length(s)>1)
{x/=1024; s=substr(s,2)}
return int(x+0.5) substr(s,1,1)
}
{
split($2, a, "/");
$2 = human(a[1]);
print
}
save it to a file and execute it this way:
awk -f awk.script your.txt
The output is:
bruce 424M (84.8349609375%)
oliver 212M (42.488671875%)
kareem 231M (42.0415482954545%)
The human
function is taken from https://unix.stackexchange.com/a/44087 . Thanks!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.