Issue
I'm learning how to use Shell script and I have a CSV file containing 5 columns name forname telephone room email
and I want to find which room
contains the most persons.
For the moment I did the following code and I'm stuck at the part where I need to count which room has the more employee or which room appear the most in the file
input="x.csv"
while read line; do
room=$(echo $line | cut -d \; -f 4)
if [ -n "$room" ]; then
fi
done < ${input}
Solution
Counting the occurances of unique values is probably done best by using uniq -c
.
So to count the entries for each room individually, you need to extract a list which contains the column room
. awk
is probably the best tool in the bash environment to do that.
For example:
#!/bin/bash
input="x.csv"
awk '{print $4}' $input | sort | uniq -c
this will return a list with two columns. The first column contains the number of occurences of the respective value in column two, such as:
4 room1b
2 room1
1 room2
1 room3
For more complex analysis follow Corentin's lead to extend awk
's input.
Answered By - blubase