I'm trying to find the average number of files per day.
The following script works:
#!/bin/sh
ls -l | grep "^-" | awk '{
key=$6$7
freq[key]++
}
END {
for (date in freq)
printf "%s\t%d\n", date, freq[date]
}'
When I add do and done for the for loop like below:
#!/bin/sh
ls -l | grep "^-" | awk '{
key=$6$7
freq[key]++
}
END {
for (date in freq)
do
printf "%s\t%d\n", date, freq[date]
done
}'
I get the below error:
awk: cmd. line:8: done
awk: cmd. line:8: ^ syntax error
How to write multi-line for loop statements inside END block of awk command?
Solution:
We need to use { and } to start and end for loop in END block of awk command.
#!/bin/sh
ls -l | grep "^-" | awk '{
key=$6$7
freq[key]++
}
END {
for (date in freq)
{
count=count+1
total=total+freq[date];
printf "%s\t%d\n", date, freq[date]
}
printf "Average files per day : %d\n",(total/count)
}'