Bash Script Generate Output Value "0"

Hi All

could u please help me about my query problem below :

cat filesname | awk -F"[ |]" '{cc[substr($2,1,8)]++ } END {for ( i in cc ) print i,cc}' | sort -nk1

will shown output below :

12:02:06 4
12:02:08 3
12:02:12 8
12:02:17 2

my expectation result for value "0" also will print also to output files.

expectation like below :

12:02:06 4
12:02:07 0
12:02:08 3
12:02:09 0
12:02:10 0
12:02:11 0
12:02:12 8
12:02:13 0
12:02:14 0
12:02:15 0
12:02:16 0
12:02:17 2

aprreciate for any one can help my problem above.

Thanks
Fajar

It would help if you could post an extract from filesname, containing some of the lines representing the issue.

Also, for reference, awk does not need the help of cat to read filesname .

Instead of:

cat filesname | awk ...

do:

awk ... filesname

So you are extracting timestamps from the file and counting how many times each timestamp appears? But you want to add in missing timestamps to the list? That is, timestamps that don't actually appear in the file and therefore have the count of zero?

Do you know what the first and last timestamps are going to be before you begin?

Andrew

Hi Andrew

Yes correct, for value 0 currently missing in output files.

for the first time stamp begin 00:00:00 and last time stamp 23:59:59

Thanks
Fajar

So you want 86400 lines all but four of which have a zero count?

Try this:

awk -F"[ |]" '
{cc[substr($2,1,8)]++ }
END {
 for(h=0;h<24;h++) for(m=0;m<60;m++) for(s=0;s<60;s++) {
    i=sprintf("%02d:%02d:%02d",h,m,s)
    print i,cc+0
}}' filesname
2 Likes

Recent bash ? Try

echo -e "x "{00..23}:{00..59}:{00..59}"\n" | awk '{CNT[$2]++} END {for (c in CNT) print c, CNT[c]-1}' - file | sort
.
.
.
12:02:05 0
12:02:06 2
12:02:07 0
12:02:08 3
12:02:09 0
12:02:10 0
12:02:11 0
12:02:12 3
12:02:13 0
.
.
.
1 Like

Hi Chubler

Great Thanks for your help, its works