#!/bin/bash #################################### # # Processing a comma separated file # using a command line argument for the file # or using stdin # Author: Bill Kraynek # ##################################### trap "if [ -e temp1 ]; then rm temp1 temp2 temp3 temp4 temp5 temp6 temp7; fi" 0 # Get file from command or from stdin if [ $# = 1 ] then # Get file from command line tr -d "\"" < $1 > temp1 elif [ $# = 0 ] then # Get file from stdin cat > temp0 tr -d "\"" < temp0 > temp1 rm temp0 else echo -e "Usage: $0 file\nOr : $0 < file\nOr : $0" exit 1 fi # Pick out week and season fields cut -d, -f1-3 temp1 > week cut -d, -f1,7-8 temp1 > season sort week > temp2 sort season > temp3 # Recombine season and week join -1 1 -2 1 -t, -o "1.1 1.2 2.2" temp3 temp2 > temp4 sort -t, -k 2 -g -r temp4 > temp5 awk -F, ' BEGIN { { print "\n\tResults\nName\t Season\t Week\n"} } {t1+=$2;t2+=$3;printf "%-10.10s %5s\t%5s\n",$1,$2,$3};END {printf "%-10.10s %5s\t%5s\n","Total",t1,t2}' temp5 > results; cat results # Setup for example of the uniq command cat week season | awk -F, '{printf ",%s,\n",$1}' | sort > temp6 uniq -c temp6 > temp7 awk -F, ' BEGIN { {print "\n\tExample\n"} } {printf "%-8.8s appeared%3d times\n",$2,$1}' temp7 > final_results; cat final_results