user raw 発生回数を計算します。

user raw 発生回数を計算します。

私のタブで区切られたファイルは次のとおりです。

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

期待される出力がサンプル入力と一致しないようです。as文字列が出力から切り捨てられる理由を想像できないので、これは良い開始です。

$ 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

これを行う方法がわからない場合は、質問を更新して要件を明確にし、より良い例を提供してください。

長い出力行の中央の間隔に問題があることがわかりました。これはスクリプトの問題ではなく、コピー/貼り付けの問題でした。

関連情報