我的制表符分隔文件如下:
Presence For Month 01/2020
Sl No Salary no Name 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr
1 111111 aaaaaaaaaaaaaaaaaaaaaaaa L A A A A A A A A A A A A A A A A A A A A A A A A
2 222222 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa P P P P P P P P P P A P P P P P P P P P P P F P P
我想计算他们的存在、缺席或从第 4 列开始的任何其他评论。这是按月汇总,因此总天数可以是 28、29、30 或 31。
预期输出为:
Presence For Month 01/2020
Sl Salary no Name 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 TOTALS
We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr P A X ALL
1 111111 aaaaaaaaaaaaaaaaaaaaaaaa X A A A A A A A A A A A A A A A A A A A A A A A A P X X X X X 1 24 6 31
2 222222 aaaaaaaaaaaaaaaaaaaaaaaa P P P P P P P P P P A A A A A A A A A A X X X X X X X X X X P 11 10 10 31
headers=`awk -F"\t" 'BEGIN {
print "------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"}{
if (NR==2) { printf "%5s|%9s|%-35s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%16s\n","Sl","SR NO","NAME",$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,"TOTALS"}
if (NR==3) { printf "%5s|%9s|%-35s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%16s\n","Sl","SR NO","NAME",$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,"TOTALS"} } END {
print "------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"} ' con `
awk '$1~/[0-9]/{ print $0}' con | tr '\t' '|' | tr -s ' ' > details
awk -F"|" '{ print $2 }' details > sr_details
for sr in `cat sr_details`
do
echo "$sr" >.sr1
sr=`awk '{print $1}' .sr1`
#echo "`grep "^$sr" sr_details`" > sr2
grep "$sr" details > sr2
tot=`cat sr2 | tr '|' '\n' | awk 'NR>3 && $1~/[A-Z]/{ print $1 "|"}' | sort | uniq -c | tr '\n' '|'`
awk -F"|" -v tot="$tot" '{ print $0"|"tot }' sr2 >> final_details
done
awk -F"|" -v headers="$headers" 'BEGIN { printf "%s\n",headers } (NR%30)==0 { printf "%s\n",headers } {
printf "%5s|%9s|%-35s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s %2s %2s\n-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n",$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30,$31,$32,$33,$34,$35,$37,$39,$41 }' /root/Desktop/final_details > presence
rm -f final_details details sr_details sr2
答案1
您的预期输出似乎与您的示例输入不匹配,我无法想象为什么a
s 的一个字符串在输出中被截断,所以这是一个好的开始:
$ cat tst.awk
BEGIN { FS=" *\t *"; OFS="\t" }
NR == 1 { print }
NR == 2 { print $0, "TOTALS" }
NR == 3 { print $0, "P", "A", "X", "ALL" }
NR > 3 {
delete cnt
tot = 0
for (i=4; i<=NF; i++) {
if ($i ~ /^$/) {
$i = "X"
}
type = $i
gsub(/[[:space:]]/,"",type)
cnt[type]++
tot++
}
print $0, cnt["P"]+0, cnt["A"]+0, cnt["X"]+0, tot+0
}
$ awk -f tst.awk file
Presence For Month 01/2020
Sl No Salary no Name 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 TOTALS
We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr P A X ALL
1 111111 aaaaaaaaaaaaaaaaaaaaaaaa L A A A X A A A A A X X A A A A A A X A A A A A X X A A A A A 0 24 6 31
2 222222 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa P P P P X P P P P P X X P A P P P P X P P P P P X X P P F P P 23 1 6 31
如果您不知道如何完成它,请更新您的问题以澄清您的要求并提供更好的示例。
我发现长输出行的中间间距出了问题 - 这是复制/粘贴问题,而不是脚本问题。