awk 命令或脚本计算员工的日薪和小时薪

awk 命令或脚本计算员工的日薪和小时薪

我有一个包含几名员工的文本文件。文本文件遵循此方案。

员工姓名 部门 20.00

我需要一个带有变量的 awk 命令来计算每日工资。我还需要用制表符和美元符号分隔每个字段,以显示每小时和每日费率,以及 /hr 和 /day 尾随文本。所以,例如

      员工姓名 部门 $20.00/小时 $160/天
      员工姓名 部门 $50.00/小时 $400/天
      员工姓名 部门 $200.00/小时 $1600/天

我还需要总计并显示所有工人的时薪,并显示文本“工人每小时总共赚取 889 美元(或任何总数)。

我是 awk 的新手,我已经做了一些谷歌搜索,但我真的不知道从哪里开始。

答案1

awk '{$NF="$"$NF"/hr " "$"$NF*8"/day";print $0}' filename

输出

Adam Adamson Accounting $20.00/hr $160/day
Iver Iverson InfoTech $50.00/hr $400/day
Cary Caryson ChiefExecutive $200.00/hr $1600/day
Mary Maryson Maintenance $15.00/hr $120/day
Stan Stanson SalesDept. $10.00/hr $80/day
Scot Scotson SalesDept. $10.00/hr $80/day
Eric Ericson Executive $100.00/hr $800/day
Enid Enidson Executive $100.00/hr $800/day
Maye Mayeson Maintenance $15.00/hr $120/day
Axel Axelson Accounting $21.00/hr $168/day
Pete Peteson PayrollDept. $15.50/hr $124/day
Mick Mickson Marketing $12.00/hr $96/day
Iris Irisson InfoTech $55.00/hr $440/day
Hank Hankson HumanRes $42.42/hr $339.36/day

答案2

awk -v hpd=8 '
  { printf "%s\t%s\t%s\t$%.2f/hr\t$%.2f/day\n", $1, $2, $3, $4, $4 * hpd;
    ht += $4; # hourly total
  };

  END {
    printf "\nWorkers earned a combined $%.2f per hour\n", ht
  }
' input.txt
Adam    Adamson Accounting      $20.00/hr       $160.00/day
Iver    Iverson InfoTech        $50.00/hr       $400.00/day
Cary    Caryson ChiefExecutive  $200.00/hr      $1600.00/day
Mary    Maryson Maintenance     $15.00/hr       $120.00/day
Stan    Stanson SalesDept.      $10.00/hr       $80.00/day
Scot    Scotson SalesDept.      $10.00/hr       $80.00/day
Eric    Ericson Executive       $100.00/hr      $800.00/day
Enid    Enidson Executive       $100.00/hr      $800.00/day
Maye    Mayeson Maintenance     $15.00/hr       $120.00/day
Axel    Axelson Accounting      $21.00/hr       $168.00/day
Pete    Peteson PayrollDept.    $15.50/hr       $124.00/day
Mick    Mickson Marketing       $12.00/hr       $96.00/day
Iris    Irisson InfoTech        $55.00/hr       $440.00/day
Hank    Hankson HumanRes        $42.42/hr       $339.36/day

Workers earned a combined $665.92 per hour

第一个代码块(即第一对大括号内{}针对输入文件的每一行运行(input.txt在本例中)。该END {...}块仅在读取并处理所有输入后运行一次。

hpd=8您可以通过更改命令行部分来更改每天的小时数。我可以将其硬编码为 8,但我认为将其设为变量更有趣。

作为练习,看看是否可以修改最终printf语句,以便它也打印每日总计。弄清楚这一点将帮助您理解 printf 语句的作用。输出应该类似于:

Workers earned a combined $665.92 per hour or $5327.36 per day

答案3

艰辛的道路。对于仅使用多维数组的 GNU awk。无论内容的长度如何(如使用该column -t实用程序时),列的格式都会保留。分隔符在 OFS 变量中全局定义:

awk -v pre="$" -v suf="/hr" '
        {hr+=$NF;
        for(i=1; i<=NF; i++) {
                row[NR][i]=$i
                l=length($i)
                if(l>len[i]) len[i]=l
                }
        }
END     {for(i in row){
                for(j in row[i])
                        printf("%-*s" OFS, (j==NF?4:0) + len[j],
                                j==NF? pre row[i][j] suf: row[i][j])
                print pre row[i][j] * 8 "/day"
                }
        print "\nworkers earned a combined $" hr " per hour"
        }
' OFS='\t' file

第一部分中,计算每列的最大宽度,并在打印时根据它进行缩进。

答案4

需要用awk吗?

#!/bin/bash
while IFS='' read -r LinefromFile || [[ -n "${LinefromFile}" ]]; do
    a=($(echo "$LinefromFile"))
    printf "%-20s" $a[1] $a[2] $a[3] 
    printf "%-20s%.2f" $a[4] $((a[4] * 8))
    printf "\n"
done < "$1"


相关内容