使用awk将不同列上执行计算。

huangapple go评论50阅读模式
英文:

Combine awk to perform calculation on different columns

问题

我已成功达到所需的目标,使用awk命令可以实现这一点。不过,您可以使用更简洁的awk命令来达到相同的效果。以下是简化后的示例:

cat output_file.txt | awk -v OFS="\t" '/7000|Total:/ {
    if ($4 > 1073741824)
        $4 = sprintf("%.2fGB", $4 / 1024 / 1024 / 1024);
    else if ($4 > 1048576)
        $4 = sprintf("%.2fMB", $4 / 1024 / 1024);
    else if ($4 > 1024)
        $4 = sprintf("%.2fKB", $4 / 1024);
    
    if ($7 >= 0)
        $7 = sprintf("%.2fGB", $7 * 4 / 1024 / 1024);
    
    if ($8 >= 0)
        $8 = sprintf("%.2fKB", $8 * 4 / 1024);

    print $1, $2, $3, $4, $5, $6, $7, $8;
}'

这个命令首先匹配包含"7000"或"Total:"的行。然后,它根据您的要求处理第4、7和8列,将它们转换为所需的单位,并使用sprintf格式化输出。最后,它输出修改后的行,用制表符作为字段分隔符。

这将产生与您所期望的输出相同的结果,但使用了更简洁的awk命令。

英文:

I have the following output below and I want to manipulate the 4th, 7th and 8th column to show in either kilobytes, meagebytes or gigabytes. Column 4 is in bytes and column 7 and 8 is in pages, which is 4 (so multiply the value in column 7 and 8 by 4 to get kilobytes)

Segment Summary:
id         key        addr             size             ovhd     class blkused  blkfree
8389645    52564801   700010000000000  686145536        8482408  R     167512   4
3146755    52564802   700020000000000  21474836480      251660232 V     785701   4457179
3146759    52564803   700030000000000  67738288128      1        B     16537668 0
3146756    52564804   700040000000000  43558125568      1        B     10634308 0
3146760    52564805   700050000000000  2195456          27096    M     536      0
3146762    52564806   700060000000000  2195456          27096    M     536      0
3146763    52564807   700070000000000  2195456          27096    M     534      2
4195340    52564808   700080000000000  2195456          27096    M     534      2
Total:     -          -                133466177536     -        -     28127329 4457187

   (* segment locked in memory)
No reserve memory is allocated

I have successfully managed to achieve exactly what I want using awk, however I am using three awks instead of one to achieve what I want. See below example command I am using:

cat output_file.txt | grep -E '7000|Total:' | awk '{
if ($4 > 1073741824) printf ("%s %s %s %.2f%s %s %s %s %s\n"),$1,$2,$3,$4/1024/1024/1024,"GB",$5,$6,$7,$8;
else if ($4 > 1048576) printf ("%s %s %s %.2f%s %s %s %s %s\n"),$1,$2,$3,$4/1024/1024,"MB",$5,$6,$7,$8;
else if ($4 > 1024) printf ("%s %s %s %.2f%s %s %s %s %s\n"),$1,$2,$3,$4/1024,"KB",$5,$6,$7,$8'} | awk '{
if ($7 > 262144) printf ("%s %s %s %s %s %s %.2f%s %s\n"),$1,$2,$3,$4,$5,$6,($7*4)/1024/1024,"GB",$8;
else if ($7 > 256) printf ("%s %s %s %s %s %s %.2f%s %s\n"),$1,$2,$3,$4,$5,$6,($7*4)/1024,"MB",$8;
else if ($7 >= 0) printf ("%s %s %s %s %s %s %.2f%s %s\n"),$1,$2,$3,$4,$5,$6,$7*4,"KB",$8'}       | awk '{
if ($8 > 262144) printf ("%s %s %s %s %s %s %s %.2f%s\n"),$1,$2,$3,$4,$5,$6,$7,($8*4)/1024/1024,"GB";
else if ($8 > 256) printf ("%s %s %s %s %s %s %s %.2f%s\n"),$1,$2,$3,$4,$5,$6,$7,($8*4)/1024,"MB";
else if ($8 >= 0) printf ("%s %s %s %s %s %s %s %.2f%s\n"),$1,$2,$3,$4,$5,$6,$7,$8,"KB"'}

The above is a mess. If there is an easier way to achieve what I want, assistance will be greatly appreciated.

Output:

8389645 52564801 700010000000000 654.36MB 8482408 R 654.34MB 4.00KB
3146755 52564802 700020000000000 20.00GB 251660232 V 3.00GB 17.00GB
3146759 52564803 700030000000000 63.09GB 1 B 63.09GB 0.00KB
3146756 52564804 700040000000000 40.57GB 1 B 40.57GB 0.00KB
3146760 52564805 700050000000000 2.09MB 27096 M 2.09MB 0.00KB
3146762 52564806 700060000000000 2.09MB 27096 M 2.09MB 0.00KB
3146763 52564807 700070000000000 2.09MB 27096 M 2.09MB 2.00KB
4195340 52564808 700080000000000 2.09MB 27096 M 2.09MB 2.00KB
Total: - - 124.30GB - - 107.30GB 17.00GB

Please to demonstate how this can be achieved, you do not have to use my Example output above

答案1

得分: 2

以下是已经翻译好的内容:

"The general idea would be to update your fields ($4, $7, $8) when conditionals evaluate to true, then use one consolidated printf format to print the line with the (3x) updated field values."

"对于重写当前的 awk 代码的一个想法:"

"这会生成:"

"8389645 52564801 700010000000000 654.36MB 8482408 R 654.34MB 4.00KB
3146755 52564802 700020000000000 20.00GB 251660232 V 3.00GB 17.00GB
3146759 52564803 700030000000000 63.09GB 1 B 63.09GB 0.00KB
3146756 52564804 700040000000000 40.57GB 1 B 40.57GB 0.00KB
3146760 52564805 700050000000000 2.09MB 27096 M 2.09MB 0.00KB
3146762 52564806 700060000000000 2.09MB 27096 M 2.09MB 0.00KB
3146763 52564807 700070000000000 2.09MB 27096 M 2.09MB 2.00KB
4195340 52564808 700080000000000 2.09MB 27096 M 2.09MB 2.00KB
Total: - - 124.30GB - - 107.30GB 17.00GB"

英文:

The general idea would be to update your fields ($4, $7, $8) when conditionals evaluate to true, then use one consolidated printf format to print the line with the (3x) updated field values.

One idea for a rewrite of the current awk code:

awk '
/7000|Total:/ {      if ($4 > 1073741824) { units[4]="GB"; $4=$4/1024/1024/1024 }
                else if ($4 > 1048576)    { units[4]="MB"; $4=$4/1024/1024      }
                else if ($4 > 1024)       { units[4]="KB"; $4=$4/1024           }

                for (i=7;i<=8;i++) {
                         if ($i >  262144) { units[i]="GB"; $i=($i*4)/1024/1024 }
                    else if ($i >  256)    { units[i]="MB"; $i=($i*4)/1024      }
                    else if ($i >= 0)      { units[i]="KB"; $i=$i               }
                }
                printf "%s %s %s %.2f%s %s %s %.2f%s %.2f%s\n", $1, $2, $3, $4, units[4], $5, $6, $7, units[7], $8, units[8]
              }
' output_file.txt

NOTE: this replaces all of OP's current code (cat | grep | awk | awk | awk)

This generates:

8389645 52564801 700010000000000 654.36MB 8482408 R 654.34MB 4.00KB
3146755 52564802 700020000000000 20.00GB 251660232 V 3.00GB 17.00GB
3146759 52564803 700030000000000 63.09GB 1 B 63.09GB 0.00KB
3146756 52564804 700040000000000 40.57GB 1 B 40.57GB 0.00KB
3146760 52564805 700050000000000 2.09MB 27096 M 2.09MB 0.00KB
3146762 52564806 700060000000000 2.09MB 27096 M 2.09MB 0.00KB
3146763 52564807 700070000000000 2.09MB 27096 M 2.09MB 2.00KB
4195340 52564808 700080000000000 2.09MB 27096 M 2.09MB 2.00KB
Total: - - 124.30GB - - 107.30GB 17.00GB

答案2

得分: 0

以下是翻译好的部分:

here's the calculation part. you can fill in the units manually :

Segment Summary:
id         key        addr             size             ovhd     class blkused  blkfree
8389645    52564801   700010000000000  686145536        8482408  R     167512   4
3146755    52564802   700020000000000  21474836480      251660232 V     785701   4457179
3146759    52564803   700030000000000  67738288128      1        B     16537668 0
3146756    52564804   700040000000000  43558125568      1        B     10634308 0
3146760    52564805   700050000000000  2195456          27096    M     536      0
3146762    52564806   700060000000000  2195456          27096    M     536      0
3146763    52564807   700070000000000  2195456          27096    M     534      2
4195340    52564808   700080000000000  2195456          27096    M     534      2
Total:     -          -                133466177536     -        -     28127329 4457187' |

>       mawk 'BEGIN { CONVFMT = OFMT = "%.7f" 
>                          _ +=_+=_^ = _<_ 
>             } (__ = +$_) { 
>                 if ((___= _++^_--)<__) { 
>                     $_ /= ___^ ( (_^(--*_)<__) + \
>                          ((_++-+-++_)^(_+_)<__) + !!--) 
>                 }
>                 for(___ = _+_-!!_; ___ <= NF; ___++) { 
>                 
>                    $___ *= (_^(-_+_+(-_)^((\
>                      __  = +$___)<^(--*_++))))^(_^_<_) 
>                 } 
>             } ';

Segment  Summary:
id       key       addr             size         ovhd       class  blkused      blkfree
8389645  52564801  700010000000000  654.3593750  8482408    R      654.3437500  4
3146755  52564802  700020000000000  20           251660232  V      2.9972115    17.0027885
3146759  52564803  700030000000000  63.0861969   1          B      63.0861969   0
3146756  52564804  700040000000000  40.5666656   1          B      40.5666656   0
3146760  52564805  700050000000000  2.0937500    27096      M      2.0937500    0
3146762  52564806  700060000000000  2.0937500    27096      M      2.0937500    0
3146763  52564807  700070000000000  2.0937500    27096      M      2.0859375    2
4195340  52564808  700080000000000  2.0937500    27096      M      2.0859375    2
Total:   -         -                124.3000641  -          -      107.2972450  17.0028191
英文:

here's the calculation part. you can fill in the units manually :

echo &#39;

Segment Summary:
id         key        addr             size             ovhd     class blkused  blkfree
8389645    52564801   700010000000000  686145536        8482408  R     167512   4
3146755    52564802   700020000000000  21474836480      251660232 V     785701   4457179
3146759    52564803   700030000000000  67738288128      1        B     16537668 0
3146756    52564804   700040000000000  43558125568      1        B     10634308 0
3146760    52564805   700050000000000  2195456          27096    M     536      0
3146762    52564806   700060000000000  2195456          27096    M     536      0
3146763    52564807   700070000000000  2195456          27096    M     534      2
4195340    52564808   700080000000000  2195456          27096    M     534      2
Total:     -          -                133466177536     -        -     28127329 4457187&#39; |

> mawk 'BEGIN { CONVFMT = OFMT = "%.7f"
> _ +=+=^ = <
> } (__ = +$) {
> if ((
= ++^--)<) {
> $_ /= ^ ( (^(--*)<) +
> ((++-+-++)^(+)<
) + !!--)
> }
> for(
__ = +-!!; ___ <= NF; ++) {
>
> $
*= (
^-(++(-)^((
> __ = +$
)<^(--*++))))^(^_<__)
> }
> } _'

Segment  Summary:
id       key       addr             size         ovhd       class  blkused      blkfree
8389645  52564801  700010000000000  654.3593750  8482408    R      654.3437500  4
3146755  52564802  700020000000000  20           251660232  V      2.9972115    17.0027885
3146759  52564803  700030000000000  63.0861969   1          B      63.0861969   0
3146756  52564804  700040000000000  40.5666656   1          B      40.5666656   0
3146760  52564805  700050000000000  2.0937500    27096      M      2.0937500    0
3146762  52564806  700060000000000  2.0937500    27096      M      2.0937500    0
3146763  52564807  700070000000000  2.0937500    27096      M      2.0859375    2
4195340  52564808  700080000000000  2.0937500    27096      M      2.0859375    2
Total:   -         -                124.3000641  -          -      107.2972450  17.0028191

huangapple
  • 本文由 发表于 2023年3月7日 03:46:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655197.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定