AWK:如何将 awk 脚本中的列作为 for 循环的一部分进行排序
AWK: how to sort column in awk script as part of a for loop
我有以下 awk 脚本:
#!/usr/bin/env bash
awk '
BEGIN{
FS=OFS="\t";
printf "\n\n%33-s\t%20-s",
"Description", "Amount"
printf "\n-------------------------------------------------- \n";
}
{
if ( > 0) {
a[] += ;
sa += ;
}
else {
b[] += ;
sb += ;
}
}
END{
print "INCOME" "\n";
for (i in a) {
printf "%-33s\t%-20 .2f\n", i, a[i];
}
printf "%-33s\t%-20 .2f\n", "\nTOTAL INCOME", sa;
print "\n""\n" "EXPENSES" "\n";
for (j in b) {
printf "%-33s\t%-20 .2f\n", j, b[j];
}
printf "%-33s\t%-20 .2f\n", "\nTOTAL EXPENSES", sb;
}
' "${@:--}"
这会在 运行 之后按预期打印以下内容 ./report out.txt:
Description Amount
--------------------------------------------------
INCOME
Client Income 65855.52
Other Income 33496.21
Refunds 24072.49
Transfers 66445.38
Rental Income 74505.46
Tax Refunds 2200.16
Salaries & Wages 234198.04
Interest 4.07
TOTAL INCOME 500777.33
EXPENSES
Business - Miscellaneous -473.55
Government -985.83
Personal Expenditures -89212.40
Hiring & Rentals -3271.20
Travel -24148.74
Accounting & Legal -3091.20
Bank – Charges -1106.87
Motor Vehicle – Miscellaneous -153.59
Vehicle Miscellaneous -70.00
Capex -18186.76
Food - Fast Food -3004.19
Christian to Edorne -1018.88
Business – Miscellaneous -63.09
Bank – Withdrawals -5720.00
IT – Subscriptions -1077.95
Property – Rent -60100.00
Food – Groceries -65760.27
Equipment Hiring -3325.00
Property – Maintenance -13956.72
Transfers Overseas -27638.31
Property – Bond -1800.00
Business – Repairs & Maintenance -1362.25
Weed Spray Chemicals -133.17
Transport -218.40
Motor Vehicle Expenses -862.52
Business – ACC -206.04
Education -1515.04
Office & Stationary -323.34
Food – Liquor -405.55
Petrol - Vehicle -10341.62
Petrol - Equipment -823.12
Transfers -211239.22
Property - Maintenance -10762.56
Business – Storage -1659.50
Motor Vehicle – Parking -39.90
Motor Vehicle – WOF -264.05
IT – ISP -14706.36
Tax -11783.08
Food – Fast Food -4410.23
Electricity -6980.73
Christian to Crewcut -20.95
Healthcare -7250.95
Vehicle – Maintenance -302.00
Food – Eating Out -6209.95
Business Services – Mail -400.00
Household -2190.58
Edorne to Christian -4961.30
Salaries & Wages -55634.21
Miscellaneous -1788.81
Insurance -2623.96
Motor Vehicle – Maintenance -4711.62
Property – Body Corporate -26736.16
Principal & Interest -12605.61
Property – Rates -16336.82
TOTAL EXPENSES -743974.15
我的问题是如何按字母顺序(第一列)对收入和支出项目进行排序?我试图通过将 for 循环行更改为
来对第一列进行排序
printf "%-33s\t%-20 .2f\n", i, a[i] | "sort -u";
printf "%-33s\t%-20 .2f\n", j, b[j] | "sort -u";
这是行不通的。
出于安全原因和大量数据,我真的无法提供源数据。我希望无论如何有人可以帮助我解决这个问题,因为我的问题本质上是相当笼统的。
我认为您会发现在 运行 此脚本之前对日期进行排序更容易。您不告诉我们它来自哪里,而是指定排序列或通过 awk '{ print $3 $7}' |排序
使用 GNU awk
,您可以 control array scanning 通过 PROCINFO["sorted_in"]
对数组进行排序(按索引或数据),以便 for (index in array)
以所需的顺序处理数组。
将其添加到您的 END
块处理中将如下所示:
END{
PROCINFO["sorted_in"]="@ind_str_asc" # sort array by index based on "string" data and in "ascending" order
print "INCOME" "\n"
for (j in a) {
printf "%-33s\t%-20 .2f\n", i, a[i];
}
printf "%-33s\t%-20 .2f\n", "\nTOTAL INCOME", sa;
print "\n""\n" "EXPENSES" "\n"
for (j in b) {
printf "%-33s\t%-20 .2f\n", j, b[j];
}
printf "%-33s\t%-20 .2f\n", "\nTOTAL EXPENSES", sb
}
注意: 一旦设置 PROCINFO["sorted_in"]
将应用于所有数组引用,直到 awk
脚本结束或直到新的 PROCINFO["sorted_in"]
已应用
我有以下 awk 脚本:
#!/usr/bin/env bash
awk '
BEGIN{
FS=OFS="\t";
printf "\n\n%33-s\t%20-s",
"Description", "Amount"
printf "\n-------------------------------------------------- \n";
}
{
if ( > 0) {
a[] += ;
sa += ;
}
else {
b[] += ;
sb += ;
}
}
END{
print "INCOME" "\n";
for (i in a) {
printf "%-33s\t%-20 .2f\n", i, a[i];
}
printf "%-33s\t%-20 .2f\n", "\nTOTAL INCOME", sa;
print "\n""\n" "EXPENSES" "\n";
for (j in b) {
printf "%-33s\t%-20 .2f\n", j, b[j];
}
printf "%-33s\t%-20 .2f\n", "\nTOTAL EXPENSES", sb;
}
' "${@:--}"
这会在 运行 之后按预期打印以下内容 ./report out.txt:
Description Amount
--------------------------------------------------
INCOME
Client Income 65855.52
Other Income 33496.21
Refunds 24072.49
Transfers 66445.38
Rental Income 74505.46
Tax Refunds 2200.16
Salaries & Wages 234198.04
Interest 4.07
TOTAL INCOME 500777.33
EXPENSES
Business - Miscellaneous -473.55
Government -985.83
Personal Expenditures -89212.40
Hiring & Rentals -3271.20
Travel -24148.74
Accounting & Legal -3091.20
Bank – Charges -1106.87
Motor Vehicle – Miscellaneous -153.59
Vehicle Miscellaneous -70.00
Capex -18186.76
Food - Fast Food -3004.19
Christian to Edorne -1018.88
Business – Miscellaneous -63.09
Bank – Withdrawals -5720.00
IT – Subscriptions -1077.95
Property – Rent -60100.00
Food – Groceries -65760.27
Equipment Hiring -3325.00
Property – Maintenance -13956.72
Transfers Overseas -27638.31
Property – Bond -1800.00
Business – Repairs & Maintenance -1362.25
Weed Spray Chemicals -133.17
Transport -218.40
Motor Vehicle Expenses -862.52
Business – ACC -206.04
Education -1515.04
Office & Stationary -323.34
Food – Liquor -405.55
Petrol - Vehicle -10341.62
Petrol - Equipment -823.12
Transfers -211239.22
Property - Maintenance -10762.56
Business – Storage -1659.50
Motor Vehicle – Parking -39.90
Motor Vehicle – WOF -264.05
IT – ISP -14706.36
Tax -11783.08
Food – Fast Food -4410.23
Electricity -6980.73
Christian to Crewcut -20.95
Healthcare -7250.95
Vehicle – Maintenance -302.00
Food – Eating Out -6209.95
Business Services – Mail -400.00
Household -2190.58
Edorne to Christian -4961.30
Salaries & Wages -55634.21
Miscellaneous -1788.81
Insurance -2623.96
Motor Vehicle – Maintenance -4711.62
Property – Body Corporate -26736.16
Principal & Interest -12605.61
Property – Rates -16336.82
TOTAL EXPENSES -743974.15
我的问题是如何按字母顺序(第一列)对收入和支出项目进行排序?我试图通过将 for 循环行更改为
来对第一列进行排序printf "%-33s\t%-20 .2f\n", i, a[i] | "sort -u";
printf "%-33s\t%-20 .2f\n", j, b[j] | "sort -u";
这是行不通的。 出于安全原因和大量数据,我真的无法提供源数据。我希望无论如何有人可以帮助我解决这个问题,因为我的问题本质上是相当笼统的。
我认为您会发现在 运行 此脚本之前对日期进行排序更容易。您不告诉我们它来自哪里,而是指定排序列或通过 awk '{ print $3 $7}' |排序
使用 GNU awk
,您可以 control array scanning 通过 PROCINFO["sorted_in"]
对数组进行排序(按索引或数据),以便 for (index in array)
以所需的顺序处理数组。
将其添加到您的 END
块处理中将如下所示:
END{
PROCINFO["sorted_in"]="@ind_str_asc" # sort array by index based on "string" data and in "ascending" order
print "INCOME" "\n"
for (j in a) {
printf "%-33s\t%-20 .2f\n", i, a[i];
}
printf "%-33s\t%-20 .2f\n", "\nTOTAL INCOME", sa;
print "\n""\n" "EXPENSES" "\n"
for (j in b) {
printf "%-33s\t%-20 .2f\n", j, b[j];
}
printf "%-33s\t%-20 .2f\n", "\nTOTAL EXPENSES", sb
}
注意: 一旦设置 PROCINFO["sorted_in"]
将应用于所有数组引用,直到 awk
脚本结束或直到新的 PROCINFO["sorted_in"]
已应用