lundi 23 avril 2018

Awk script else statement not working

I am writing an awk script inside of a shell script to display all logins of current month, and number of logins on each day of the month so far. I will have to determine the day on which the number of logins has been the greatest, but first I would like to figure out how to write the else statement of my if statement which follows in the code below:

#!/bin/bash
month=$(date +"%b")
day=$(date +"%e")

last | awk -v m=$month -v d=$day 'BEGIN{
 max=0; c=0; maxd=day}
 ($5 ~ m) {
 if ($6 =d) {print; c++; print c}
 else {printf("Else statement test")}
 }
'

So far it works fine without the line containing the else statement, but it seems like it won't recognize the else no matter what I add to it. With

$5 ~ m

I check whether the current line is of current month, and then

$6 =d

checks if it's still the same day. If so, then counter increases, and I print the current number of daily logins. I would like to save the value of the counter to an associative array in the else statement and set the counter variable back to zero too when I encounter a new day (when $6 is no longer =d).

I've tried to add these operations to the else statement, but when I read the script, it won't recognize the "else". It will only print logins of today (Apr. 23) and count the lines, but won't execute the else part. Why isn't it working?

Aucun commentaire:

Enregistrer un commentaire