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

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

Combine awk to perform calculation on different columns

问题

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

  1. cat output_file.txt | awk -v OFS="\t" '/7000|Total:/ {
  2. if ($4 > 1073741824)
  3. $4 = sprintf("%.2fGB", $4 / 1024 / 1024 / 1024);
  4. else if ($4 > 1048576)
  5. $4 = sprintf("%.2fMB", $4 / 1024 / 1024);
  6. else if ($4 > 1024)
  7. $4 = sprintf("%.2fKB", $4 / 1024);
  8. if ($7 >= 0)
  9. $7 = sprintf("%.2fGB", $7 * 4 / 1024 / 1024);
  10. if ($8 >= 0)
  11. $8 = sprintf("%.2fKB", $8 * 4 / 1024);
  12. print $1, $2, $3, $4, $5, $6, $7, $8;
  13. }'

这个命令首先匹配包含"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)

  1. Segment Summary:
  2. id key addr size ovhd class blkused blkfree
  3. 8389645 52564801 700010000000000 686145536 8482408 R 167512 4
  4. 3146755 52564802 700020000000000 21474836480 251660232 V 785701 4457179
  5. 3146759 52564803 700030000000000 67738288128 1 B 16537668 0
  6. 3146756 52564804 700040000000000 43558125568 1 B 10634308 0
  7. 3146760 52564805 700050000000000 2195456 27096 M 536 0
  8. 3146762 52564806 700060000000000 2195456 27096 M 536 0
  9. 3146763 52564807 700070000000000 2195456 27096 M 534 2
  10. 4195340 52564808 700080000000000 2195456 27096 M 534 2
  11. Total: - - 133466177536 - - 28127329 4457187
  12. (* segment locked in memory)
  13. 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:

  1. cat output_file.txt | grep -E '7000|Total:' | awk '{
  2. 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;
  3. 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;
  4. 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 '{
  5. 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;
  6. 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;
  7. 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 '{
  8. 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";
  9. 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";
  10. 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:

  1. 8389645 52564801 700010000000000 654.36MB 8482408 R 654.34MB 4.00KB
  2. 3146755 52564802 700020000000000 20.00GB 251660232 V 3.00GB 17.00GB
  3. 3146759 52564803 700030000000000 63.09GB 1 B 63.09GB 0.00KB
  4. 3146756 52564804 700040000000000 40.57GB 1 B 40.57GB 0.00KB
  5. 3146760 52564805 700050000000000 2.09MB 27096 M 2.09MB 0.00KB
  6. 3146762 52564806 700060000000000 2.09MB 27096 M 2.09MB 0.00KB
  7. 3146763 52564807 700070000000000 2.09MB 27096 M 2.09MB 2.00KB
  8. 4195340 52564808 700080000000000 2.09MB 27096 M 2.09MB 2.00KB
  9. 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:

  1. awk '
  2. /7000|Total:/ { if ($4 > 1073741824) { units[4]="GB"; $4=$4/1024/1024/1024 }
  3. else if ($4 > 1048576) { units[4]="MB"; $4=$4/1024/1024 }
  4. else if ($4 > 1024) { units[4]="KB"; $4=$4/1024 }
  5. for (i=7;i<=8;i++) {
  6. if ($i > 262144) { units[i]="GB"; $i=($i*4)/1024/1024 }
  7. else if ($i > 256) { units[i]="MB"; $i=($i*4)/1024 }
  8. else if ($i >= 0) { units[i]="KB"; $i=$i }
  9. }
  10. 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]
  11. }
  12. ' output_file.txt

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

This generates:

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

答案2

得分: 0

以下是翻译好的部分:

  1. here's the calculation part. you can fill in the units manually :
  2. Segment Summary:
  3. id key addr size ovhd class blkused blkfree
  4. 8389645 52564801 700010000000000 686145536 8482408 R 167512 4
  5. 3146755 52564802 700020000000000 21474836480 251660232 V 785701 4457179
  6. 3146759 52564803 700030000000000 67738288128 1 B 16537668 0
  7. 3146756 52564804 700040000000000 43558125568 1 B 10634308 0
  8. 3146760 52564805 700050000000000 2195456 27096 M 536 0
  9. 3146762 52564806 700060000000000 2195456 27096 M 536 0
  10. 3146763 52564807 700070000000000 2195456 27096 M 534 2
  11. 4195340 52564808 700080000000000 2195456 27096 M 534 2
  12. Total: - - 133466177536 - - 28127329 4457187' |
  13. > mawk 'BEGIN { CONVFMT = OFMT = "%.7f"
  14. > _ +=_+=_^ = _<_
  15. > } (__ = +$_) {
  16. > if ((___= _++^_--)<__) {
  17. > $_ /= ___^ ( (_^(--*_)<__) + \
  18. > ((_++-+-++_)^(_+_)<__) + !!--)
  19. > }
  20. > for(___ = _+_-!!_; ___ <= NF; ___++) {
  21. >
  22. > $___ *= (_^(-_+_+(-_)^((\
  23. > __ = +$___)<^(--*_++))))^(_^_<_)
  24. > }
  25. > } ';
  26. Segment Summary:
  27. id key addr size ovhd class blkused blkfree
  28. 8389645 52564801 700010000000000 654.3593750 8482408 R 654.3437500 4
  29. 3146755 52564802 700020000000000 20 251660232 V 2.9972115 17.0027885
  30. 3146759 52564803 700030000000000 63.0861969 1 B 63.0861969 0
  31. 3146756 52564804 700040000000000 40.5666656 1 B 40.5666656 0
  32. 3146760 52564805 700050000000000 2.0937500 27096 M 2.0937500 0
  33. 3146762 52564806 700060000000000 2.0937500 27096 M 2.0937500 0
  34. 3146763 52564807 700070000000000 2.0937500 27096 M 2.0859375 2
  35. 4195340 52564808 700080000000000 2.0937500 27096 M 2.0859375 2
  36. Total: - - 124.3000641 - - 107.2972450 17.0028191
英文:

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

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

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

  1. Segment Summary:
  2. id key addr size ovhd class blkused blkfree
  3. 8389645 52564801 700010000000000 654.3593750 8482408 R 654.3437500 4
  4. 3146755 52564802 700020000000000 20 251660232 V 2.9972115 17.0027885
  5. 3146759 52564803 700030000000000 63.0861969 1 B 63.0861969 0
  6. 3146756 52564804 700040000000000 40.5666656 1 B 40.5666656 0
  7. 3146760 52564805 700050000000000 2.0937500 27096 M 2.0937500 0
  8. 3146762 52564806 700060000000000 2.0937500 27096 M 2.0937500 0
  9. 3146763 52564807 700070000000000 2.0937500 27096 M 2.0859375 2
  10. 4195340 52564808 700080000000000 2.0937500 27096 M 2.0859375 2
  11. 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:

确定