bash 用于 awk 打印除以 1024 的值的 shell 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25077709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:00:14  来源:igfitidea点击:

shell script for awk print divide value with 1024

bashshellawk

提问by naveen_sfx

I am trying to do some arthimatic operations on awk output process. my sample file contains string and it's count. Now i am trying to make the count values to be in format of MB (defualt count will come in bytes.)

我正在尝试对 awk 输出过程进行一些arthimatic 操作。我的示例文件包含字符串和计数。现在我试图使计数值采用 MB 格式(默认计数将以字节为单位。)

Example : myfile.txt

示例:myfile.txt

   a 123455
   b 34455566
   c 10394934839
   d 102445555

my script :

我的脚本:

  cat myfile.txt | while read line; do name=$line ; name=`echo $fname|awk '{print   }'` ; cnt=`echo $fname|awk '{print }'`; if [$cnt -gt 1024] ; then echo "$name value in critical $cnt";fi done

Problem: I want the cnt value should be convert into MB ( cnt/1024/1024) .

问题:我希望 cnt 值应转换为 MB ( cnt/1024/1024) 。

no clue how to achieve this.

不知道如何实现这一目标。

采纳答案by damienfrancois

You can do it like this (pure Bash):

你可以这样做(纯 Bash):

$ cat myfile.txt | while read l f ; do echo $l $((f/1024/1024)) ;if [ $((f/1024/1024)) -gt 
1024 ] ; then echo "$l value in critical ";fi done
a 0
b 32
c 9913
c value in critical 
d 97

回答by anubhava

You can do it directly in awkwithout looping in BASH:

您可以直接在awk不循环的情况下进行BASH

awk '{print , /(1024*1024)}' file
a 0.117736
b 32.8594
c 9913.38
d 97.6997

OR for 2 decimal point output:

或 2 个小数点输出:

awk '{printf "%s %.2f\n", , /(1024*1024)}' file
a 0.12
b 32.86
c 9913.38
d 97.70

回答by Kent

kent$  awk '{printf "%s %.2f\n",,/1024/1024}' file
a 0.12
b 32.86
c 9913.38
d 97.70

change the %.2fto gain different precision.

改变%.2f以获得不同的精度。

if you want to add the critical check:

如果要添加关键检查:

awk '{v=/1024/1024;printf "%s %.2f%s\n",,v,(v>1024?" <=critical":"")}' file
a 0.12
b 32.86
c 9913.38 <=critical
d 97.70

回答by Kent

Another awk way

另一种 awk 方式

awk '{/=2^20}1' file

Formatted

格式化

awk '=sprintf("%.2f",/2^20)' file

With crit

带暴击

awk '=sprintf("%.2f%s",x=(/2^20),x>1024?" [critical]":"")' test

回答by Pithikos

When you have structured data and you want to loop per line, just use awk. Besides the saving of CPU cycles (which is negligible in most cases) the biggest benefit is that you avoid the weird quote and space moaning of Bash.

当您有结构化数据并且想要每行循环时,只需使用awk. 除了节省 CPU 周期(在大多数情况下可以忽略不计)之外,最大的好处是您可以避免 Bash 的奇怪引用和空间呻吟。

awk '{ MB = int(/1024/1024); print  " " MB; if (MB > 1024) print  " value in critical"}' myfile.txt

Output:

输出:

a 0
b 32
c 9913
c value in critical
d 97

Breaking it down:

分解它:

MB = int(/1024/1024)    ->    Take the value in the second field ().
                                Make it into an integer (to round the number)
                                and store it in the variable MB.

print  " " MB           ->    Print the first field (), followed by
                                a space, and finally the value in MB.

if (MB > 1024)
    print  " val.."     ->    If MB < 1024, print first field followed by msg.