0

The script will read the contents of a user-input file and count the number of employees with a specific job. Ex file line:

Sophia Lewis, 542467, Accountant 

The script I have so far is:

if [ -s 1ドル ]
then
cat 1ドル | tr -s ' ' | cut -d' ' -f4- | sort | uniq -c
else
 echo "ERROR: file '1ドル' does not exist."
fi

Output:

4 Sales 
2 Accountant 
1 CEO 

But I want the output to appear as:

There are 4 ‘Sales’ employees. 
There are 2 ‘Accountant’ employees. 
There is 1 ‘CEO’ employee. 
There are a total of 7 employees in the company

Should I take out the cat and put in echo statements so I can customize each line? And is there a way for it to know if it should be "is/are" x employees?

Rui F Ribeiro
57.9k28 gold badges154 silver badges237 bronze badges
asked Apr 21, 2014 at 16:22
1
  • Yes, you will need to rewrite it and no, the only way for it to know is for you to code it. Commented Apr 21, 2014 at 16:38

1 Answer 1

3

If your shell is bash version 4:

declare -i total=0
declare -A type
if [ -s "1ドル" ]; then
 while IFS=, read name id job; do
 [[ $job =~ ^[[:space:]]*(.+)[[:space:]]*$ ]] &&
 (( type["${BASH_REMATCH[1]}"]++, total++ ))
 done < "1ドル"
 for job in "${!type[@]}"; do
 printf "There are %d '%s' employees.\n" ${type["$job"]} "$job"
 done
 echo "There are a total of $total employees in the company"
else
 echo "ERROR: file '1ドル' does not exist or has zero size."
fi

Or use awk:

awk -F' *, *' '
 { type[3ドル]++; total++ } 
 END {
 for (job in type) 
 printf "There are %d '\''%s'\'' employees.\n", type[job], job
 print "There are a total of", total, "employees in the company"
 }
' "1ドル"
answered Apr 21, 2014 at 17:01

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.