Skip to content

Instantly share code, notes, and snippets.

@skyscribe
Created August 14, 2012 01:02
Show Gist options
  • Save skyscribe/3345296 to your computer and use it in GitHub Desktop.
Save skyscribe/3345296 to your computer and use it in GitHub Desktop.
Filter cloc result for a certain folder and get statistic information
#!/bin/bash
for folder in `ls -l | grep "^d" | awk '{print $NF}'`;do
echo "counting lines for folder $folder" >> cloc.txt;
cloc.pl $folder >> cloc.txt;
done
filterCloc.awk cloc.txt > cloc_summary.txt
#!/bin/awk -f
/counting lines for folder/{
#print "current domain is", current_domain
current_domain = $NF
}
/^C\+\+ /{
code_stats[current_domain, "c++"] = $NF
}
/^C /{
code_stats[current_domain, "c source"] = $NF
}
/^C\/C\+\+ Header /{
code_stats[current_domain, "header"] = $NF
}
/^(make|CMake) /{
code_stats[current_domain, "make"] += $NF
}
END{
printf("%-40s %-10s %10s\n", "domain", "type", "loc");
previous_domain = "";
domain_total = 0;
all_total = 0;
n = asorti(code_stats, new_stat)
for (i = 1; i <= n; i++){
detail = new_stat[i]
split(detail, indexes, SUBSEP);
domain = indexes[1]
type = indexes[2]
loc = code_stats[detail]
if (domain != previous_domain){
printf("%-40s %-10s %10d\n", previous_domain, "total", domain_total);
printf("------------------------------------------------------------------\n");
all_total = all_total + domain_total
domain_total = 0;
}
printf("%-40s %-10s %10d\n", domain, type, loc);
domain_total = domain_total + loc
previous_domain = domain
}
printf("%-40s %-10s %10d\n", previous_domain, "total", domain_total);
printf("------------------------------------------------------------------\n");
printf("%-40s %-10s %10d\n", "ALL DOMAINS", "total", all_total);
}
@skyscribe
Copy link
Author

Key points:

  • Array sort by index : asorti
  • Multi-dimension array simulation
  • cloc tool to count loc

@skyscribe
Copy link
Author

cloc is a famous tool to count line of codes for SCM, its official site can be found here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment